'How to assign href to anchor tag in codebehind c#
The text I inserted in database is
You also have to click on is <a href="" target="_blank"> link </a>
This text I am assigning to the label when page loads. My requirement is when I click the "link" I need to redirect to certain page. How can I set the href to the above code in code behind .
Solution 1:[1]
Try to use HyperLink.
<asp:HyperLink id="hyperlink1"
ImageUrl="images/pict.jpg"
NavigateUrl="http://www.microsoft.com"
Text="Microsoft Official Site"
Target="_new"
runat="server"/>
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlink.aspx
Solution 2:[2]
You should add runat="server" to your anchor and then give an ID to it. So you can edit href property in codebehind.
in html side:
in code behind: xxxxx.HRef = "bla bla"
Look at this: How do you set href attribute of anchor tag that is within a repeater in code behind?
Solution 3:[3]
Assuming you can slightly change the format of what you put in the database then I'd do something along these lines:
string labelFromDatabase="You also have to click on is <a href=\"{0}\" target=\"_blank\"> link </a>";
string url = "mypage.aspx";
myLabel.Text = String.Format(labelFromDatabase, url);
Adding in the {0} placeholder into the database held string means you can easily just use String.Format to put in whatever url you want.
Main things to be aware of are that putting { or } in the DB string will need special care (since they are special characters when you pass it into String.Format. Also you will of course need to make sure that url is appropriately escaped if necessary (but that is the case with all solutions).
Solution 4:[4]
Try this
string Myurl="index.aspx";
label1.Text = "You also have to click on is <a href=" + Myurl+ " target="_blank"> link </a>
Solution 5:[5]
you need to store your string in database with some formate like this...
"You also have to click on is <a href='{0}' target='_blank'> link </a>"
and when you assign that text to your lable at that time use string.formate method to add URL to href like this...
//get your database string
string _samplestring ="You also have to click on is <a href='{0}' target='_blank'> link </a>";
string _url ="http://stackoverflow.com/";
lbl.Text = string.Format(_samplestring, _url);
if you also need to assign something to target at run time then store your string like this..
"You also have to click on is <a href='{0}' target='{1}'> link </a>"
and use it like this...
//get your database string
string _samplestring ="You also have to click on is <a href='{0}' target='{1}'> link </a>";
string _url ="http://stackoverflow.com/";
string _target = "_blank";
lbl.Text = string.Format(_samplestring, _url,_target);
Solution 6:[6]
Use the following code for assigning string to href in anchor tag which is created in code behind
string strstring = "../master/YourPage.aspx?TransID="+ dr["TransId"];
Assign this string to url
marqueeText += "<a href='"+strstring+"'" + <span style='color:red;font-weight:bold;font-size:16px'>"
+ Convert.ToString(dr["SocietyName"]) + "</span></a>";
Hope this will help you.
Solution 7:[7]
simplest workaround is to add 'runat' attribute with 'server' value, and set Target and Href properties.
//in design
<a id="aBack" runat="server">
<img src="backarrow.png" style="cursor: pointer;" width="32px" height="32px" />
</a>
//in Code behind C#
aBack.Target = "_parent";//to target parent page from iframe
aBack.HRef = url;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | halit |
| Solution 2 | Community |
| Solution 3 | Chris |
| Solution 4 | sajanyamaha |
| Solution 5 | Tejas Vaishnav |
| Solution 6 | Priyanka Thakur |
| Solution 7 | sun wells |
