Add onclick event to a HYPERLINK in asp.net
Hyperlink is a client side object rather run via server side validations.
But if you want to capture hyperlink ID at it's onclick event you have to add
on click event manually.
public void HyperLinkHandler(string ID)
{
Response.Write("You clicked HyperLink with ID : " + ID);
}
<asp:HyperLink ID="HyperLink1" runat="server" >HyperLink</asp:HyperLink>
Thank you
Dasun Tharaka.
But if you want to capture hyperlink ID at it's onclick event you have to add
on click event manually.
protected void Page_Load(object sender, EventArgs e)
{
HyperLink1.Attributes.Add("onClick",Page.GetPostBackEventReference(this.HyperLink1, "HyperLinkPostBack$" + this.HyperLink1.ID));
if ((IsPostBack)
&& (Request.Form["__EVENTTARGET"] != null)
&& (Request.Form["__EVENTARGUMENT"].ToString().StartsWith("HyperLinkPostBack"))
)
{
string[] args = Request.Form["__EVENTARGUMENT"].ToString().Split('$');
if ((string.Compare(args[0], "HyperLinkPostBack",
false, System.Globalization.CultureInfo.InvariantCulture) == 0) && (args.Length > 1))
{
HyperLinkHandler(args[1].ToString());
}
}
}
public void HyperLinkHandler(string ID)
{
Response.Write("You clicked HyperLink with ID : " + ID);
}
<asp:HyperLink ID="HyperLink1" runat="server" >HyperLink</asp:HyperLink>
Thank you
Dasun Tharaka.
Comments
Post a Comment