竹木人

----------------隐感存理,去伪存真,做个安分的守候者

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

小项目要用到多选,修改相关数据记录:

效果如下:

具体实现如下:

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

 

 

 

posted on 2012-05-17 17:45  竹木人  阅读(7163)  评论(0编辑  收藏  举报