ASP.NET的Gridview控件简单使用(分页、序号、选框、全选)(附源码)

例子图

这次我们实现的功能是:全选,单选,可以提取一行的数据,分页,结合下拉列表筛选数据,显示序号。


1.全选
//全选
protected void CheckBox2_CheckedChanged1(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl(“CheckBox1”);
if (CheckBox2.Checked == true)
{
cbox.Checked = true;
}
else
{
cbox.Checked = false;
}
}
}
当点击全选框的时候,其它框也会被选中
注:CheckBox的AutoPostBack属性 要 设置 为 True


2.单选提取一行中的数据
//提交按钮
protected void Button1_Click1(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl(“CheckBox1”);
if (cbox.Checked == true)
{
string userid = GridView1.DataKeys[i].Value.ToString();
DB db = new DB();
string sqlstr = “select username from base_user where userid = ‘” + userid + “’ and auditcode!=’3’ and auditcode!=’0’”;
int cot = db.reDt(sqlstr).Rows.Count;
if (cot > 0)
{
string name = db.reDt(sqlstr).Rows[0][“username”].ToString();
DataInterface di = new DataInterface();
string ss = di.synResInfo(“11201”, “440000”, userid, name);
//Response.Write(ss);
string time = DateTime.Now.ToLongDateString().ToString();
string Respondlog = “log\” + time + “Respond.xml”;
}
//Response.Redirect(Respondlog);
}
}
int cp = Convert.ToInt32(DropDownList1.Text);
this.bind(cp);
}


3.显示序号

< asp:GridView ID=”GridView1” runat=”server” AutoGenerateColumns=”False” style=” width:100%;”>
< Columns>
< asp:TemplateField HeaderStyle-Width=”10%”>
< ItemTemplate>
<%# (Container.DataItemIndex + 1) + (Convert.ToInt32(this.Label6.Text) - 1) * (Convert.ToInt32(DropDownList1.Text))%>
< /ItemTemplate>
< /asp:TemplateField>
< asp:TemplateField>
< ItemTemplate>
< asp:CheckBox ID=”CheckBox1” runat=”server” />
< /ItemTemplate>
< /asp:TemplateField>
< /Columns>
< SelectedRowStyle BackColor=”#669999” Font-Bold=”True” ForeColor=”White” />
< /asp:GridView>

解释:<%# (Container.DataItemIndex + 1) + (Convert.ToInt32(this.Label6.Text) - 1) * (Convert.ToInt32(DropDownList1.Text))%>

Container.DataItemIndex + 1 :为当页的数量
this.Label6.Text:总页数
DropDownList1.Text:每页显示数


4.前端源码

