datagrid自编的分页代码。。
时间:2010-12-27 来源:danball
DataTable dt = DataAccess.DataWpxx.Get_Wpxx("", lbdm, xlid, "1", rb_zt.Text.Trim());
dg_user.DataSource = dt;
//从这里开始
// iii页数 jj索引 myjls每页记录数 jls记录数 ys页数 zys总页数 zdys转到页数
int myjls = 10;
if (tb_jls.Text.Trim() == "")
tb_jls.Text = myjls.ToString().Trim();
else
{
try
{
int.Parse(tb_jls.Text.Trim());
}
catch (Exception)
{
tb_jls.Text = myjls.ToString().Trim();
}
myjls = int.Parse(tb_jls.Text.Trim());
}
dg_user.PageSize = myjls;
//取总页数
int zjls = dt.Rows.Count;
lbl_sps.Text = zjls.ToString().Trim();
double zys = 0;
if (zjls % myjls != 0)
zys = zjls / myjls + 1;
else
zys = zjls / myjls;
lbl_zys.Text = zys.ToString().Trim();
//转到赋值
//取当前页数
int iii = 0;
int jj = 0;
if (ys == "0")
{
iii = 1;
jj = 0;
}
else
{
iii = int.Parse(ys);
jj = int.Parse(ys) - 1;
}
lbl_ys.Text = iii.ToString().Trim();
dd_zdys.Items.Clear();
for (int iiii = 1; iiii <= zys; iiii++) //之前写成iii了
{
ListItem it = new ListItem(iiii.ToString().Trim(), iiii.ToString().Trim());
dd_zdys.Items.Insert(iiii - 1, it);
}
if (dd_zdys.Items.Count == 0)
dd_zdys.Items.Insert(0, "1");
dd_zdys.SelectedIndex = jj;
if (zjls == 0)
{
lbn_f.Enabled = false;
lbn_p.Enabled = false;
lbn_n.Enabled = false;
lbn_l.Enabled = false;
}
else
{
//按钮是否显示
if (iii == 1)
{
lbn_f.Enabled = false;
lbn_p.Enabled = false;
lbn_n.Enabled = false;
lbn_l.Enabled = false;
}
else
{
lbn_f.Enabled = true;
lbn_p.Enabled = true;
}
if (iii == zys)
{
lbn_n.Enabled = false;
lbn_l.Enabled = false;
}
else
{
lbn_n.Enabled = true;
lbn_l.Enabled = true;
}
}
PagedDataSource Pds = new PagedDataSource(); //分页的对象
Pds.DataSource = dt.DefaultView;
Pds.AllowPaging = true;
Pds.PageSize = myjls;
Pds.CurrentPageIndex = jj;
dg_user.DataSource = Pds;
dg_user.DataBind();
}
#endregion
#region Imb_go_Click
protected void Imb_go_Click(object sender, ImageClickEventArgs e)
{
if (tb_jls.Text == "")
tb_jls.Text = "10";
try
{
int.Parse(tb_jls.Text.Trim());
}
catch (Exception)
{
tb_jls.Text = "10";
}
Bind_Wpxx("0", dd_dl.SelectedItem.Value, dd_xl.SelectedItem.Value);
}
#endregion
#region lbn_f_Click 首页
protected void lbn_f_Click(object sender, System.EventArgs e)
{
Bind_Wpxx("0", dd_dl.SelectedItem.Value, dd_xl.SelectedItem.Value);
}
#endregion
#region lbn_p_Click 上一页
protected void lbn_p_Click(object sender, System.EventArgs e)
{
int i = int.Parse(lbl_ys.Text.Trim()) - 1;
Bind_Wpxx(i.ToString().Trim(), dd_dl.SelectedItem.Value, dd_xl.SelectedItem.Value);
}
#endregion
#region lbn_n_Click 下一页
protected void lbn_n_Click(object sender, System.EventArgs e)
{
int i = int.Parse(lbl_ys.Text.Trim()) + 1;
Bind_Wpxx(i.ToString().Trim(), dd_dl.SelectedItem.Value, dd_xl.SelectedItem.Value);
}
#endregion
#region lbn_l_Click 最后一页
protected void lbn_l_Click(object sender, System.EventArgs e)
{
int i = int.Parse(lbl_zys.Text.Trim());
Bind_Wpxx(i.ToString().Trim(), dd_dl.SelectedItem.Value, dd_xl.SelectedItem.Value);
}
#endregion
#region dd_zdys_SelectedIndexChanged 转到
protected void dd_zdys_SelectedIndexChanged(object sender, System.EventArgs e)
{
Bind_Wpxx(dd_zdys.SelectedItem.Value, dd_dl.SelectedItem.Value, dd_xl.SelectedItem.Value);
}
#endregion
前端分页部分代码:
<table cellspacing="0" cellpadding="0" width="800" border="0" align="center">
<tr valign="middle">
<td width="20">
【共
</td>
<td align="center" width="30">
<font color="red">
<asp:Label ID="lbl_sps" runat="server"></asp:Label></font>
</td>
<td width="80">
行记录】
</td>
<td align="right" width="30">
每页
</td>
<td width="40">
<asp:TextBox ID="tb_jls" runat="server" Width="40px" MaxLength="4">10</asp:TextBox>
</td>
<td width="10">
行
</td>
<td align="left" width="60">
<asp:ImageButton ID="Imb_go" BorderStyle="None" ToolTip="执行查询" runat="server"
onclick="Imb_go_Click"></asp:ImageButton>
</td>
<td valign="middle" align="right" width="60">
【第
</td>
<td align="center" width="25">
<asp:Label ID="lbl_ys" runat="server" ForeColor="Red"></asp:Label>
</td>
<td align="left" width="30">
页】
</td>
<td align="right" width="40">
【共
</td>
<td align="center" width="25">
<asp:Label ID="lbl_zys" runat="server" ForeColor="Red"></asp:Label>
</td>
<td align="left" width="80">
页】
</td>
<td valign="middle" align="right" width="50">
<asp:LinkButton ID="lbn_f" runat="server" ForeColor="Blue"
onclick="lbn_f_Click">首页</asp:LinkButton>
</td>
<td align="center" width="50">
<asp:LinkButton ID="lbn_p" runat="server" ForeColor="Blue"
onclick="lbn_p_Click">上一页</asp:LinkButton>
</td>
<td align="center" width="50">
<asp:LinkButton ID="lbn_n" runat="server" ForeColor="Blue"
onclick="lbn_n_Click">下一页</asp:LinkButton>
</td>
<td align="left" width="50">
<asp:LinkButton ID="lbn_l" runat="server" ForeColor="Blue"
onclick="lbn_l_Click">末页</asp:LinkButton>
</td>
<td align="right" width="100">
转到
</td>
<td width="50">
<asp:DropDownList ID="dd_zdys" runat="server" AutoPostBack="True"
onselectedindexchanged="dd_zdys_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td width="20">
页
</td>
</tr>
</table>