本文实例讲述了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程序设计有所帮助。

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。