Dr.Denis McCracleJizz
Dr.Denis McCracleJizz

Reputation: 870

Repeater Paging, dynamic buttons

i am trying to implement paging with a reapeter that creates a table. The problem i have is that i add buttons inside the tr but when i click on them nothing happens if i put a break point inside their click handler.

Creating the button

    private void CreatePaging(PlaceHolder literal)
    {
        int numPage = (totalItems / ITEMS_PER_PAGE);
        if (totalItems % ITEMS_PER_PAGE > 0) numPage++;



        literal.Controls.Add(CreateButton("<<", "b"));
        for (int i = 0; i < numPage; i++) {



            if (currentPageIndex == i)
            {
                Label lbl = new Label();
                literal.Controls.Add(lbl);
                lbl.Text = " " + (i + 1).ToString() + " ";    
            }
            else {
                literal.Controls.Add(CreateButton((i+1).ToString(), (i+1).ToString()));
            }

        }
        literal.Controls.Add(CreateButton("&gt;&gt;", "f"));
    }



    LinkButton CreateButton(string title, string index) {
        LinkButton lnk = new LinkButton();
        lnk.ID = index.ToString();
        lnk.Text = title;
        lnk.CommandArgument = index.ToString();
        lnk.Click += new EventHandler(PageBtnClicked);
        return lnk;
    }

Upvotes: 0

Views: 478

Answers (2)

Mikey Mouse
Mikey Mouse

Reputation: 3098

On your Repeater in the markup, you could add OnItemCommand="rptMystuff_ItemCommand"

And in your next page Button / LinkButton CommandName="NextPage"

Then in your code behind

    protected void rptMyStuff_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
         if (e.CommandName == "NextPage")
         {
              //Code to switch pages
         }
         if (e.CommandName == "PreviousPage")
         {
              //More code
         } 
    }

Edit:

Oh and as for remembering what page you're on, might as well store than in a HiddenField on the web page. Something like

      public int PageNum
      {
          get { return string.IsNullOrEmpty(hfPageNum.Value) ? 0 : int.Parse(hfPageNum.Value); }
          set { hfPageNum.Value = value.ToString(CultureInfo.InvariantCulture); }
      }

Upvotes: 0

user1517584
user1517584

Reputation: 74

Check when your CreatePaging() method is called during the page-lifecycle. It needs to be called during either Page_Init or Page_Load.

If it's called after that, then the controls won't have been created by the time ASP.NET processes the call-back and it doesn't know what handler to pass the button-click event to.

Upvotes: 1

Related Questions