baaroz
baaroz

Reputation: 19587

How to add event handler to html tag in asp.net code behind?

I have this html.

<a id="a1" runat=server>

I want to add event handler to this <a> tag using code behind. If a user press this tag, it should cause an event on the server-side.

Upvotes: 3

Views: 6973

Answers (3)

Deepan Babu
Deepan Babu

Reputation: 515

Try this,

Html Tag:

<a id="a1" runat="server">

Code Behind:

protected void Page_Init(object sender, EventArgs e)
{
      a1.ServerClick += new EventHandler(a1_ServerClick);
}

 protected void a1_ServerClick(object sender, EventArgs e)
 {
     //Your Code here....
 }

This will Create a Click Event for the <a> tag.

Upvotes: 2

Razvan Trifan
Razvan Trifan

Reputation: 544

I'm not sure what you are trying to accomplish, but it seems that you want something like this:

On the .ASPX page

<asp:LinkButton ID="myLinkButton" OnClick="myLinkButton_Click" runat="server"></asp:LinkButton>

In the .ASPX.CS

protected void Page_Load(object sender, EventArgs e)
    {
        myLinkButton.CommandArgument = "1";
    }

    protected void myLinkButton_Click(object sender, EventArgs e)
    {
        int myLinkButtonID = Convert.ToInt32(((LinkButton)sender).CommandArgument);
    }

Upvotes: 3

jclozano
jclozano

Reputation: 628

For an easy solution there is the linkbutton on the toolbox which shows as a link but reacts as a button.

Upvotes: 5

Related Questions