分页会写,但不知道怎么做成控件
控件客户端是:
<table border="0" cellpadding="3" cellspacing="3" width="100%">
<tr>
<td style="margin-left:auto; margin-right:auto;">
当前条目:<asp:Literal ID="lbtn_num" runat="server"></asp:Literal>
(总计条目:<asp:Literal ID="lbtn_sum" runat="server"></asp:Literal>)
</td>
</tr>
<tr>
<td>
<asp:Label ID="lb_page_on" runat="server" ForeColor="#66667f" Font-Bold="true" Text="Page:"></asp:Label>
<asp:Literal ID="l_current" runat="server"></asp:Literal> <asp:Repeater ID="rpt_number" runat="server">
<ItemTemplate>
<asp:LinkButton ID="lbtn_number" runat="server" Text='<%# "-"+Container.DataItem %>' OnClick="lbtn_num_Click"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
<asp:LinkButton ID="lbtn_first" runat="server" OnClick="lbtn_first_Click">[第一页]</asp:LinkButton>
<asp:LinkButton ID="lbtn_pre" runat="server" OnClick="lbtn_pre_Click" >[上一页]</asp:LinkButton>
<asp:LinkButton ID="lbtn_next" runat="server" OnClick="lbtn_next_Click">[下一页]</asp:LinkButton>
<asp:LinkButton ID="lbtn_end" runat="server" OnClick="lbtn_end_Click">[最后一页]</asp:LinkButton>
</td>
</tr>
</table>
源代码是:
public partial class ctrls_PageControl : System.Web.UI.UserControl
{
public static int pageSum, num;
protected void Page_Load(object sender, EventArgs e)
{
GetDataPage(1);
} private void GetDataPage(int pageIndex)
{
this.l_current.Text = pageIndex.ToString(); num = int.Parse(new News().GetTitle("S01").Rows[0][0].ToString()); //输入字符串的格式不正确 lbtn_sum.Text = num.ToString();//获得总条数 pageSum = num / 20 + 1;
BinPage(pageIndex); }
private void BinPage(int pageIndex)
{
List<int> numberList = new List<int>();
if (pageIndex < pageSum - 2)
{
for (int i = pageIndex + 1; i < pageIndex + 2; i++)
{
numberList.Add(i);
}
}
else
{
for (int i = pageIndex + 1; i <= pageSum; i++)
{
numberList.Add(i);
}
} this.rpt_number.DataSource = numberList;
this.rpt_number.DataBind(); if (pageIndex == 1)
{
lbtn_num.Text = "1 - 20";
}
else
{
if (20 * pageIndex > num)
{
lbtn_num.Text = 20 * (pageIndex - 1) + 1 + "-" + num;
}
else
{
lbtn_num.Text = 20 * (pageIndex - 1) + 1 + "-" + 20 * pageIndex;
}
}
} GetTitle()为根据A表中title得到B表中数据的存储过程:
public DataTable GetTitle(string cate_pkid)
{
SqlParameter[] parameters = new SqlParameter[] {
new SqlParameter("@Parent_ID", SqlDbType.VarChar, 10)};
parameters[0].Value = cate_pkid;
DataTable dt = FillDatatable("up_getTitleBYhead", parameters);
return dt;
}
控件客户端是:
<table border="0" cellpadding="3" cellspacing="3" width="100%">
<tr>
<td style="margin-left:auto; margin-right:auto;">
当前条目:<asp:Literal ID="lbtn_num" runat="server"></asp:Literal>
(总计条目:<asp:Literal ID="lbtn_sum" runat="server"></asp:Literal>)
</td>
</tr>
<tr>
<td>
<asp:Label ID="lb_page_on" runat="server" ForeColor="#66667f" Font-Bold="true" Text="Page:"></asp:Label>
<asp:Literal ID="l_current" runat="server"></asp:Literal> <asp:Repeater ID="rpt_number" runat="server">
<ItemTemplate>
<asp:LinkButton ID="lbtn_number" runat="server" Text='<%# "-"+Container.DataItem %>' OnClick="lbtn_num_Click"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
<asp:LinkButton ID="lbtn_first" runat="server" OnClick="lbtn_first_Click">[第一页]</asp:LinkButton>
<asp:LinkButton ID="lbtn_pre" runat="server" OnClick="lbtn_pre_Click" >[上一页]</asp:LinkButton>
<asp:LinkButton ID="lbtn_next" runat="server" OnClick="lbtn_next_Click">[下一页]</asp:LinkButton>
<asp:LinkButton ID="lbtn_end" runat="server" OnClick="lbtn_end_Click">[最后一页]</asp:LinkButton>
</td>
</tr>
</table>
源代码是:
public partial class ctrls_PageControl : System.Web.UI.UserControl
{
public static int pageSum, num;
protected void Page_Load(object sender, EventArgs e)
{
GetDataPage(1);
} private void GetDataPage(int pageIndex)
{
this.l_current.Text = pageIndex.ToString(); num = int.Parse(new News().GetTitle("S01").Rows[0][0].ToString()); //输入字符串的格式不正确 lbtn_sum.Text = num.ToString();//获得总条数 pageSum = num / 20 + 1;
BinPage(pageIndex); }
private void BinPage(int pageIndex)
{
List<int> numberList = new List<int>();
if (pageIndex < pageSum - 2)
{
for (int i = pageIndex + 1; i < pageIndex + 2; i++)
{
numberList.Add(i);
}
}
else
{
for (int i = pageIndex + 1; i <= pageSum; i++)
{
numberList.Add(i);
}
} this.rpt_number.DataSource = numberList;
this.rpt_number.DataBind(); if (pageIndex == 1)
{
lbtn_num.Text = "1 - 20";
}
else
{
if (20 * pageIndex > num)
{
lbtn_num.Text = 20 * (pageIndex - 1) + 1 + "-" + num;
}
else
{
lbtn_num.Text = 20 * (pageIndex - 1) + 1 + "-" + 20 * pageIndex;
}
}
} GetTitle()为根据A表中title得到B表中数据的存储过程:
public DataTable GetTitle(string cate_pkid)
{
SqlParameter[] parameters = new SqlParameter[] {
new SqlParameter("@Parent_ID", SqlDbType.VarChar, 10)};
parameters[0].Value = cate_pkid;
DataTable dt = FillDatatable("up_getTitleBYhead", parameters);
return dt;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货