如何在代码隐藏 C# 中将 href 分配给锚标记
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14052532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to assign href to anchor tag in codebehind c#
提问by lakki
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 .
我在页面加载时分配给标签的文本。我的要求是当我点击“链接”时,我需要重定向到某个页面。如何在后面的代码中将 href 设置为上述代码。
回答by sajanyamaha
Try this
尝试这个
string Myurl="index.aspx";
label1.Text = "You also have to click on is <a href=" + Myurl+ " target="_blank"> link </a>
回答by Chris
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.
将{0}
占位符添加到数据库保存的字符串中意味着您可以轻松地使用它String.Format
来放入您想要的任何 url。
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).
需要注意的主要事项是放入{
或}
放入 DB 字符串需要特别小心(因为它们是特殊字符,当您将其传递到String.Format
.所有解决方案的情况)。
回答by halit
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
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlink.aspx
回答by Tejas Vaishnav
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...
当您将该文本分配给您的标签时,请使用 string.formate 方法将 URL 添加到 href 像这样...
//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);
回答by Priyanka Thakur
Use the following code for assigning string to href in anchor tag which is created in code behind
使用以下代码将字符串分配给在后面的代码中创建的锚标记中的 href
string strstring = "../master/YourPage.aspx?TransID="+ dr["TransId"];
string strstring = "../master/YourPage.aspx?TransID="+ dr["TransId"];
Assign this string to url
将此字符串分配给 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.
希望这会帮助你。
回答by huseyind
You should add runat="server" to your anchor and then give an ID to it. So you can edit href property in codebehind.
您应该将 runat="server" 添加到您的锚点,然后为其提供一个 ID。因此,您可以在代码隐藏中编辑 href 属性。
in html side:
在 html 端:
in code behind: xxxxx.HRef = "bla bla"
在后面的代码中:xxxxx.HRef = "bla bla"
Look at this: How do you set href attribute of anchor tag that is within a repeater in code behind?