欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net Checbox在GridView中的应用实例分析

程序员文章站 2023-11-27 09:37:04
本文实例讲述了asp.net checbox在gridview中的应用方法。分享给大家供大家参考,具体如下: 先看效果图: 前台代码:样式自己定义哦! &...

本文实例讲述了asp.net checbox在gridview中的应用方法。分享给大家供大家参考,具体如下:

先看效果图:

asp.net Checbox在GridView中的应用实例分析

前台代码:样式自己定义哦!

<div style="text-align: center; width: 80%">
   <yxy:hkrgridview id="tabgridview1" runat="server" autogeneratecolumns="false" width="100%"
     onrowdatabound="hkrgridview1_rowdatabound" onrowcreated="hkrgridview1_rowcreated"
     showheader="true" allowpaging="false" datakeynames="keyid" showfooter="false">
     <columns>
       <asp:boundfield>
         <itemstyle horizontalalign="center" width="5%" />
       </asp:boundfield>
       <asp:templatefield>
       <itemtemplate>
       <a href='<%#databinder.eval(container.dataitem,"newname")%>' ><%#databinder.eval(container.dataitem,"oldname")%></a>
       </itemtemplate>
       </asp:templatefield>
     <asp:boundfield datafield="memo">
         <itemstyle width="20%" horizontalalign="left" />
       </asp:boundfield>
       <asp:templatefield>
         <itemtemplate>
           <asp:checkbox id="checkbox" runat="server" />
         </itemtemplate>
         <itemstyle horizontalalign="center" width="5%" />
       </asp:templatefield>
     </columns>
     <footerstyle forecolor="red" horizontalalign="center" font-bold="true" />
   </yxy:hkrgridview>
   <div id="divone" style="text-align: right">
     <asp:checkbox id="checkboxall" runat="server" text="全选" width="80px" autopostback="true"
       oncheckedchanged="checkboxall_checkedchanged" />
     <asp:checkbox id="checkbox1" runat="server" text="反选" width="80px" autopostback="true"
       oncheckedchanged="checkbox1_checkedchanged" />
     <asp:button id="button1" runat="server" text="取 消" cssclass="button" onclick="button1_click" />
   </div>
</div>

后台代码:

protected void hkrgridview1_rowcreated(object sender, gridviewroweventargs e)
{
    if (e.row.rowtype == datacontrolrowtype.header)
    {
      tablecellcollection tcheader = e.row.cells;
      tcheader.clear();
      //第一行表头
      tcheader.add(new tableheadercell());
      tcheader[0].text = "序号<br>no.";
      tcheader.add(new tableheadercell());
      tcheader[1].text = "附件名称";
      tcheader.add(new tableheadercell());
      tcheader[2].text = "备注";
      tcheader.add(new tableheadercell());
      tcheader[3].text = "选择</tr>";
    }
}
protected void hkrgridview1_rowdatabound(object sender, gridviewroweventargs e)
{
    gridviewrow gr = e.row;
    if (e.row.rowindex != -1)
    {
      e.row.cells[0].text = (e.row.rowindex + 1).tostring();
    }
}
private void hkrgridviewbind(){
    string strsql = "select keyid,newname, oldname, localhome,memo from aq_file_up where (relaid = @relaid) and (fjtype = @fjtype) and (usestatus = 1)";
    sqlparameter[] sps = {
              new sqlparameter("@relaid",this.hdnrelaid.value),
              new sqlparameter("@fjtype",this.hdnfjtype.value)
               };
    datatable dt = pms.sqlhelper.executedatatable(commandtype.text, strsql, sps);
    tabgridview1.datasource = dt;
    tabgridview1.databind();
}
rotected void button1_click(object sender, eventargs e)
{
    checkboxall.checked = false;
    checkbox1.checked = false;
    for (int i = 0; i <= tabgridview1.rows.count - 1; i++)
    {
      checkbox cb = (checkbox)tabgridview1.rows[i].findcontrol("checkbox");
      cb.checked = false;
    }
}
protected void checkbox1_checkedchanged(object sender, eventargs e)
{
   for (int i = 0; i <= tabgridview1.rows.count - 1; i++)
    {
      checkbox cb = (checkbox)tabgridview1.rows[i].findcontrol("checkbox");
      if (cb.checked == false)
      {
        cb.checked = true;
      }
      else
      {
        cb.checked = false;
      }
    }
    //checkbox1.checked =true;
    checkboxall.checked = false;
}
protected void checkboxall_checkedchanged(object sender, eventargs e)
{
   for (int i = 0; i <= tabgridview1.rows.count - 1; i++)
    {
      checkbox cb = (checkbox)tabgridview1.rows[i].findcontrol("checkbox");
      if (checkboxall.checked == true)
      {
        cb.checked = true;
      }
      else
      {
        cb.checked = false;
      }
    }
    checkbox1.checked = false;
}

更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作xml技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

希望本文所述对大家asp.net程序设计有所帮助。