加载到我的gridview中的链接尝试导航到我的本地服务器 [英] Link loaded into my gridview try to navigate to my local server

查看:70
本文介绍了加载到我的gridview中的链接尝试导航到我的本地服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将链接加载到gridview中,但是如果未在http://后面附加链接,则会将其发送到我的服务器.因此,单击时类似www.yahoo.com之类的内容将转到 http://localhost :1304/.../controls/www.yahoo.com .除了进行字符串操作外,单击该链接时如何使浏览器打开链接字段中任何内容的新窗口.

I'm loading links into a gridview, but if they aren't appended with http:// it goes to my server. So something like www.yahoo.com when clicked would go to http://localhost:1304/.../controls/www.yahoo.com. How would I make the browser open a new window to whatever is in the link field when clicked besides doing string manipulation.

我已经尝试过asp:hyperlinkfield和templatefields

I've tried both asp:hyperlinkfield and templatefields

<asp:TemplateField HeaderText="Link"> 
   <ItemTemplate>
      <asp:HyperLink runat="server" Text='<% #(Eval("Link")) %>' NavigateUrl='<% #Eval("Link") %>' />
   </ItemTemplate>
</asp:TemplateField>

    <%--<asp:HyperLinkField DataTextField="Link" HeaderText="Link" SortExpression="Link" DataNavigateUrlFormatString="{0}" DataNavigateUrlFields="Link" Target="_blank" />--%>

这是页面上的一些资料. comcast链接将自己附加到本地目的地,而yahoo很好.

Here is some source from the page. The comcast link appends itself to a local destination while the yahoo one is fine.

<td>15478963</td><td>test data - comcast</td><td><a href="Controls/www.comcast.net" target="_blank">www.comcast.net</a></td><td align="right">12/23/2009</td><td>Justen</td>
            </tr><tr style="color:Black;background-color:Gainsboro;">
                <td>12345678</td><td>Update works!</td><td><a href="http://www.yahoo.com" target="_blank">http://www.yahoo.com</a></td><td align="right">12/23/2009</td><td>Justen</td>

推荐答案

此行为(将URL附加到本地服务器的末尾)是由浏览器解释URL的方式引起的.例如,要重定向到www.yahoo.com,您必须使用以 http://开头的绝对 URL.

This behavior (appending the url to the end of your local server) is caused by the way URL's are interpreted by browsers. To redirect to www.yahoo.com, for example, you have to use an absolute URL, starting with http://.

如果您的链接指向的地址不是以 http://开头的地址,则浏览器会将其解释为相对于当前地址的相对网址(本地服务器)位置.

If your links are pointing to addresses which don't start with http://, then the browser interprets them as relative urls, in relation to the present (local server) location.

因此,您必须进行一些字符串操作,以检查地址是否以 http://开头,并在必要时添加它.也许您可以在OnRowDataBound GridView的事件处理程序中做到这一点

So you'll have to do some string manipulation, to check whether the address starts with http:// and add it when it's necessary. Maybe you could do it in the OnRowDataBound event handler for the GridView

此外,要使链接在浏览器中打开新窗口(或选项卡),您应使用上一个答案中指出的target="_blank"属性.

Also, to make the links open a new window (or tab) in the browser, you should use the target="_blank" attribute, as pointed out in the previous answer.

这篇关于加载到我的gridview中的链接尝试导航到我的本地服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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