Set ASP.Net HyperLink NavigateUrl from Database Column using C# and VB.Net

finestkatch4u
 
on Jul 24, 2022 10:57 PM
367 Views

Setting hyperlink NavigateUrl from SQL column

<asp:HyperLink runat="server" CssClass="LinkNauha" Target="_blank" ID="link" Text='<%# Eval("title") %>' NavigateUrl='<%# Eval("url") %>'></asp:HyperLink>

NavigateUrl link comes from SQL Table column ('<%# Eval("url") %>').

For Example: url = /myfolder/youfolder/hello.mp3

I want url be like: http://mywebsite.com/myfolder/youfolder/hello.mp3 how do add http://mywebsite.com/ to

<%# Eval("url") %>

This mp3 file is stored in main domain and I am trying to access it from Sub Domain.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
KasimA
 
on Jul 25, 2022 03:15 AM

Hi finestkatch4u,

Use string.Format in the NavigateUrl to specify the main domain.

HTML

<asp:GridView ID="gvCustomer" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="Url">
            <ItemTemplate>
                <asp:HyperLink runat="server" CssClass="LinkNauha" Target="_blank" ID="link"
                    Text='<%# Eval("title") %>' NavigateUrl='<%# string.Format("http://mywebsite.com{0}", Eval("url")) %>'></asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Namespace

C#

using System.Data;

VB.Net

using System.Data;

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[2] {
            new DataColumn("title"),
            new DataColumn("Url")});
        dt.Rows.Add("hello", "/myfolder/youfolder/hello.mp3");
        gvCustomer.DataSource = dt;
        gvCustomer.DataBind();
    }
}

VB.Net

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then

        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn(1) {New DataColumn("title"), New DataColumn("Url")})
        dt.Rows.Add("hello", "/myfolder/youfolder/hello.mp3")
        gvCustomer.DataSource = dt
        gvCustomer.DataBind()
    End If
End Sub

Screenshot