Ger*_*bdo 6 c# asp.net paging repeater linkbutton
我正在做一个带有搜索的网页,它会从MSSQL中获取大量信息.我所做的是一个存储过程,只返回要在网站上看到的页面.
现在我正在进行分页,因为我需要显示类似于谷歌的东西.如果您在第1页,他们会显示前10页,如果您在第19页,则显示自第9页到第28页.
我认为显示页码的最佳选择是在转发器中使用链接按钮.我现在遇到的问题是我不知道在回发时获取页码的最佳方法.
做一个快速示例我将一个ArrayList分配给repeater.datasource:
<asp:Repeater ID="Repeater2" runat="server">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument="<%# Container.DataItem %>"><%# Container.DataItem %></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
<asp:LinkButton ID="LinkButton2" runat="server" CommandArgument="4654">Test #1</asp:LinkButton>
Run Code Online (Sandbox Code Playgroud)
在我的Default.aspx.cs文件中,我有下一个代码
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
string x = LinkButton2.CommandArgument;
//string y = LinkButton1.CommandArgument;
//I know this line will not work since the Linkbutton1 is inside the Repeater.
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能使它有效?
有没有人有这个分页更好的解决方案?
谢谢
杰瑞
您正在寻找ItemCommand事件:
<asp:Repeater ID="Repeater1" OnItemCommand="ItemCommand" runat="server">
<ItemTemplate>
<asp:LinkButton CommandName="ButtonEvent" CommandArgument="<%# Container.DataItem %>" Text="<%#Container.DataItem %>" runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
Run Code Online (Sandbox Code Playgroud)
代码背后:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Repeater1.DataSource = Enumerable.Range(1, 10);
Repeater1.DataBind();
}
}
protected void ItemCommand(Object Sender, RepeaterCommandEventArgs e)
{
Response.Write("The no. " + ((LinkButton)e.CommandSource).Text + " button was clicked!");
}
Run Code Online (Sandbox Code Playgroud)
...但你真的确定你需要LinkButton吗?一个普通的HTML锚标签也可以正常工作,并且它的模糊性较低.:)
只是想一下,您是否尝试过使用“DataGrid”对象,添加一列,使其成为项目模板,然后将需要在格式化模板中重复的元素放入其中。当设置为 true 时,DataGrid 还会自动处理分页...
| 归档时间: |
|
| 查看次数: |
17038 次 |
| 最近记录: |