onClick 用于动态生成 LinkBut​​ton

发布于 2024-12-10 14:48:23 字数 1565 浏览 0 评论 0原文

在我的 sharepoint Web 部件应用程序中。我正在动态生成 LinkBut​​tons,如下所示。这工作正常

foreach (var pName in productTypesNames[productType] )
{
   var subLi = new HtmlGenericControl("li");
   var linkButton = new LinkButton{ Text = pName };
   linkButton.Click += new EventHandler(linkButton_Click);
   subLi.Controls.Add(linkButton);
   ul.Controls.Add(subLi);
}

但是,当我单击 UI 中的一个链接时,我的调试器永远不会命中在

void linkButton_Click(object sender, EventArgs e)
{
}

“更多代码”

protected void StateClicked(object sender, CommandEventArgs e)
{
  //Generate a dictionary of type Dictionary<string, List<string>>
  //Display the dictionary
  foreach (var productType in productTypesNames.Keys)
        {
            var li = new HtmlGenericControl("li");
            nav.Controls.Add(li);
            var ul = new HtmlGenericControl("ul");

            var anchor = new HtmlGenericControl("a");
            anchor.Attributes.Add("href", "#");

            foreach (var pName in productTypesNames[productType] )
            {
                var subLi = new HtmlGenericControl("li");
                var linkButton = new LinkButton{ Text = pName };
                linkButton.Click += new EventHandler(linkButton_Click);
                subLi.Controls.Add(linkButton);
                ul.Controls.Add(subLi);
            }
            anchor.InnerHtml = productType;
            li.Controls.Add(anchor);
            li.Controls.Add(ul);
        }
 }

的第一行设置的断点,其中通过单击美国的图像地图来调用 stateClicked。

In my sharepoint web-part application. I am dynamically generating LinkButtons as below. and this works fine

foreach (var pName in productTypesNames[productType] )
{
   var subLi = new HtmlGenericControl("li");
   var linkButton = new LinkButton{ Text = pName };
   linkButton.Click += new EventHandler(linkButton_Click);
   subLi.Controls.Add(linkButton);
   ul.Controls.Add(subLi);
}

However, when I click on one of the links in UI, my debugger never hits the breakpoint that is set at very first line of

void linkButton_Click(object sender, EventArgs e)
{
}

More Code

protected void StateClicked(object sender, CommandEventArgs e)
{
  //Generate a dictionary of type Dictionary<string, List<string>>
  //Display the dictionary
  foreach (var productType in productTypesNames.Keys)
        {
            var li = new HtmlGenericControl("li");
            nav.Controls.Add(li);
            var ul = new HtmlGenericControl("ul");

            var anchor = new HtmlGenericControl("a");
            anchor.Attributes.Add("href", "#");

            foreach (var pName in productTypesNames[productType] )
            {
                var subLi = new HtmlGenericControl("li");
                var linkButton = new LinkButton{ Text = pName };
                linkButton.Click += new EventHandler(linkButton_Click);
                subLi.Controls.Add(linkButton);
                ul.Controls.Add(subLi);
            }
            anchor.InnerHtml = productType;
            li.Controls.Add(anchor);
            li.Controls.Add(ul);
        }
 }

Where stateClicked is called by a click on the image map of USA.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

[旋木] 2024-12-17 14:48:23

您可能不会在每次回发时重新创建动态生成的链接。

如果您的 foreach 周围有一个 if (!IsPostback),请尝试将其删除。

You probably aren't recreating the dynamically generated links on every postback.

If you have a if (!IsPostback) wrapped around your foreach, try removing it.

_失温 2024-12-17 14:48:23

我在这里遇到了同样的问题....

我在触发事件后创建了一个 HtmlTable...
该表有 (n) 个 HtmlTableRow(在事件处理程序中计算)
现在每行包含 2 个 LinkBut​​ton,它们是在处理事件后从后面的代码生成的......
并且每个 LinkBut​​ton 都分配有一个新的事件处理程序:

lnkbtnEdit.CommandArgument = b.BookID.ToString();
lnkbtnEdit.Click += new EventHandler(lnkbtnEdit_Click);

其中 lnkbtnEdit_Click 签名如下:
protected void lnkbtnEdit_Click(object sender, EventArgs e)

奇怪的是..当我点击生成的LinkBut​​ton时有一个回发...但事件没有触发...

我不知道问题到底是什么...但我找到了解决方案:..
看起来这些生成的控件在回发时消失了(尝试分配一个 id 并使用 Page.FindControl() 女巫返回 null !!)

所以我必须重新链接按钮....在Page_Load上我重新生成了LinkBut​​tons,具有相同的ID...并将它们链接到各自的EventHandler

i had the same problem here ....

i was creating an HtmlTable after firing an event...
this table has (n) HtmlTableRows (calculated in the event handler)
now each row contains 2 LinkButtons that are generated from the code behind .. after an event is handled...
and each LinkButton is assigned a new event handler:

lnkbtnEdit.CommandArgument = b.BookID.ToString();
lnkbtnEdit.Click += new EventHandler(lnkbtnEdit_Click);

where lnkbtnEdit_Click signature is as follows:
protected void lnkbtnEdit_Click(object sender, EventArgs e)

the weird thing is that .. there is a postback when i click the generated LinkButton... but the event was not firing...

i don't know exactly what the problem was ... but i found the solution: ..
it appears as if these generated controls disappear on the postback (tried to assign an id and used Page.FindControl() witch returned with null !!)

so i had to re-link the buttons.... on the Page_Load i re-generated the LinkButtons, with same ID... and linked them to their respective EventHandlers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文