Go to an external site if it is a hyperlink

I have a "view" link to an aspx page in a gridview for each row.

Depending on the type of resource 1) File or 2) Hyperlink, it must either download the file or go to the mentioned hyperlink.

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

I have earned for the file type, but how to redirect to an external link, for example, "www.yahoo.com", if it is a hyperlink.

In code <

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

EDIT: It is shown that the link must have the http: // prefix for it to work. Feeling stupid now :)

+3
source share
3 answers

I understood that the link must have a prefix http://for it to work.

+6
source

URL-, ? , Response.Redirect.

if(resource.ResourceType.ToLower().Equals("hyperlink")){
   Response.Redirect(resource.Url);
}
+3

Add the asp.net hyperlink to the element template. Then handle the RowDataBound event to dynamically change the NavigateURL property of the hyperlink. This way you avoid postpacks.

<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]: Example MSDN

0
source

Source: https://habr.com/ru/post/1715757/