现在的位置: 首页 > 综合 > 正文

怎么触发DataGrid模板列中控件的事件?

2018年04月09日 ⁄ 综合 ⁄ 共 798字 ⁄ 字号 评论关闭

有很多网友遇到这样的问题:在DataGrid模板列中的按钮可以触发ItemCommand事件但是怎么触发其他事件?(比如放置DropDownList怎么触发SelectedIndexChange事件?……)
按照
http://www.cnblogs.com/lovecherry/archive/2005/03/25/125525.html我们进行一下修改:
在模板列中增加一个DropDownList

<asp:TemplateColumn HeaderText="学院">
      <ItemTemplate>
       <asp:DropDownList ID="dep2" Runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"></asp:DropDownList>
      </ItemTemplate>
      <EditItemTemplate>
       <asp:DropDownList ID="dep" Runat="server"></asp:DropDownList>
      </EditItemTemplate>
</asp:TemplateColumn>

在前台直接加上事件DropDownList2_SelectedIndexChanged

然后在后台添加事件就可以了

protected void DropDownList2_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   Response.Write(((DropDownList)sender).SelectedItem);
  }

注意,事件不能是private的,这里的sender就是这个下拉框,类型转换一下就能使用了

 

抱歉!评论已关闭.