Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
visual studio, ASP.NET

hi friends,
I use PlaceHolder control and add two HyperLink controls in Page_load by this code:
System.Web.UI.WebControls.HyperLink hl = new HyperLink();

hl = new HyperLink();

Now the problem is how should i recongnize which hyperlink did user clicked !!??
for example use some way to change requesting query or changing session after hyperlink click !

Sorry for bad explanation and my bad english.
i'll appreciate any help.
thanks in advanced.
Updated 3-Aug-12 1:49am

With the ID or Text property of HyperLink you can come to know which HyperLink is clicked actually. Try this:
HyperLink hl = new HyperLink();
hl.ID = "HyperLnk1";
hl.Text = "HyperLnkText1";

hl = new HyperLink();
hl.ID = "HyperLnk2";
hl.Text = "HyperLnkText2";

Now you can recognize the hyperlinks using Text or ID property.

Share this answer
Mohamad77 4-Aug-12 1:17am    
thanks for the answer but can i recognize with ID or Text !?
where should i type the code like this:

if(hl.text == HyperLink1) label1.text = HyperLink11
else if(hl.text == HyperLink2) label1.text = HyperLink12
_Amy 4-Aug-12 1:24am    
Any event, where ever you are trying to find the clicked hyperlink name. Find your control first and compare it.
If you are trying to find it in next page, then:
HyperLink hl=(HyperLink)Page.PreviousPage.FindControl("HyperLnk1");
Mohamad77 4-Aug-12 1:40am    
thanks _Amy
I'll try it.
If you want to subscribe to a click event I would rather use the ASP LinkButton control than a regular hyperlink. Example:
protected void Page_Load(object sender, EventArgs e)
    LinkButton linkButton = new LinkButton();
    linkButton.Text       = "LinkButton";
    linkButton.Click     += new EventHandler(linkButton_Click);


void linkButton_Click(object sender, EventArgs e)
    LinkButton linkButton = sender as LinkButton;

    if (linkButton.Text == "LinkButton")
        // Open a different page
Share this answer
Mohamad77 4-Aug-12 1:19am    
thanks helmi,
i'm sorry i forgot to say that i should use HyperLink to Navigate to another page ! i don't know that it's possible to navigate to the other page by clicking LinkButton! is it possible ? then how ?

thanks again.
helmi77 4-Aug-12 3:50am    
Of course it is, please refer to the if statement above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900