Display link (Url) on Web Page on ASP.Net DataList LinkButton Click using C# and VB.Net

Last Reply 3 months ago By pandeyism

Posted 3 months ago

how to show datalist value url in page while click on linkbutton

I want call all path value which are saved in database, not to define folder like "("~/Uploads/{0}", lnk.Text)". 

Posted 3 months ago Modified on 2 months ago

Hi indradeo,

Refer below sample.

Database
This article makes use of a table named Files whose schema is defined as follows.

Upload and Display Images using Path stored in database in GridView in ASP.Net using C# and VB.Net

HTML

<asp:DataList ID="DataList1" runat="server">
    <ItemTemplate>
        <asp:LinkButton Text='<%# Eval("Path") %>' runat="server" OnClick="OpenFrame" />
    </ItemTemplate>
</asp:DataList>
<br />
<asp:Literal ID="ltEmbed" runat="server" />

Namespaces

C#

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data.SqlClient
Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT TOP 4 Id, Name, Path from tblfilespath", con))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    DataList1.DataSource = dt;
                    DataList1.DataBind();
                }
            }
        }
    }
}

protected void OpenFrame(object sender, EventArgs e)
{
    LinkButton lnk = (sender) as LinkButton;
    string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"500px\" height=\"300px\">";
    embed += "If you are unable to view file, you can download from <a href = \"{0}\">here</a>";
    embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
    embed += "</object>";
    ltEmbed.Text = string.Format(embed, lnk.Text);
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As SqlConnection = New SqlConnection(constr)
            Using cmd As SqlCommand = New SqlCommand("SELECT Id, Name, Path from tblfilespath", con)
                Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
                    Dim dt As DataTable = New DataTable()
                    da.Fill(dt)
                    DataList1.DataSource = dt
                    DataList1.DataBind()
                End Using
            End Using
        End Using
    End If
End Sub

Protected Sub OpenFrame(ByVal sender As Object, ByVal e As EventArgs)
    Dim lnk As LinkButton = TryCast((sender), LinkButton)
    Dim embed As String = "<object data=""{0}"" type=""application/pdf"" width=""500px"" height=""300px"">"
    embed += "If you are unable to view file, you can download from <a href = ""{0}"">here</a>"
    embed += " or download <a target = ""_blank"" href = ""http://get.adobe.com/reader/"">Adobe PDF Reader</a> to view the file."
    embed += "</object>"
    ltEmbed.Text = String.Format(embed, lnk.Text)
End Sub

Screenshot