There are hundreds of posts out there about this very same issue and not one provides an acceptable answer to my situation.
I have a search page. The user enters or selects some search criteria and clicks the search button. The page posts back, hides the search form which is inside a panel, and displays the search results panel which contains a DataList as well as navigation buttons for next page and numbered pages. I dynamically create LinkButtons which display 1 2 3 4 5 etc. for navigating to results page 1, 2 3 4 5 etc. Works great visually. Now, I'm trying to wire up the click event for each page. Clicking on a number is not causing my event to fire and navigate to another page. I tried moving the linkbutton creation to the page_onload event. Problem is, the information necessary to build the links is not available in the page onload event. If I put a call to my method in both page_onload (or even the panel onload) and in the click event of my search button, I get duplicate linkButtons, but at least the second group works.
protected void makePageLinks()
{
int startPageForRange;
if (PageIndex <= 4 || PageCount <= PageSize)
startPageForRange = 1;
else if (PageIndex >= PageCount -6)
startPageForRange = PageCount - PageSize + 1;
else
startPageForRange = PageIndex -3;
int endPageForRange;
if (startPageForRange + PageSize - 1 < PageCount)
endPageForRange = startPageForRange + PageSize - 1;
else
endPageForRange = PageCount;
int i;
for (i = startPageForRange; i <= endPageForRange; i++)
{
LinkButton pageLink = new LinkButton();
pageLink.ID = "Page_" + i + "_Link";
pageLink.Text = i.ToString();
pageLink.Attributes.Add("title", "Go to page " + i);
pageLink.CommandName = "page";
pageLink.CommandArgument = i.ToString();
pageLink.Command += new CommandEventHandler(numberedPage_Click);
if (i == PageIndex + 1)
{
pageLink.CssClass = "current";
}
topPageLinks.Controls.Add(pageLink);
}
}