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:
C#
System.Web.UI.WebControls.HyperLink hl = new HyperLink();
PlaceHolder1.Controls.Add(hl);

hl = new HyperLink();
PlaceHolder1.Controls.Add(hl);



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.
Posted
Updated 3-Aug-12 1:49am
v2

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

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


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


--Amit
 
Share this answer
 
Comments
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);

    PlaceHolder1.Controls.Add(linkButton);
}

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

    if (linkButton.Text == "LinkButton")
    {
        // Open a different page
        Response.Redirect("WebForm2.aspx");
    }
}
 
Share this answer
 
v2
Comments
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