5

我在数据库中插入的文本是

 You also have to click on is <a href="" target="_blank"> link </a>

我在页面加载时分配给标签的文本。我的要求是当我单击“链接”时,我需要重定向到某个页面。如何在后面的代码中将 href 设置为上述代码。

4

6 回答 6

9

尝试使用超链接。

 <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

于 2012-12-27T10:05:00.400 回答
3

您应该将 runat="server" 添加到您的锚点,然后给它一个 ID。因此,您可以在代码隐藏中编辑 href 属性。

在html方面:

在后面的代码中:xxxxx.HRef = "bla bla"

看看这个: 你如何在后面的代码中设置转发器内的锚标记的 href 属性?

于 2016-02-05T23:13:32.217 回答
2

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

于 2012-12-27T10:03:13.907 回答
0

尝试这个

string Myurl="index.aspx";
label1.Text = "You also have to click on is <a href=" + Myurl+ " target="_blank"> link </a>
于 2012-12-27T09:51:33.217 回答
0

使用以下代码将字符串分配给在后面的代码中创建的锚标记中的 href

字符串 strstring = "../master/YourPage.aspx?TransID="+ dr["TransId"];

将此字符串分配给 url

marqueeText += "<a href='"+strstring+"'" + <span style='color:red;font-weight:bold;font-size:16px'>"
+ Convert.ToString(dr["SocietyName"]) + "</span></a>";

希望这会帮助你。

于 2015-04-22T07:54:09.787 回答
0

你需要用这样的格式将你的字符串存储在数据库中......

 "You also have to click on is <a href='{0}' target='_blank'> link </a>"

并且当您当时将该文本分配给您的标签时,使用 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);

如果您还需要在运行时为目标分配一些东西,那么像这样存储您的字符串..

"You also have to click on is <a href='{0}' target='{1}'> link </a>"

并像这样使用它...

//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);
于 2012-12-27T10:16:46.980 回答