3

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.

1
  • Could you elaborate on what are you going to accomplish? Commented Nov 24, 2011 at 18:50

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

6 Comments

Or you could use Webmethods or callbacks but you need to express what exactly you want
With the linkbutton you get a postback which implies a trip to the server; what do you intend to gain from using a "onserverclick" that you can't get with the simple event handler that comes with the linkbutton?
ok let me explain more-I am getting an id of product on server side,now I want this linkbutton to send his event handler the id of the product as a parameter (to the function),in simple event handler I don't know The id yet
maybee something like:a1.attribute.add("onseverclick",function(id))
But where will the ID come from? If you will assemble the link beforehand wouldn't it be easier to put the validation on the "Page_Load"?
|
3

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);
    }

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.