ASP. NET perfectly solves multiple checkbox values and related operations

Source: Internet
Author: User

You need to select multiple items for a small project and modify the relevant data records:

The effect is as follows:

The specific implementation is as follows:

The gridview control is used.

Certificate ---------------------------------------------------------------------------------------------------------------------------------------

Gridview is a control that provides related database operations. msdn explains: click here

Certificate ---------------------------------------------------------------------------------------------------------------------------------------

The procedure is as follows:

A. Key aspx code:

 

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"                             BorderWidth="0px" Width="701px" ForeColor="Blue" Height="182px">                              <Columns>                                  <asp:TemplateField>                                      <ItemTemplate>                                          <asp:CheckBox ID="CheckBox1"  runat="server" />                                          <asp:HiddenField ID="HidID" runat="server" Value='<%# Bind("id") %> ' />                                           <asp:Label  ID="Label1" runat="server" style="font-size:12px;color:#3344ff"  Text='<%# Bind("content") %>'></asp:Label>                                      </ItemTemplate>                                  </asp:TemplateField>                              </Columns> </asp:GridView>

 

B. key code in the aspx Background: (triggered by a button)

// Bind a value to the gridview
Public void BIND ()
{
String SQL;
SQL = "select ID, content from Intro ";
Gridview1.datasource = access. greatds (SQL );
Gridview1.databind ();
}
Protected void button#click (Object sender, eventargs e) {int rowcount = gridview1.rows. count; string STR = configurationmanager. connectionstrings ["kuny"]. connectionstring; oledbconnection conn = new oledbconnection (STR); Conn. open (); int tag = 0; For (INT I = 0; I <rowcount; I ++) {checkbox tempchk = (checkbox) gridview1.rows [I]. findcontrol ("checkbox1"); hiddenfield hidid = (hiddenfield) gridview1. Rows [I]. findcontrol ("hidid"); If (tempchk. checked = true) {string update_exam_item = "Update baoming_info set bk_exerm_id" + hidid. value + "=" + "1" + "where bk_exer_id =" + session ["stuid"]; // alert (update_exam_item); oledbcommand cmd = new oledbcommand (update_exam_item, conn); cmd. executenonquery (); tag ++ ;}} if (TAG = 0) {alert ("You must select at least one test item! ") ;}Else {conn. Close (); response. Redirect (" step_1.aspx ");}}

 

Summary:

For (INT I = 0; I <rowcount; I ++ ){
       // Retrieve the checkbox and hide the text value through traversal.Checkbox tempchk = (checkbox) gridview1.rows [I]. findcontrol ("checkbox1"); hiddenfield hidid = (hiddenfield) gridview1.rows [I]. findcontrol ("hidid ");
       //If (tempchk. checked = true) {string update_exam_item = "Update baoming_info set bk_exerm_id" + hidid. value + "=" + "1" + "where bk_exer_id =" + session ["stuid"]; // alert (update_exam_item); oledbcommand cmd = new oledbcommand (update_exam_item, conn); cmd. executenonquery (); tag ++ ;}}
To sum up, use the gridview control first, bind the data source BIND () to it, add the corresponding checkbox in the properties templatefield and itemtemplate of the gridview, and then trigger it with the button event for related operations.

 

 

 

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.