在 gridview asp.net c# 中创建超链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/222988/
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
create hyperlink in gridview asp.net c#
提问by
My code is in c# asp.net 3.5
我的代码在 c# asp.net 3.5 中
In the following code the "Msg" has many words with spaces and characters (eg:Failed to prepare Sync Favorites : Directory does not exist: \STL-FNP-02\ryounes$\Sync\Favorites). This "Msg" is pulled from database to a gridview. I am not able to create hyperlink for this "Msg" in gridview. Since it has spaces it is not creating hyperlink.
在以下代码中,“Msg”有许多带有空格和字符的单词(例如:无法准备同步收藏夹:目录不存在:\STL-FNP-02\ryounes$\Sync\Favorites)。这个“消息”从数据库拉到网格视图。我无法在 gridview 中为此“消息”创建超链接。由于它有空格,因此不会创建超链接。
I need to create hyperlink for this "Msg" and latter use it in linq query.
我需要为此“消息”创建超链接,然后在 linq 查询中使用它。
I think one shud either use eval or url encoder. I am not sure how to do it. Can anyone say how to go about it?
我认为应该使用 eval 或 url 编码器。我不知道该怎么做。任何人都可以说如何去做吗?
<asp:HyperLinkField DataTextField="Msg" HeaderText="Msg" DataNavigateUrlFields="Msg"
DataNavigateUrlFormatString="Sync.aspx?Msg={0}" />
采纳答案by Marcus King
The easiest way to get around it is to use something like
绕过它的最简单方法是使用类似的东西
<asp:TemplateField HeaderText="Msg">
<asp:HyperLink runat="server" Text='<%# HttpUtility.UrlEncode(Eval("Msg")) %>' NavigateUrl='<%#Eval("Msg")%>' />
</asp:TemplateField>
Not 100% certain that the syntax is correct but you get the general idea
不能 100% 确定语法是正确的,但您已大致了解
回答by Mitchel Sellers
It doesn't create the link as it is not a valid URL, rather than using a hyperlink column most likely you are going to need to migrate to a template and manage it yourself, or at minimum do some formatting on it.
它不会创建链接,因为它不是有效的 URL,而不是使用超链接列,您很可能需要迁移到模板并自行管理它,或者至少对其进行一些格式化。
I would be cautious regardless of making that a hyperlink, where is it going to go anway?
无论是否将其设为超链接,我都会保持谨慎,它会去哪里?
If there is a specific place based on message that you should be going, calculate that BEFORE you bind to the grid...
如果根据您应该去的消息有一个特定的地方,请在绑定到网格之前计算...
回答by Rafe
It sounds like you need to url-encode the string so that the words and spaces can exist as one variable in the query string. Take a look at this function: http://msdn.microsoft.com/en-us/library/zttxte6w.aspx
听起来您需要对字符串进行 url 编码,以便单词和空格可以作为查询字符串中的一个变量存在。看看这个函数:http: //msdn.microsoft.com/en-us/library/zttxte6w.aspx