使用DataNavigateUrlFormatString和&符的Gridview超链接字段 [英] Gridview Hyperlinkfield using DataNavigateUrlFormatString with Ampersand

查看:679
本文介绍了使用DataNavigateUrlFormatString和&符的Gridview超链接字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有gridview从表中提取字段,而我的超链接字段用于转到该行的特定页面以获取更详细的数据.除非在超链接字段中使用的字段带有&"符号,否则一切似乎都工作良好.我认为它是在读取&符,因此它不会显示正确的信息,因为&符在数据库的名称中.

So I have gridview pulling fields from a table and my hyperlinkfield is used to go to a specific page for that row to get more detailed data. Everything seems to work great except when the field used in the hyperlinkfield has an ampersand. I assume it is reading the ampersand as something else and so it doesn't bring up the proper info because the ampersand is in the name in the database.

超链接域代码:

<asp:HyperLinkField HeaderText="Name" Text="{0}" DataNavigateUrlFields="Name" DataNavigateUrlFormatString="item.aspx?name={0}" DataTextField="Name" />

示例:

单击名称测试项"将带您到mysite.com/item.aspx?name=test%20item,并且可以使用.

A clicking on the name "test item" would take you to mysite.com/item.aspx?name=test%20item and this works.

但是,单击测试与测试项目"会将您带到mysite.com/item.aspx?name=test%20item%20&%20item,这是行不通的.它只会拉出一个空白信息的页面.

However, clicking on "test & test item" it takes you to mysite.com/item.aspx?name=test%20item%20&%20item which does not work. It just pulls up a page with blank info.

该如何解决?

更新:

我将超链接字段转换为模板字段内的超链接,但是该网址现在变得很奇怪.该网址现在像mysite.com/item.aspx?name=System.Int32%5b%5d

I converted the hyperlinkfield to a hyperlink inside a template field, but the url is now coming out weird.the url now comes out like mysite.com/item.aspx?name=System.Int32%5b%5d

<asp:TemplateField HeaderText="Name">
                      <ItemTemplate>
                <asp:HyperLink runat="server" Text='<%#Eval("Name") %>' DataNavigateUrlFields="Name" NavigateUrl='<%# "name.aspx?name=" + HttpUtility.UrlEncode({0}.ToString())%>'  DataTextField="Name" />
                          </ItemTemplate>
                  </asp:TemplateField>

推荐答案

我的原始答案不完整(由于无法看到完整的代码).该答案将包含缺少的元素.

My original answer was incomplete (due to not being able to see the entire code). This answer will contain the missing elements.

主要对象是

The main object is to UrlEncode the data field Name, so that it can be used as (part of) a url link .

1 -首先,我们必须确保将 GridView 字段" Name "列为 DataKeyNames >如下:

1 - First we must ensure that the field "Name", is listed as DataKeyNames for the GridView as follows:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="Name"  ...

2 -其次(如果使用 navigateURL )创建一个TemplateField,即( asp:TemplateField )并使用 Eval()方法与 UrlEncode()一起访问DataField.

2 - Secondly (if using a navigateURL) create a TemplateField i.e. (asp:TemplateField ) and use Eval() method to access the DataField in conjunction with UrlEncode() .

<asp:TemplateField HeaderText="Name">
    <ItemTemplate>
            <asp:HyperLink ID="NameLink" runat="server" Text='<%# Eval("Name") %>' NavigateUrl='<%# "name.aspx?name=" + HttpUtility.UrlEncode(Eval("Name").ToString())%>'  ></asp:HyperLink>
    </ItemTemplate>
</asp:TemplateField>

选项2 或者,您可以使用HyperLinkField和DataNavigateURL,但仍应将 Name 指定为 DataKeyNames .

Option 2 Alternatively you can use a HyperLinkField and DataNavigateURL but you should still designate Name as a DataKeyNames .

希望这没问题.让我知道是否需要澄清.

Hope this works with no problems. Let me know if any clarification is needed.

快乐的编码和欢呼声,

这篇关于使用DataNavigateUrlFormatString和&符的Gridview超链接字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