Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change href link in content place holder from C# code

Tags:

c#

asp.net

I have a content placeholder containing a link:

<asp:Content ID="Content5" runat="server"  contentplaceholderid="ContentPlaceHolder3">
<a href= "../WOPages/WO_Main.aspx?WONum=12345">WorkOrder</a>

and I would like to change the href querystring from code. How do I find it to change it?

like image 288
dsteele Avatar asked Aug 03 '09 22:08

dsteele


3 Answers

If you add an id and a runat="server" attribute to your link...

<a id="YourLink" runat="server" href="../WOPages/WO_Main.aspx?WONum=12345">
    WorkOrder
</a>

...then you can access/change the HRef property programmatically...

YourLink.HRef = "http://stackoverflow.com/";
like image 154
LukeH Avatar answered Nov 06 '22 11:11

LukeH


You could clear all controls from the ContentPlaceholder and then add a new hyperlink control like this:

// Create your hyperlink control
HyperLink lnk = new HyperLink();
lnk.NavigateUrl = "http://domain.com";
lnk.Text = "Click here";

ContentPlaceHolder3.Controls.Clear();
ContentPlaceHolder3.Controls.Add(lnk);

or give the hyperlink an Id and update the hyperlink by finding the control in the ContentPlaceholder:

HyperLink lnk = ContentPlaceHolder3.FindControl("MyLink") as HyperLink;
lnk.NavigateUrl = "http://domain.com/update/";
lnk.Text = "Click here too";
like image 33
Ben Griswold Avatar answered Nov 06 '22 10:11

Ben Griswold


You could use render tags or do this:

<a href="<asp:literal id="hrefString" runat="server"></asp:literal>"

and assign the literal in code.

like image 33
Steve Avatar answered Nov 06 '22 09:11

Steve