简体   繁体   中英

how to find selected hyperlink in asp.net using C#

I have a list of 10 hyperlink on default1.apx. On selecting any hyperlink it redirects to another page and all hyperlinks redirects to the same page default2.aspx. But how can i now which hyperlink is clicked from 10 hyperlinks list in asp.net using C#.

There are a couple of ways, all depending on how you're doing your redirect.

I'm guessing you're using Response.Redirect() , which means you must be raising a server side event when clicking. In this case all you need to do is check the sender argument which will give you details of which control was clicked.

Another way is to append the controls' name onto the URL in a GET request. So that each link is slightly different. For example link one could point to default2.aspx?linkthatwasclicked='link1' where you can substiute link1 accordingly, this value can then be retreived on default2.aspx through the Request.QueryString object.

m.edmondson has got it, another way I've seen is having a "hyperlink.aspx" page which then requests an id:

<a href="hyperlink.aspx?id=1">Link 1</a>

then in the hyperlink.aspx code:

int id = int.Parse(Request.QueryString["id"].ToString());
switch(id)
{
    case 1:
    // Add a count to a table maybe
    // Get the url from the DB here...
    string url = GetUrlById(id);
    Response.Redirect(url);
    break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM