简体   繁体   中英

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>

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: 在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM