Linkbutton inside ASP.Net Paging Repeater

I am doing a search webpage that brings a lot of information from MSSQL. I made a stored procedure that only returns the page that will be displayed on the website.

I'm working on paging right now, because I need to show something similar than Google. If you are on page 1, they show the first 10 pages, and if you are on page 19, they are displayed from pages 9 to 28.

I think the best way to show page numbers is to use the link button inside the repeater. The problem that I am facing right now is that I do not know how best to take the page number when posting back.

As a quick sample, I assigned an ArrayList to repater.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> 

In my Default.aspx.cs file, I have the following code

  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. } 

What should I do to make this work?

Does anyone have a better solution for this search call?

thanks

Jerry

+6
c # paging repeater linkbutton
source share
4 answers

You are looking for the ItemCommand event:

  <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> 

Code behind:

 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!"); } 

... but are you really sure you need LinkButton? A bright HTML anchor tag can work just as well, and it's less fuzz. :)

+9
source share

Just think about whether you tried using the DataGrid object by adding a column, making it an element template, and then inserting the elements that you want to repeat in a formatted template. DataGrid also automatically handles paging when set to true ...

0
source share

You have never stated what type of control this is your paging. If you are using ASP.Net 3.5, I highly recommend using the ListView control and handling the paging with the DataPager control.

0
source share

I used @JakobGade and this is what worked for me:

 <asp:Repeater ID="rpMemList" runat="server" ClientIDMode="Static" onitemcommand="rpMemList_ItemCommand"> <ItemTemplate> <asp:LinkButton ID="lbMember" CommandArgument='<%# Eval("memID")%>' CommandName="SelMem" runat="server" ClientIDMode="Predictable"><%# Eval("memFullName")%></asp:LinkButton> </ItemTemplate> </asp:Repeater> 

Then tested it in code:

  protected void rpMemList_ItemCommand(object source, RepeaterCommandEventArgs e) { string a = e.CommandArgument.ToString(); string b = e.CommandName.ToString(); string c = e.CommandSource.ToString(); string d = e.Item.ToString(); } 
0
source share

All Articles