user5481980
user5481980

Reputation: 23

Dynamically created LinkButton OnClick Event not firing on PostBack

I am quite new to ASP and I have been stuck on an issue for about a week. The issue is probably something to do with the Asp Page Life Cycle but I am unable to find how this can be resolved. The issue is that skipto(..) is never called when I click the LinkButton (that were created on first Page Load), which means the LinkButtons are not rendered.

Sample Code below:

// Code Behind
protected void Page_Load(object sender, EventArgs e)
{
    loadData();
    if (!Page.IsPostBack)
    {           
        skiptof();
    }
}

public void loadData() {
    // Loads from database
}

public void skipto(object sender, EventArgs e)
{
    LinkButton btn = sender as LinkButton;
        if (btn != null)
        {
            if (btn.CommandArgument != null && btn.CommandArgument != "0")
            {
                int currPage = 1;
                int.TryParse(btn.CommandArgument, out currPage);
                skiptof(currPage);
            }
        }
}

 public void skiptof(int currPage = 1)
 {
    int lastPage = // calculate from LoadData()
    string pageDisabled = "";
    // pages
    HtmlGenericControl ul = new HtmlGenericControl("ul");
    while (pageCount <= lastPage)
    {
            // Disable the current page
        pageDisabled = pageCount == currPage ? " class=\"disabled\"" : "";
        HtmlGenericControl pagesli = new HtmlGenericControl("li");
        if (pageDisabled != "")
        {
            pagesli.Attributes.Add("class", "disabled");
        }
        LinkButton pagesPageLink = new LinkButton();
        pagesPageLink.Click += new EventHandler(skipto);
        pagesPageLink.CommandArgument = pageCount.ToString();
        pagesPageLink.Text = pageCount.ToString();
        pagesli.Controls.Add(pagesPageLink);
        ul.Controls.Add(pagesli);
        pageCount += 1;
    }
    pagination.Controls.Add(ul);
 }


 // page 
<asp:ScriptManager ID="ScriptManager1" runat="server"/>
<asp:UpdatePanel runat="server" id="UpdatePanel" UpdateMode="Conditional">
    <ContentTemplate>
        <div id="details" runat="server"></div>
        <div class="pagination text-center" id="pagination" runat="server"></div>
    </ContentTemplate>
</asp:UpdatePanel>

Upvotes: 2

Views: 2249

Answers (1)

User2012384
User2012384

Reputation: 4927

Your problem is:

You didn't bind the data again on postback, I've modified your code a little bit, there are several problems:

  1. in the method skipof:
public void skiptof(int currPage = 1) {
    //Clear the controls here then add them again
  pagination.Controls.Clear();
  int lastPage = // calculate from LoadData()
  string pageDisabled = "";
  HtmlGenericControl ul = new HtmlGenericControl("ul");
  while (pageCount <= lastPage) {
      // Disable the current page
      pageDisabled = pageCount == currPage ? " class=\"disabled\"" : "";
      HtmlGenericControl pagesli = new HtmlGenericControl("li");
      if (pageDisabled != "") {
          pagesli.Attributes.Add("class", "disabled");
      }
      LinkButton pagesPageLink = new LinkButton();
        // you can directly assign the method to be called here, there is no need to create a new EventHandler
      pagesPageLink.Click += PagesPageLink_Click;
      pagesPageLink.CommandArgument = pageCount.ToString();
      pagesPageLink.Text = pageCount.ToString();
      pagesli.Controls.Add(pagesPageLink);
      ul.Controls.Add(pagesli);
      pageCount += 1;
  }
  pagination.Controls.Add(ul);
 }
  1. You didn't bind the data again in postback, so I modified it: Page Load:
   protected void Page_Load(object sender, EventArgs e) {
        //Remove the Page.IsPostBack checking
      skiptof();
   }

Please take note that the controls you added dynamically will be cleared and you have to add it again on postback to avoid data lost.

Then you'll be able to get the value on PagesPageLink_Click event: enter image description here

The whole sample is here: http://pastie.org/10503291

Upvotes: 2

Related Questions