有时候希望在 GridView 模板中使用自动回发的 CheckBox (autopostback=true) ,但是 CheckBox 没有 CommandName 属性,因此也就无法在 GridView.RowCommand  事件中处理,并且如何获取当前 GridView 行信息呢?

我们可以选择像处理页面上普通 CheckBox 的 CheckedChanged 事件的“最原始”的方式。

要点:
1. 注意到 .net 中控件事件委托统一原型 (object sender,  EventArgs e)
第一个参数 sender,表示触发此事件控件

2. ASP.NET  服务器控件基类 Control 中定义了属性 NamingContainer ,表示对服务器控件的命名容器的引用,此引用创建唯一的命名空间。在模板列中的控件就是表示包含此控件的模板列。
注:也可以使用 BindingContainer 属性获取,但 MSDN 不建议使用此属性,
         此属性主要用于控件开发以及 ASP.NET 引擎内部使用 

        
下面以 DataGrid/GridView 模板列中使用 CheckBox/DropDownList 为例:

 .aspx
ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList<asp:TemplateField>
ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList                    
<ItemTemplate>                        
ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList                    
<asp:CheckBox ID="chkItem" runat="server" OnCheckedChanged="chkItem_CheckedChanged" AutoPostBack="true" />
ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList                    
<asp:DropDownList ID="drpItem" runat="server" OnSelectedIndexChanged="drpItem_SelectedIndexChanged" AutoPostBack="true">
ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList                        
<asp:ListItem></asp:ListItem>
ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList                        
<asp:ListItem Value="red">red</asp:ListItem>
ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList                        
<asp:ListItem Value="green">green</asp:ListItem>
ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList                        
<asp:ListItem Value="orange">orange</asp:ListItem>
ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList                    
</asp:DropDownList>
ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList                    
</ItemTemplate>
ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList                
</asp:TemplateField>

 .aspx.cs
ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownListprotected void chkItem_CheckedChanged(object sender, EventArgs e)
    }

ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownListprotected void chkItem2_CheckedChanged(object sender, EventArgs e)
    }

ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList

说明
1。事实上,对于 Button LinkButton ImageButton 当然也可以采取此中方式,只是我们习惯了 ItemCommand 事件中处理
2。对于 DataList/Repeater  只要转换外对应的模板项类型,DataListItem/RepeaterItem

下载

相关文章:

  • 2021-09-14
  • 2021-10-03
  • 2021-10-30
  • 2021-08-04
  • 2021-10-29
  • 2022-12-23
  • 2021-10-24
猜你喜欢
  • 2022-12-23
  • 2021-05-19
  • 2022-12-23
  • 2021-07-12
  • 2021-09-07
  • 2021-08-07
相关资源
相似解决方案