Application instance analysis of asp.net Checbox in the GridView, checboxgridview
This article describes the application of asp.net Checbox in the GridView. We will share this with you for your reference. The details are as follows:
First look:
Front-end code: Customize the style!
<Div style = "text-align: center; width: 80%"> <yxy: hkrGridView ID = "tabGridView1" runat = "server" AutoGenerateColumns = "False" Width = "100%" OnRowDataBound = "inline" OnRowCreated = "inline" ShowHeader = "True" AllowPaging = "false "DataKeyNames =" keyId "ShowFooter =" false "> <Columns> <asp: boundField> <ItemStyle HorizontalAlign = "Center" Width = "5%"/> </asp: BoundField> <asp: TemplateField> <ItemTemplate> <a href = '<% # DataBinder. eval (Container. dataItem, "newName") %> '> <% # DataBinder. eval (Container. dataItem, "oldName") %> </a> </ItemTemplate> </asp: TemplateField> <asp: boundField DataField = "memo"> <ItemStyle Width = "20%" HorizontalAlign = "left"/> </asp: BoundField> <asp: TemplateField> <ItemTemplate> <asp: checkBox ID = "CheckBox" runat = "server"/> </ItemTemplate> <ItemStyle HorizontalAlign = "Center" Width = "5%"/> </asp: templateField> </Columns> <FooterStyle ForeColor = "Red" HorizontalAlign = "Center" Font-Bold = "true"/> </yxy: hkrGridView> <div id = "DivOne" style = "text-align: right"> <asp: checkBox ID = "CheckBoxAll" runat = "server" Text = "select all" Width = "80px" AutoPostBack = "True" OnCheckedChanged = "CheckBoxAll_CheckedChanged"/> <asp: checkBox ID = "CheckBox1" runat = "server" Text = "invert" Width = "80px" AutoPostBack = "True" OnCheckedChanged = "checkbox#checkedchanged"/> <asp: button ID = "Button1" runat = "server" Text = "Remove" CssClass = "Button" OnClick = "button#click"/> </div>
Background code:
Protected void HkrGridView1_RowCreated (object sender, GridViewRowEventArgs e) {if (e. row. rowType = DataControlRowType. header) {TableCellCollection tcHeader = e. row. cells; tcHeader. clear (); // the header tcHeader of the first line. add (new TableHeaderCell (); tcHeader [0]. text = "No. <BR> No. "; tcHeader. add (new TableHeaderCell (); tcHeader [1]. text = "attachment name"; tcHeader. add (new TableHeaderCell (); tcHeader [2]. text = "Remarks"; tcHeade R. add (new TableHeaderCell (); tcHeader [3]. text = "select </tr>" ;}} protected void HkrGridView1_RowDataBound (object sender, GridViewRowEventArgs e) {GridViewRow gr = e. row; if (e. row. rowIndex! =-1) {e. row. cells [0]. text = (e. row. rowIndex + 1 ). toString () ;}} private void HkrGridViewBind () {string strSql = "SELECT keyId, newName, oldName, localHome, memo FROM aq_file_up WHERE (relaid = @ relaid) AND (fjType = @ fjType) AND (usestatus = 1) "; SqlParameter [] sps = {new SqlParameter (" @ relaid ", this. hdnRelaId. value), new SqlParameter ("@ fjType", this. hdnFjType. value)}; DataTable dt = pms. sqlHelper. executeDataTable (CommandType. text, strSql, sps); tabGridView1.DataSource = dt; tabGridView1.DataBind ();} rotected void button#click (object sender, EventArgs e) {CheckBoxAll. checked = false; CheckBox1.Checked = false; for (int I = 0; I <= tabGridView1.Rows. count-1; I ++) {CheckBox cb = (CheckBox) tabGridView1.Rows [I]. findControl ("CheckBox"); cb. checked = false ;}} protected void CheckBox1_CheckedChanged (object sender, EventArgs e) {for (int I = 0; I <= tabGridView1.Rows. count-1; I ++) {CheckBox cb = (CheckBox) tabGridView1.Rows [I]. findControl ("CheckBox"); if (cb. checked = false) {cb. checked = true;} else {cb. checked = false ;}/// CheckBox1.Checked = true; CheckBoxAll. checked = false;} protected void CheckBoxAll_CheckedChanged (object sender, EventArgs e) {for (int I = 0; I <= tabGridView1.Rows. count-1; I ++) {CheckBox cb = (CheckBox) tabGridView1.Rows [I]. findControl ("CheckBox"); if (CheckBoxAll. checked = true) {cb. checked = true;} else {cb. checked = false ;}} CheckBox1.Checked = false ;}