6

I'm doing a webpage with a search that brings a lot of information from MSSQL. What I did is a stored procedure that return only the page to be seen on the website.

Right now I'm working on the paging as I need to show something similar than google. If you are at page 1 they show first 10 pages and if you are at page 19 they show since page 9 to 28.

I think the best option to show the page numbers is using a linkbutton inside a repeater. The problem that I have now is that I do not know the best way to take the page number at postback.

Doing a quick sample I assigned an ArrayList to 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>

At my Default.aspx.cs file I have the next 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 Shall I do to make it works?

Does anyone has a better solution for this paging?

Thank you

Jerry

Jakob Gade
  • 12,319
  • 15
  • 70
  • 118
Gerardo Abdo
  • 1,150
  • 3
  • 10
  • 16

4 Answers4

10

You're 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 the LinkButton? A plain HTML anchor tag might work just as fine, and it's less fuzz. :)

Jakob Gade
  • 12,319
  • 15
  • 70
  • 118
0

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 within code-behind:

    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();

    }
Ravi Ram
  • 24,078
  • 21
  • 82
  • 113
0

Just a thought, have you tried using a "DataGrid" object, adding a column, making it an item template and then putting in the elements you need to repeat within the template formatted. The DataGrid also automatically handles paging when set to true...

DRapp
  • 47,638
  • 12
  • 72
  • 142
  • Well, the problem with using an automatic pagging is that I need to bring all the registers from the DB and to make it quicker I want to return just what will be shown ;) – Gerardo Abdo Feb 26 '10 at 17:31
0

You never stated what type of control it is your paging. If you are using ASP.Net 3.5 then I HIGHLY suggest using the ListView control and handling the paging with the DataPager control.

Payton Byrd
  • 956
  • 1
  • 12
  • 27
  • What I have seen is that Datapager needs a control assigned to automatically paging and for that I need to bring all the registers from the DB and to make it quicker I want to return just what will be shown – Gerardo Abdo Feb 26 '10 at 17:23