我对ASP非常陌生,并且在一个问题上停留了大约一周。该问题可能与“ Asp页面生命周期”有关,但我无法找到解决方法。问题是,当我单击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>

最佳答案

您的问题是:

您没有在回发时再次绑定数据,我对您的代码做了一些修改,其中存在一些问题:


在方法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);
 }




您没有在回发中再次绑定数据,所以我对其进行了修改:
页面加载:



   protected void Page_Load(object sender, EventArgs e) {
        //Remove the Page.IsPostBack checking
      skiptof();
   }



请注意,您动态添加的控件将被清除,您必须在回发时再次添加它,以免丢失数据。

然后,您将能够获取PagesPageLink_Click事件的值:
c# - 动态创建的LinkBut​​ton OnClick事件不会在PostBack上触发-LMLPHP

整个示例在这里:
http://pastie.org/10503291

关于c# - 动态创建的LinkBut​​ton OnClick事件不会在PostBack上触发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33312784/

10-10 17:04