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