代码之家  ›  专栏  ›  技术社区  ›  krishna

如果是超链接,请转到外部网站

  •  3
  • krishna  · 技术社区  · 16 年前

    我有一个“视图”链接,指向网格视图中每一行的ASPX页。

    根据资源1)文件或2)超链接的类型,它应该下载该文件或转到提到的超链接。

    <asp:TemplateField HeaderText="View">
                    <ItemTemplate>
                        <a id="View" href="../resources/ResourceFile.aspx?Id=<%# Eval("Id")%>" target="_blank">View</a>
                    </ItemTemplate>
        </asp:TemplateField>
    

    我把它用于文件类型,但是如果它是超链接,我如何重定向到像“www.yahoo.com”这样的外部链接。

    在后面的代码中

    if(resource.ResourceType.ToLower().Equals("hyperlink")){
                        // what should i do here?
                   // the link is stored in resource.value
                    }
    

    编辑:已确定链接应具有http://prefix才能工作。现在觉得自己很傻:)

    3 回复  |  直到 12 年前
        1
  •  6
  •   Nalaka526 user6239040    12 年前

    已确定链接应具有 http:// 工作的前缀。

        2
  •  3
  •   David R. Longnecker    16 年前

    资源对象是否可以访问要重定向到的URL?如果是这样,您可以使用response.redirect。

    if(resource.ResourceType.ToLower().Equals("hyperlink")){
       Response.Redirect(resource.Url);
    }
    
        3
  •  0
  •   Billy Coover    16 年前

    向项模板添加ASP.NET超链接。然后处理rowdatabound事件以动态更改超链接的navigateURL属性。这样就避免了延迟。

    <asp:TemplateField HeaderText="View">
        <ItemTemplate>
            <asp:Hyperlink runat="server" id="View" target="_blank">View</a>
        </ItemTemplate>
    </asp:TemplateField>
    
    void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink hl = (HyperLink)e.Row.FindControl("View");
            hl.NavigateUrl = "Link to file or url based on resource type";
        }
    }
    

    [ http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx][1]

    [1]:msdn示例

    推荐文章