有時候希望在 GridView 模板中使用AutoPostBack的 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:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkItem" runat="server" OnCheckedChanged="chkItem_CheckedChanged" AutoPostBack="true" />
<asp:DropDownList ID="drpItem" runat="server" OnSelectedIndexChanged="drpItem_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="red">red</asp:ListItem>
<asp:ListItem Value="green">green</asp:ListItem>
<asp:ListItem Value="orange">orange</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
.aspx.cs
protected void chkItem_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = sender as CheckBox; // 觸發事件的 CheckBox
GridViewRow row = chk.NamingContainer as GridViewRow; // GridView 當前行
// 也可以使用 BindingContainer 屬性擷取,但 MSDN 不建議使用此屬性,
// 此屬性主要用於控制項開發以及 ASP.NET 引擎內部使用
// GridViewRow row = chk.BindingContainer as GridViewRow;
row.BackColor = chk.Checked ? System.Drawing.Color.BlueViolet : System.Drawing.Color.White;
Response.Write(String.Format("選中第 {0} 行", row.RowIndex + 1));
// more codes
// Label lbl = row.FindControl("MyLabelID") as Label;
// string str = row.Cells[0].Text;
//
}
protected void drpItem_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList drp = sender as DropDownList; // 觸發事件的 DropDownList
GridViewRow row = drp.NamingContainer as GridViewRow; // GridView 當前行
row.Style.Add(HtmlTextWriterStyle.BackgroundColor, drp.SelectedValue);
Response.Write(String.Format("選中第 {0} 行", row.RowIndex + 1));
// more codes
// Label lbl = row.FindControl("MyLabelID") as Label;
// string str = row.Cells[0].Text;
//
}
protected void chkItem2_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = sender as CheckBox; // 觸發事件的 CheckBox
DataGridItem row = chk.NamingContainer as DataGridItem;
row.BackColor = chk.Checked ? System.Drawing.Color.BlueViolet : System.Drawing.Color.White;
Response.Write(String.Format("選中第 {0} 行", row.ItemIndex + 1));
// more codes
// Label lbl = row.FindControl("MyLabelID") as Label;
// string str = row.Cells[0].Text;
//
}
protected void drpItem2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList drp = sender as DropDownList; // 觸發事件的 DropDownList
DataGridItem row = drp.NamingContainer as DataGridItem;
row.Style.Add(HtmlTextWriterStyle.BackgroundColor, drp.SelectedValue);
Response.Write(String.Format("選中第 {0} 行", row.ItemIndex + 1));
// more codes
// Label lbl = row.FindControl("MyLabelID") as Label;
// string str = row.Cells[0].Text;
//
}
說明
1。事實上,對於 Button LinkButton ImageButton 當然也可以採取此中方式,只是我們習慣了 ItemCommand 事件中處理
2。對於 DataList/Repeater 只要轉換外對應的模板項類型,DataListItem/RepeaterItem