Checkbox/dropdownlist using automatic postbacks in Gridview/datagrid template columns
Source: Internet
Author: User
Points:
1. Note the unified prototype of the control event delegate in. NET (object sender, EventArgs e)
The first parameter, sender, indicates that the event control is triggered
2. The ASP.net server control, which has a unique namespace, is defined as a reference to the naming container of the server control, which is a property namingcontainer. A control in a template column is a template column that contains this control.
Note: You can also use the Bindingcontainer property to get it, but MSDN does not recommend using this property.
This property is primarily used for control development and internal use of the ASP.net engine
The following is an example of using checkbox/dropdownlist in the Datagrid/gridview template column:
. aspx
< Asp:templatefield >
< ItemTemplate >
< Asp:checkbox ID = "Chkitem" runat = "server" OnCheckedChanged = "chkitem_checkedchanged" AutoPostBack = "true"/> ;
< asp:dropdownlist ID = "Drpitem" runat = "server" onselectedindexchanged = "drpitem_selectedindexchanged" Autopostbac K = "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 that triggers the event
GridViewRow row = chk. NamingContainer as GridViewRow; When the GridView is moving forward
You can also use the Bindingcontainer property to get it, but MSDN does not recommend using this property.
This property is primarily 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.BlueViolet:System.Drawing.Color.White;
Response.Write (String.Format ("select {0} row", 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; When the GridView is moving forward
Row. Style.add (Htmltextwriterstyle.backgroundcolor, DRP. SelectedValue);
Response.Write (String.Format ("select {0} row", 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 that triggers the event
DataGridItem row = chk. NamingContainer as DataGridItem;
Row. BackColor = Chk. Checked? System.Drawing.Color.BlueViolet:System.Drawing.Color.White;
Response.Write (String.Format ("select {0} row", row.) ItemIndex + 1));
More Codes
Label lbl = row. FindControl ("Mylabelid") as Label;
String str = row. Cells[0]. Text;
//
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.