简体   繁体   English

如何将 href 分配给代码隐藏中的锚标记 c#

[英]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.页面加载时,我将此文本分配给 label。 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 设置为上述代码。

Try to use HyperLink. 尝试使用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

You should add runat="server" to your anchor and then give an ID to it. 您应该将runat =“server”添加到锚点,然后为其提供ID。 So you can edit href property in codebehind. 所以你可以在codebehind中编辑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? 看看这个: 如何设置代码背后的转发器内的锚标签的href属性?

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时它们是特殊字符。当然,你也需要确保url被适当地转义,如果必要的(但所有解决方案都是如此)。

Try this 尝试这个

string Myurl="index.aspx";
label1.Text = "You also have to click on is <a href=" + Myurl+ " target="_blank"> link </a>

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);

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. 希望这会帮助你。

simplest workaround is to add 'runat' attribute with 'server' value, and set Target and Href properties.最简单的解决方法是添加带有“server”值的“runat”属性,并设置 Target 和 Href 属性。

//in design //在设计中

<a id="aBack" runat="server">
  <img src="backarrow.png" style="cursor: pointer;" width="32px" height="32px" />
</a>

//in Code behind C# //在后面的代码 C#

aBack.Target = "_parent";//to target parent page from iframe aBack.Target = "_parent";//指向iframe的父页面

aBack.HRef = url; aBack.HRef = url;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM