views:

212

answers:

2

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 > >>

A: 

What I don't understand is why you regenerate your paging controls all the time. Couldn't you just use an approach where you have your link-buttons "prev" and "next" and you maintain in the viewstate of your control (as separate properties) the current page that is being viewed and the amount of items (i.e. the page-size)?

In the event handler you then do the proper actions since you know that your current page will be something like currentPageIndex+1 or something similar. Regenerating the prev/next buttons all the time, especially in the li_Click handler isn't a good approach from my point of view and this is also what may cause your problems.

Juri
Already tried that, even when having the liNext LinkButton always there, making it visible when needed and setting just the CommandArgument in the event handler results in same thing, i can click any link once, then it stops working, it's as if the controls are outside the control tree or something (impossible, i know...)
Colin
with "stops working" do you mean that the event handler isn't called?
Juri
Not exactly, what i mean is that the previous / next link will always keep the value they get after clicking once, the same goes for any numeric value. THe pager is used in sharepoint to page a DataFormWebPart that uses an ObjectDataSource. This is the reason this control needs to be "all inclusive", i.e. render itself from a self contained datasource.I think the problem lies in the fact that the numerical links should change after one of the links is clicked, i.e. the control tree changes, but those changes are not rendered in the page....
Colin
A: 

Found it. was thinking way to difficult, it doesn't matter that the numbers should change imediately, the next page refresh will take care of that. Just stored everything in ViewState like Yuri said and build pager just once. Thnx!

Colin