onClick 用于动态生成 LinkButton
在我的 sharepoint Web 部件应用程序中。我正在动态生成 LinkButtons,如下所示。这工作正常
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能不会在每次回发时重新创建动态生成的链接。
如果您的 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.我在这里遇到了同样的问题....
我在触发事件后创建了一个
HtmlTable
...该表有 (n) 个
HtmlTableRow
(在事件处理程序中计算)现在每行包含 2 个
LinkButton
,它们是在处理事件后从后面的代码生成的......并且每个
LinkButton
都分配有一个新的事件处理程序:lnkbtnEdit.CommandArgument = b.BookID.ToString();
lnkbtnEdit.Click += new EventHandler(lnkbtnEdit_Click);
其中
lnkbtnEdit_Click
签名如下:protected void lnkbtnEdit_Click(object sender, EventArgs e)
奇怪的是..当我点击生成的
LinkButton
时有一个回发...但事件没有触发...我不知道问题到底是什么...但我找到了解决方案:..
看起来这些生成的控件在回发时消失了(尝试分配一个 id 并使用
Page.FindControl()
女巫返回 null !!)所以我必须重新链接按钮....在Page_Load上我重新生成了LinkButtons,具有相同的ID...并将它们链接到各自的
EventHandler
i had the same problem here ....
i was creating an
HtmlTable
after firing an event...this table has (n)
HtmlTableRow
s (calculated in the event handler)now each row contains 2
LinkButton
s 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
EventHandler
s