c#ItemDataBound新事件处理程序

时间:2012-02-14 18:37:38

标签: c# asp.net

    protected virtual void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
      this.list = (DropDownList)e.Item.FindControl("edit_list");
      if (list != null)
      {
         list.SelectedIndexChanged += new EventHandler(List_SelectedIndexChanged);
       }     
    }

已分配列表,但selectedIndex eventHandler将不起作用 如果我让它RepairsStateList.BackColor = Color.Black;它正在工作

protected void List_SelectedIndexChanged(object source, System.EventArgs e)
{
    Response.Write("<script>alert('vv') </script>");
}

1 个答案:

答案 0 :(得分:1)

此下拉列表的

AutoPostBack属性必须设置为true ...

比你的代码必须

protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)    {        
// get reference to the row
GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);   

// Get the reference of this DropDownlist
DropDownList dropdownlist1 = (DropDownList) gvr.FindControl("dropdownlist1");

}

修改

替换此行
this.list = (DropDownList)e.Item.FindControl("edit_list");

这个

  DropDownList list = (DropDownList)e.Item.FindControl("edit_list");
  if (list != null)
  {
     list.SelectedIndexChanged += new EventHandler(List_SelectedIndexChanged);
   }