I am attempting to implement a pagination bar above a table. I have a static <ul>
element on my page:
<div><center><ul id="topnav" runat="server"></ul></center></div>
In my Page_Load event, I attempt to load it with page numbers:
foreach ( int linkpage in linkpages ) {
HtmlGenericControl li;
LinkButton pbut;
li = new HtmlGenericControl( "li" );
pbut = new LinkButton();
pbut.CommandName = "page";
pbut.CommandArgument = linkpage.ToString();
pbut.Text = linkpage.ToString();
li.Controls.Add( pbut );
topnav.Controls.Add( li );
}
However, when the control is actually rendered it posts as name 'ctlXX' (where XX is an incrementing number) and no argument, so I am unable to get the page number. The .Text attribute renders fine, however. What am I doing wrong? Is this entirely the wrong approach?
If you are dynamically adding buttons you need to do it in the Page_Init
method otherwise the control won't participate in ViewState. The could be the problem. Certainly it won't help.
Also the name is what .Net dynamically assigns to the button because you haven't specified an ID yourself. It will be that ID that is visible in the Request.Form
collection if that is where you are looking.
But - i think your approach is wrong to be honest. If you want pagination then you should be looking at something like a GridView or ListView control which has pagination built into the control. That would be the more standard what of doing things. GridView paging explained here and a general tutorial for ListViews including pagination here.
Generally pagination is a problem that has been solved a number of times in ASP.Net and you really wouldn't want to reinvent the wheel - well I wouldn't. Also by using these type of grid controls you will get things like sorting out of the box - another very common requirement.