如何在asp.net中使用动态点击事件生成动态锚标记

时间:2015-07-21 11:29:52

标签: asp.net dynamic anchor

当我点击锚标记时,页面获得回发并且什么都不做

代码如下所示,

System.Web.UI.HtmlControls.HtmlAnchor anchor1 = new System.Web.UI.HtmlControls.HtmlAnchor();                    
anchor1.InnerText = "Edit";
anchor1.Attributes.Add("runat", "server");                    
anchor1.Attributes.Add("onServerClick", "anchor1_Click");                 
anchor1.Style.Add("color", "blue");
anchor1.Style.Add("float", "right");

protected void anchor1_Click(object sender, EventArgs e) {
    ScriptManager.RegisterStartupScript(this, this.GetType(), "msg", "alert('Enter Value For Drop Down Name');", true);
}

2 个答案:

答案 0 :(得分:1)

  

请试试这个

anchor1.ServerClick += new EventHandler(anchor1_Click);

答案 1 :(得分:0)

尝试这样的尝试,

protected void Page_Load(object sender, EventArgs e)
{
  System.Web.UI.HtmlControls.HtmlAnchor anchor1 = new System.Web.UI.HtmlControls.HtmlAnchor();
  anchor1.InnerText = "Edit";
  anchor1.Attributes.Add("runat", "server");                
  anchor1.Style.Add("color", "blue");
  anchor1.Style.Add("float", "right");
  anchor1.ServerClick += new EventHandler(anchor1_Click);
}

protected void anchor1_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, this.GetType(), "msg", "alert('Enter Value For Drop Down Name');", true);

}