简体   繁体   English

动态创建的LinkBut​​ton OnClick事件不会在PostBack上触发

[英]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. 我对ASP非常陌生,并且在一个问题上停留了大约一周。 The issue is probably something to do with the Asp Page Life Cycle but I am unable to find how this can be resolved. 该问题可能与“ Asp页面生命周期”有关,但我无法找到解决方法。 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. 问题是,当我单击LinkBut​​ton(在第一个页面加载时创建)时,永远不会调用skipto(..),这意味着未渲染LinkBut​​ton。

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: 在方法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: 然后,您将能够获取PagesPageLink_Click事件的值: 在此处输入图片说明

The whole sample is here: http://pastie.org/10503291 整个示例在这里: http : //pastie.org/10503291

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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