gpt4 book ai didi

c# - 动态创建的 LinkBut​​ton OnClick 事件未在 PostBack 上触发

转载 作者:太空宇宙 更新时间:2023-11-03 10:21:28 33 4
gpt4 key购买 nike

我是 ASP 的新手,我在一个问题上被困了大约一个星期。该问题可能与 Asp 页面生命周期有关,但我无法找到解决方法。问题是当我单击 LinkBut​​ton(在第一次页面加载时创建)时,skipto(..) 永远不会被调用,这意味着 LinkBut​​tons 不会呈现。

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>

最佳答案

你的问题是:

你没有在回发时再次绑定(bind)数据,我稍微修改了你的代码,有几个问题:

  1. 在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. 你没有在回传中再次绑定(bind)数据,所以我修改了它:页面加载:
   protected void Page_Load(object sender, EventArgs e) {
//Remove the Page.IsPostBack checking
skiptof();
}

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

然后您将能够获取 PagesPageLink_Click 事件的值: enter image description here

整个样本在这里: http://pastie.org/10503291

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

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com