I have problems with my dynamically link buttons in asp pages. I use them for making a custom paging for a grid view. The idea is that i want to display them 20 about 20. It is working for first 20. When i display them it's all right. Then i press next for displaying the next 20. When i press next, it displaying me, but if a press a button other than the initial 20, it is going me at the first 20.
My init page:
override protected void OnInit(EventArgs e)
{
base.OnInit(e);
if (this.Tag.Equals("Shareholder"))
{
InitComponents(false);
}
/// Must be done everytime the page loads.
InitializeList();
if (!IsPostBack)
InitializeUI();
}
Init link buttons
private void InitComponents(Boolean Prev)
{
PanelPager.Controls.Clear();
int nrInregistrari = Convert.ToInt32(DAActionar.CountActionari(11, LastBtnIndex));
if (Prev == true)
{
LinkButton lnkPrev = new LinkButton();
lnkPrev.Text = "Prev";
PanelPager.Controls.Add(lnkPrev);
}
int BtnDeAfisat = 0;
if (nrInregistrari > BTN_PER_SERIE * PAGE_SIZE)
{
BtnDeAfisat = BTN_PER_SERIE;
}
else
BtnDeAfisat = nrInregistrari / PAGE_SIZE + 1;
for (int index = 1; index <= BtnDeAfisat; index++)
{
int pageNo = index + LastBtnIndex;
LinkButton lnk = new LinkButton();
lnk.Click += new EventHandler(PageChange);
lnk.ID = "PageLink" + pageNo.ToString();
lnk.CommandName = "Page";
lnk.Text = " " + pageNo.ToString() + " ";
lnk.CommandArgument = index.ToString();
PanelPager.Controls.Add(lnk);
}
LinkButton lnkNext = new LinkButton();
lnkNext.Click += new EventHandler(NextPage);
lnkNext.Text = "Next";
PanelPager.Controls.Add(lnkNext);
LastBtnIndex += BtnDeAfisat;
}
event for next buttons
private void NextPage(object sender, EventArgs e)
{
InitComponents(true);
}
PageChange:
public void PageChange(object sender, EventArgs e)
{
int pageIndex = int.Parse((sender as LinkButton).CommandArgument) + 1;
object dataSource = GetDataSource(OwnerId, null, pageIndex);
PushData(dataSource);
}
In ASP.NET only the controls which are added as part of Page_Load will have their events attached and will be executed. Also in when a server side event is executed the page posts itself back and during that the page_load executes first and then the event handler code gets executed.
So your first set of button events executed coz they were attached to event as part of page_load. Now during event of those button you are calling InitComponents method. So as I explained during the event page_load happens first which calls InitComponents method, which adds button to panel but InitComponents method executes again as part of event handler code which removes controls from Panel and re-creates them and add them again to the Panel.
Now since controls created during 2nd execution of InitComponents method are not no part of page_load flow events attached to them are not firing when you click on them. It just posts back the page which executed InitComponents again and which creates new controls in the panel and they will have their events working fine because they are created as part of page_load flow.
Pardon me if it confuses you.
Now the solution to this would be to use a control which can create many buttons automatically for you based on the number of items and also fires events all the times.
I do not have anything working right now for you. I found this article with working sample code which you can consider as an example and implement in your work.
http://www.aspsnippets.com/Articles/Implement-Paging-in-DataList-control-in-ASPNet.aspx
The example explains how to display page numbers and also how to bind your data based on the selected page number.