我想动态添加链接到我的页面,如下所示:
foreach (Node node in this._nodes)
<a target="main" href="MyLink" class="menuclass">node.title</a>
但是现在我想在点击其中一个链接时添加c#事件,所有链接的事件都相同,我该怎么做?
提前致谢。
答案 0 :(得分:3)
用户LinkButton使链接看起来像超链接但行为像按钮。这是你的答案
int counter = 1;
foreach (Node node in this._nodes)
{
LinkButton lnkPost = new LinkButton();
lnkPost.ID = "lnk" + i.ToString();
lnkPost.Text = node.Title;
lnkPost.Click += new EventHandler(LinkPost_OnClick);
parent.Controls.Add(lnkPost);
}
protected void LinkPost_OnClick(object sender, EventArgs e)
{
//add your handler code here
}
答案 1 :(得分:1)
大概是这样的:
int counter = 1;
foreach (Node node in this._nodes)
{
HyperLink DynLink = new HyperLink();
DynLink.ID = "DynLink" + counter.ToString();
DynLink.Text = node.title;
DynLink.NavigateUrl = "MyLink";
counter++;
}