I have an issue with a pager control i am building. My code is the following
protected override void CreateChildControls()
{
base.CreateChildControls();
pnl = new Panel { ID = "NewsPager", CssClass = "NewsPager" };
Controls.Add(ddl);
AddPagerControls();
}
AddPagerControls adds a bunch of LinkButtons that use the same event handler:
private void li_Click(object sender, EventArgs e)
{
selectedValue = ((LinkButton) sender).CommandArgument;
AddPagerControls();
}
What happens now is that when i add the "Next" LinkButton, it gets it's CommandArgument set correctly using:
var liNext = new LinkButton {ID = "NewsPagerLinkNext", Text = ">", CommandArgument = (int.Parse(value) + 1).ToString()};
liNext.Click += new EventHandler(li_Click);
pnl.Controls.Add(liNext);
i.e. if the current page is 2, then the "Next" button's CommandArgument will be 3 when debugging the code. However, when the page has rendered and I click the next button it will work once (going from page 1 to 2) but then it will always be 2, even though in code it is set to 3. So something must happen when the control is rendered.
I am a bit at a loss here. I tried changing the call to AddPagerControls in CreateChildControls to if(!Page.IsPostBack){AddPagerControls();} but then the event handler won't fire at all.
Edit: P.S.I do a pnl.Controls.Clear() when the AddPagerControls method is called from event handler, otherwise the LinkButtons would be added twice.
Edit: P.P.S. I recaculate which Linkbuttons to add each time because i need to show only 5 links if more then pages exist i.e.
<< < 2 3 4 5 6 7 > >>