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


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

效果如下:

具體實現如下:

用到了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事件觸發,進行相關操作

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM