Srinivas
Srinivas

Reputation: 342

Finding the ID of a link

I have added links dynamically to an asp page. On clicking, I call a function. I want to find out the id of the link that is calling the function. I have added google.com and yahoo.com to explain my needs. I want the function to pull data from db in the original scenario which I have done.

 <a href="#"  onServerClick="abc_Click" id="4"/>click</a>
 <a href="#" onServerClick="abc_Click" id="5"/>click here</a>

C# code:

 protected void abc_Click()
 {
    if(id is 4) // don't know how to know the id of the link that calls the function. 
      Response.Redirect("http://www.google.com");
    else
      Response.Redirect("http://www.yahoo.com");
 }

I referred Call a C# function in ASP.NET when clicking on a HTML link .

Now I have two problems.

  1. The function is not getting called when link is clicked.
  2. I do not know to how to find the id of the link that calls the function.

Upvotes: 0

Views: 971

Answers (3)

Kenneth
Kenneth

Reputation: 28737

You're missing the runat-server attribute. If you include this, you need to have the link in a form. But you can't pass the ID of the link to the server (unless you first modify a hidden field with JS and then post the hidden field). However, that is a very complex solution to a rather simple problem.

The easiest way is to just add a querystring and then do the db call in the Page_Load.

Example:

<a href="/mypage.aspx?id=4" />click</a>
<a href="/mypage.aspx?id=5" />click here</a>

And then in the code-behind:

public void Page_Load(object sender, EventArgs e)
{
    if(Request.QueryString["id"] == "4")
    {
        Response.Redirect("http://www.google.com");
    }
    else
    {
        Response.Redirect("http://www.yahoo.com");
    }
}

Upvotes: 1

David
David

Reputation: 218808

You missed an important attribute for referencing these links server-side:

runat="server"

It should be something like this:

<a href="#" runat="server" onServerClick="abc_Click" id="4"/>click</a>
<a href="#" runat="server" onServerClick="abc_Click" id="5"/>click here</a>

How are you adding these dynamically, by the way? Hopefully server-side. Because if they're being added via JavaScript on the client then it's too late for the server to bind events to them.

It's also recommended that you don't make these links, since they're not actually linking to anything. (The href="#" just tells it to go to the top of the page.) Since these are supposedly posting to the server to enact some server-side action, they be better off as buttons. (Which would make what you're trying to do a lot easier, by the way.) Buttons can be styled to look like links if necessary for the UX.

Your server-side handler also needs additional arguments:

protected void abc_Click(object sender, EventArgs e)

In this case, sender will be the element that's been clicked. You can cast it to that element's type and get all the information you need from it, including its .Id property.

Upvotes: 2

MichaC
MichaC

Reputation: 13381

add runat="server" to your html markup to have the element available on your server. Otherwise it is just an html element and those obviously cannot be accessed on the server...

<a href="#" runat="server" onServerClick="abc_Click" id="4" />

You would also have to implement the event handler correctly because your implementation is missing the parameters

Upvotes: 1

Related Questions