Use the auto-resend
Sometimes you want to use the automatically resending CheckBox (autopostback = true) in the GridView template, but the CheckBox does not have the CommandName attribute, so it cannot be in the GridView. rowCommand event processing, and how to obtain the current row information of the GridView?
We can select the "most primitive" method for handling the CheckedChanged event of the normal CheckBox on the page.
Key points:
1. noticed that the unified prototype (object sender, EventArgs e) of control event Delegate in. net)
The first parameter sender indicates that the event control is triggered.
2. The NamingContainer attribute is defined in the basic Control class of the ASP. NET Server Control to reference the name container of the server Control. This reference creates a unique namespace. The control in the template column indicates the template column that contains the control.
Note: You can also use the BindingContainer attribute, but MSDN does not recommend this attribute,
This attribute is mainly used for control development and internal use of the ASP. NET engine.
The following uses CheckBox/DropDownList in the DataGrid/GridView template column as an example:
. 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; // the CheckBox that triggers the event
GridViewRow row = chk. NamingContainer as GridViewRow; // current row of the GridView
// You can also use the BindingContainer attribute, but MSDN does not recommend this attribute,
// This attribute is mainly used for control development and internal use of the ASP. NET engine.
// GridViewRow row = chk. BindingContainer as GridViewRow;
Row. BackColor = chk. Checked? System. Drawing. Color. bluevilet: System. Drawing. Color. White;
Response. Write (String. Format ("select row {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; // The DropDownList that triggers the event
GridViewRow row = drp. NamingContainer as GridViewRow; // current row of the GridView
Row. Style. Add (HtmlTextWriterStyle. BackgroundColor, drp. SelectedValue );
Response. Write (String. Format ("select row {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; // the CheckBox that triggers the event
DataGridItem row = chk. NamingContainer as DataGridItem;
Row. BackColor = chk. Checked? System. Drawing. Color. bluevilet: System. Drawing. Color. White;
Response. Write (String. Format ("select row {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; // The DropDownList that triggers the event
DataGridItem row = drp. NamingContainer as DataGridItem;
Row. Style. Add (HtmlTextWriterStyle. BackgroundColor, drp. SelectedValue );
Response. Write (String. Format ("select row {0}", row. ItemIndex + 1 ));
// More codes
// Label lbl = row. FindControl ("MyLabelID") as Label;
// String str = row. Cells [0]. Text;
//
}
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/haoliumeihua/archive/2009/04/10/4063610.aspx