2011-03-16 62 views
1

我想捕获SelectedIndexChanged事件的下拉列表,我已经放入了一个GridView控件。它回发很好,但不会进入我的SelectedIndexChanged事件处理程序。这里是我的代码捕获DropDownList索引更改网格内部的事件

DropDownList myddl; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     this.myGridview.RowDataBound += new GridViewRowEventHandler(myGridview_RowDataBound); 
     myddl = new DropDownList(); 
     myddl.SelectedIndexChanged += new EventHandler(myddl_SelectedIndexChanged); 
     if (!Page.IsPostBack) 
     { 
      List<Team> teams = giveMeTeams(); 
      this.myGridview.DataSource = teams; 
      this.myGridview.AutoGenerateColumns = false; 

      BoundField col1 = new BoundField(); 
      col1.DataField = "Name"; 
      this.myGridview.Columns.Add(col1); 

      BoundField col2 = new BoundField(); 
      col2.DataField = "Sport"; 
      this.myGridview.Columns.Add(col2); 

      BoundField col3 = new BoundField(); 
      col3.DataField = "Status"; 
      this.myGridview.Columns.Add(col3); 

      this.myGridview.DataBind(); 
     } 
    } 

    void myGridview_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     myddl = new DropDownList(); 
     myddl.SelectedIndexChanged += new EventHandler(myddl_SelectedIndexChanged); 
     List<string> items = new List<string>(); 
     items.Add("good"); 
     items.Add("bad"); 
     myddl.DataSource = items; 
     myddl.AutoPostBack = true; 
     myddl.DataBind(); 
     e.Row.Cells[2].Controls.Add(myddl); 
    } 

    void myddl_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     string temp = "In Here"; //neve hits this code 
    } 

    private List<Team> giveMeTeams() 
    { 
     Teams teams = new Teams(); 
     teams.Add(new Team("RedWings", "Hockey", "good")); 
     teams.Add(new Team("Lions", "Football", "bad")); 
     teams.Add(new Team("Packers", "Football", "good")); 
     return teams; 
    } 

任何帮助,非常感谢。 谢谢,

编辑基于评论

如你所说......而且我还没有捕捉回来后我都试过了。这里是我的新代码

 void myGridview_RowCreated(object sender, GridViewRowEventArgs e) 
    { 
     DropDownList myddl = new DropDownList(); 
     myddl = new DropDownList(); 
     myddl.SelectedIndexChanged += new EventHandler(myddl_SelectedIndexChanged); 
     myddl.ID = "MyID" + e.Row.RowIndex.ToString(); 
     e.Row.Cells[2].Controls.Add(myddl); 
    } 

    void myGridview_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     DropDownList myddl = e.Row.FindControl("MyID" + e.Row.RowIndex.ToString()) as DropDownList; 
     //myddl.SelectedIndexChanged += new EventHandler(myddl_SelectedIndexChanged); 
     List<string> items = new List<string>(); 
     items.Add("good"); 
     items.Add("bad"); 
     myddl.DataSource = items; 
     myddl.DataMember = "Status"; 
     myddl.AutoPostBack = true; 
     myddl.DataBind(); 
     e.Row.Cells[2].Controls.Add(myddl); 
    } 

它仍然没有进入我的myddl_SelectedIndexChanged()事件处理程序。

回答

2

创建网格的RowCreated中的Dropdownlist并为其分配一个ID。通过e.Row.FindControl("MyDropdownlistID")获取RowDataBound中的这些下拉菜单并将它们绑定到数据源。创建不同的Dropdownlist实例,而不是引用总是相同的

+0

感谢您的回复....我刚刚尝试了您的建议 – mikemurf22 2011-03-17 18:23:37

+0

我用我的新代码更新了原始问题。还是行不通。任何想法我失踪? – mikemurf22 2011-03-17 18:30:31

+0

@ mikemurf22:您不必将RowIndex追加到DDL的ID,它只能是GridViewRow(它们的NamingContainer)的唯一内容。 ASP.Net自动生成唯一的ID,您可以通过FindControl(“MyID”)在RowDataBound中找到它们。但主要的问题是你在RowDataBound中再次添加它们。它们将在RowCreated的每个回传中重新创建。删除行e.Row.Cells [2] .Controls.Add(myddl);'。你还必须在RowCreated和RowDatabound中嵌套你的代码,在if(e.RowTypeRowType == DataControlRowType.DataRow){ }' – 2011-03-17 19:32:29