ASP.NET 完美解決checkbox多選傳值以及相關操作

來源:互聯網
上載者:User

小項目要用到多選,修改相關資料記錄:

效果如下:

具體實現如下:

用到了GridView控制項。

---------------------------------------------------------------------------------------------------------------------------------------

GridView是一個提供相關資料庫操作的控制項,MSDN解釋:猛擊此處

---------------------------------------------------------------------------------------------------------------------------------------

具體操作如下:

a.aspx關鍵代碼:

 

<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.aspx後台關鍵代碼:(按鈕觸發)

//給GridView綁定數值
public void bind()
    {
        string sql;
        sql = "select id, content from intro";
        GridView1.DataSource = access.GreatDs(sql);
        GridView1.DataBind();
    }
protected void Button1_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("您至少需要選擇一項考試項目!"); } else { conn.Close(); Response.Redirect("step_1.aspx"); } }

 

總結:

for (int i = 0; i < rowCount; i++)        {
       //遍曆取checkbox以及隱藏text的值 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++; } }
總結下,先要用GridView控制項,並為它綁定資料來源bind(),在gridview的屬性TemplateField、ItemTemplate添加相應的checkbox等,再用button事件觸發,進行相關操作

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.