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事件触发,进行相关操作

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值