< form id="form1" runat="server" method="post" action="">
<center>
    <div id="topNav">
        <table style="width: 1000px; height: 36px; border-right-width: 2px; border-left-width: 2px;
            border-bottom-width: 2px; border-top-width: 2px; margin-bottom: 15px; margin-top: 10px;">
            <tr>
                <td style="width: 10px; padding-top: 5px;">
                </td>
                <td style="width: 100px; padding-top: 5px;">
                    <div class="button-group">
                        <asp:Button ID="Button1" class="button bg-main" runat="server" Text="提交上报" OnClick="Button1_Click1" />
                    </div>
                </td>
                <td>
                <div class="button-group">
                        <asp:Button ID="Button2" class="button bg-main" runat="server" Text="退回信息" OnClick="Button2_Click1" />
                    </div>
                </td>
                <td>
                    <asp:CheckBox ID="CheckBox2" runat="server" Font-Size="9pt" Text="全选" AutoPostBack="True"
                        OnCheckedChanged="CheckBox2_CheckedChanged1" />&nbsp;&nbsp;&nbsp;&nbsp;学员信息状态:<asp:DropDownList
                            ID="sh_status" runat="server" AutoPostBack="True" OnSelectedIndexChanged="sh_status_SelectedIndexChanged">
                            <asp:ListItem Value="1">全部</asp:ListItem>
                            <asp:ListItem Value="2">等待上报</asp:ListItem>
                            <asp:ListItem Value="3">成功上报</asp:ListItem>
                            <asp:ListItem Value="4">上报失败</asp:ListItem>
                            <asp:ListItem Value="5">学员修改</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                <td style="width: 350px; padding-top: 5px;">
                    &nbsp;
                </td>
            </tr>
        </table>
    </div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" style=" width:100%;">
        <Columns>
            <asp:TemplateField HeaderStyle-Width="10%">
                <ItemTemplate>
                    <%# (Container.DataItemIndex + 1) + (Convert.ToInt32(this.Label6.Text) - 1) * (Convert.ToInt32(DropDownList1.Text))%>
                </ItemTemplate>
                <HeaderStyle Width="50px" Height="20px" HorizontalAlign="Center" 
                    VerticalAlign="Middle"></HeaderStyle>
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </ItemTemplate>
                <HeaderStyle Width="50px" />
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            </asp:TemplateField>
            <asp:BoundField DataField="username" HeaderText="姓名">
                <HeaderStyle Width="100px" Height="20px" HorizontalAlign="Center" 
                VerticalAlign="Middle" />
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            </asp:BoundField>
            <asp:BoundField DataField="userid" HeaderText="身份证件号">
                <FooterStyle Width="200px" />
            <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            </asp:BoundField>
            <asp:BoundField DataField="auditcode" HeaderText="学员信息状态">
                <HeaderStyle Width="150px" HorizontalAlign="Center" 
                VerticalAlign="Middle" />
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            </asp:BoundField>
            <asp:BoundField DataField="reported_status" HeaderText="备注">
                <HeaderStyle Width="350px" HorizontalAlign="Center" 
                VerticalAlign="Middle" />
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            </asp:BoundField>
        </Columns>
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
    </asp:GridView>
    <div id="bottomNav">
        <table style="width: 1000px; height: 36px; border-right-width: 2px; border-left-width: 2px;
            border-bottom-width: 2px; border-top-width: 2px; margin-bottom: 0px; margin-top: 15px;">
            <tr>
                <td style="width: 400px; padding-top: 5px;">
                    &nbsp;&nbsp;&nbsp;&nbsp;<asp:LinkButton ID="lnkbtnOne" runat="server" OnClick="lnkbtnOne_Click">首页</asp:LinkButton>&nbsp;&nbsp;
                    <asp:LinkButton ID="lnkbtnUp" runat="server" OnClick="lnkbtnUp_Click">上一页</asp:LinkButton>&nbsp;&nbsp;
                    第<asp:Label ID="Label6" runat="server" Text="1"></asp:Label>页&nbsp;&nbsp;共<asp:Label
                        ID="Label7" runat="server" Text="1"></asp:Label>页&nbsp;&nbsp;
                    <asp:LinkButton ID="lnkbtnNext" runat="server" OnClick="lnkbtnNext_Click">下一页</asp:LinkButton>&nbsp;&nbsp;
                    <asp:LinkButton ID="lnkbtnBack" runat="server" OnClick="lnkbtnBack_Click">尾页</asp:LinkButton>
                </td>
                <td style="width: 400px; padding-top: 5px;">
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    共<asp:Label ID="Lall" runat="server" Text=""></asp:Label>条, 每页<asp:DropDownList ID="DropDownList1"
                        runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Width="50px"
                        AutoPostBack="True">
                        <asp:ListItem Value="50">50</asp:ListItem>
                        <asp:ListItem Value="100">100</asp:ListItem>
                        <asp:ListItem Value="200">200</asp:ListItem>
                        <asp:ListItem Value="300">300</asp:ListItem>
                        <asp:ListItem Value="500">500</asp:ListItem>
                        <asp:ListItem Value="1000">1000</asp:ListItem>
                    </asp:DropDownList>
                    条
                </td>
            </tr>
        </table>
    </div>
</center>
<br>
<br>
</form>

注:这里只贴出from里面的代码


5.后台cs文件源码

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    { bind(Convert.ToInt32(DropDownList1.Text)); }
}

