LoveCherry

技术无极限

博客园 首页 新随笔 联系 订阅 管理
  192 Posts :: 0 Stories :: 3239 Comments :: 656 Trackbacks

有很多网友遇到这样的问题:在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就是这个下拉框,类型转换一下就能使用了

posted on 2005-04-04 13:52 lovecherry 阅读(6914) 评论(13) 编辑 收藏

Feedback

#1楼 2005-05-20 16:08 dd
支持!可是楼主大人如何得知触发的dropdownlist在datagrid中的索引呢?盼告!!
 回复 引用   

#2楼[楼主] 2005-05-20 16:13 lovecherry      
http://www.cnblogs.com/lovecherry/archive/2005/04/20/141375.html
 回复 引用 查看   

#3楼 2005-05-20 16:25 dd
自己补充一下
DropDownList ddl = (DropDownList)sender;
TableCell cell = (TableCell)ddl.Parent;
DataGridItem item = (DataGridItem)cell.Parent;
Response.Write(item.Cells[0].Text);

 回复 引用   

#4楼 2005-05-20 16:26 dd
多谢!
 回复 引用   

#5楼 2007-04-23 22:53 sean[未注册用户]
请问在模板列的控件事件中怎么访问当前datagrid行的DataItem呢?
请指教
急啊!
sean031@126.com

 回复 引用   

#6楼 2007-10-26 11:24 gxpotato[未注册用户]
@sean
2.0之后有个DateKeyNames的属性,实际上这个就是个主键,可以通过e的索引来获取这个主键的键值。不考虑性能的话,可以通过DataRowView或者直接通过TABLE.SELECT这个来获取那一行的数据。

 回复 引用   

#7楼 2007-11-19 17:14 gxpotato[未注册用户]
更加简单的就是:
DataRowView drv=e.Row.DataItem as DataRowView

直接就能用drv[“字段名”]来获取数据。

 回复 引用