五二脚本 手机端 m.52jb.net
特色栏目:PHP教程JavaScript正则表达式焦点幻灯片
页面导航:首页 > 编程语言 > ASP.NET > 正文内容

Checbox在GridView中的应用实例

来源:五二脚本发布时间:08-11浏览:633 次
这篇文章主要介绍了Checbox在GridView中的应用实例,结合实例形式分析了GridView中添加与使用Checbox的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

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

关于我们 - 联系我们 - 网站地图 - 网站声明

QQ群:153638182  投稿邮箱: 578640261@qq.com  皖ICP备-14005058

严禁网站镜像,否则追究法律责任。未经www.52jb.net 同意,不得转载本网站上所有的源代码。

五二脚本每日更新大量关于网页制作网页特效php编程网站优化源码下载的实例教程。