In the Asp.net gridview control, the template column checkbox is fully selected, deselected, and canceled.

Source: Internet
Author: User

CopyCode The Code is as follows: using system;
Using system. Data;
Using system. Data. sqlclient;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Public partial class demo18: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (page. ispostback = false)
{
Binddata ();
}
}

Public void binddata ()
{
String strsql = "select userid, c_name, e_name, updatadate, isdy from demo_user ";
Datatable dt = sqlhelper. executedataset (sqlhelper. conn_string, commandtype. Text, strsql, null). Tables [0];

Gridview. datasource = DT;
Gridview. datakeynames = new string [] {"userid"}; // primary key
Gridview. databind ();
}

Protected void gridview_pageindexchanging (Object sender, gridviewpageeventargs E)
{
Gridview. pageindex = E. newpageindex;
Binddata ();
}

Protected void button#click (Object sender, eventargs E)
{
Checkboxall. Checked = false;
Checkbox1.checked = false;
For (INT I = 0; I <= gridview. Rows. Count-1; I ++)
{
Checkbox = (checkbox) gridview. Rows [I]. findcontrol ("checkbox ");
Checkbox. Checked = false;
}

}

Protected void button2_click (Object sender, eventargs E)
{
For (INT I = 0; I <= gridview. Rows. Count-1; I ++)
{
Checkbox = (checkbox) gridview. Rows [I]. findcontrol ("checkbox ");
If (checkbox. Checked = true)
{
String strsql = "Update demo_user set updatadate = @ updatadate where userid = @ userid ";
Sqlparameter [] para = {
New sqlparameter ("@ updatadate", datetime. Now ),
New sqlparameter ("@ userid", gridview. datakeys [I]. value ),
};
Sqlhelper. executenonquery (sqlhelper. conn_string, commandtype. Text, strsql, para );
}
}
Checkboxall. Checked = false;
Checkbox1.checked = false;
Binddata ();

}

Protected void checkboxall_checkedchanged (Object sender, eventargs E)
{
For (INT I = 0; I <= gridview. Rows. Count-1; I ++)
{
Checkbox = (checkbox) gridview. Rows [I]. findcontrol ("checkbox ");
If (checkboxall. Checked = true)
{
Checkbox. Checked = true;
}
Else
{
Checkbox. Checked = false;
}
}
Checkbox1.checked = false;

}

Protected void checkbox#checkedchanged (Object sender, eventargs E)
{
For (INT I = 0; I <= gridview. Rows. Count-1; I ++)
{
Checkbox = (checkbox) gridview. Rows [I]. findcontrol ("checkbox ");
If (checkbox. Checked = false)
{
Checkbox. Checked = true;
}
Else
{
Checkbox. Checked = false;
}
}
Checkboxall. Checked = false;
}
}

<Table align = "center" bgcolor = "# c0de98" border = "0" cellpadding = "0" cellspacing = "1" width = "99%">
<Tr>
<TH colspan = "2">
Gridview demonstration </Th>
</Tr>
<Tr>
<TD colspan = "2" style = "width: 100%;">
<Asp: gridview id = "gridview" runat = "server" width = "100%" autogeneratecolumns = "false" allowpaging = "true" onpageindexchanging = "gridview_pageindexchanging" pagesize = "12">
<Columns>
<Asp: templatefield headertext = "select">
<Itemtemplate>
<Asp: checkbox id = "checkbox" runat = "server"/>
</Itemtemplate>
</ASP: templatefield>
<Asp: boundfield datafield = "userid" headertext = "userid" readonly = "true"/>
<Asp: boundfield datafield = "c_name" headertext = "Chinese name" readonly = "true"/>
<Asp: boundfield datafield = "e_name" headertext = "English name" readonly = "true"/>
<Asp: boundfield datafield = "updatadate" headertext = "Update Time"/>
</Columns>
<Rowstyle horizontalalign = "center"/>
<Pagerstyle horizontalalign = "right"/>
</ASP: gridview>
</TD>
</Tr>

<Tr>
<TD>
<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"/>
<Asp: button id = "button2" runat = "server" text = "Update Time" cssclass = "button" onclick = "button2_click"/> </TD>
</Tr>
</Table>

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.