public void bind(int cp)
{

    //上报状态
    string auditcode = sh_status.Text;
    string ac = "";
    if (auditcode.Equals("1"))
    {
        ac = "auditcode != '0' and auditcode!='1' or dateupload='2'";
    }
    else if (auditcode.Equals("2"))
    {
        ac = "auditcode = '2'";
    }
    else if (auditcode.Equals("3"))
    {
        ac = "auditcode = '3'";
    }
    else if (auditcode.Equals("4"))
    {
        ac = "auditcode = '4'";
    }
    else if (auditcode.Equals("5"))
    {
        ac = "auditcode!='4' and dateupload='2'";
    }

    SqlConnection con = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=guipei;Persist Security Info=True;User ID=zh;Password=sa");
    SqlDataAdapter da = new SqlDataAdapter("select username,userid,reported_status,(case auditcode when '2' then '等待上报' when '3' then '成功上报' when '4' then '上报失败!' when '0' then '学员修改中' when '1' then '学员修改中' end) as auditcode from base_user where " + ac, con);
    DataSet ds = new DataSet();
    da.Fill(ds, "base_user");
    /*PagedDataSource ps = new PagedDataSource();
    ps.DataSource = ds.Tables["base_user"].DefaultView;
    ps.AllowPaging = true;
    ps.PageSize = 2;
    ps.CurrentPageIndex = 0;*/

    /*this.DataList1.DataSource = ps;
    this.DataList1.DataKeyField = "SID";
    this.DataList1.DataBind();*/

    /*DataList1.DataSource =  ps;
     DataList1.DataKeyField = "userid";
     DataList1.DataBind();*/



    int curpage = Convert.ToInt32(this.Label6.Text);
    PagedDataSource ps = new PagedDataSource();
    ps.DataSource = ds.Tables["base_user"].DefaultView;
    int all = ps.Count;
    Lall.Text = all.ToString();
    ps.AllowPaging = true; //是否可以分页 
    ps.PageSize = cp; //显示的数量 
    ps.CurrentPageIndex = curpage - 1; //取得当前页的页码 
    this.lnkbtnUp.Enabled = true;
    this.lnkbtnNext.Enabled = true;
    this.lnkbtnBack.Enabled = true;
    this.lnkbtnOne.Enabled = true;
    if (curpage == 1)
    {
        this.lnkbtnOne.Enabled = false;//不显示第一页按钮 
        this.lnkbtnUp.Enabled = false;//不显示上一页按钮 
    }
    if (curpage == ps.PageCount)
    {
        this.lnkbtnNext.Enabled = false;//不显示下一页 
        this.lnkbtnBack.Enabled = false;//不显示最后一页 
    }
    this.Label7.Text = Convert.ToString(ps.PageCount);
    this.GridView1.DataSource = ps;
    this.GridView1.DataKeyNames = new string[] { "userid" };//.DataKeyField = "userid";
    this.GridView1.DataBind();


}
protected void lnkbtnOne_Click(object sender, EventArgs e)
{
    this.Label6.Text = "1";
    this.bind(Convert.ToInt32(DropDownList1.Text));
}
protected void lnkbtnUp_Click(object sender, EventArgs e)
{
    this.Label6.Text = Convert.ToString(Convert.ToInt32(this.Label6.Text) - 1);
    this.bind(Convert.ToInt32(DropDownList1.Text));
}
protected void lnkbtnNext_Click(object sender, EventArgs e)
{
    this.Label6.Text = Convert.ToString(Convert.ToInt32(this.Label6.Text) + 1);
    this.bind(Convert.ToInt32(DropDownList1.Text));
}
protected void lnkbtnBack_Click(object sender, EventArgs e)
{
    this.Label6.Text = this.Label7.Text;
    this.bind(Convert.ToInt32(DropDownList1.Text));
}
//分页结束


//每页显示多少条
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    Label6.Text = "1";
    int cp = Convert.ToInt32(DropDownList1.Text);
    this.bind(cp);
}
//选择显示的提交状态
protected void sh_status_SelectedIndexChanged(object sender, EventArgs e)
{
    Label6.Text = "1";
    int cp = Convert.ToInt32(DropDownList1.Text);
    this.bind(cp);
}

注:这里不再贴出上述的源码


  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值