ASP.NET GridView column with hyperlink in VB.NET

Here, we will see a GridView with hyperlink column in VB.NET.
  • 6878
 

Here, we will see a GridView with hyperlink column. The column will show the article title. Upon clicking on the article title, the article title can be pass to another page and display all information related to the article title. A GridView with hyperlink column is very important when you are running a community website where user posts articles, forum messages etc. 

For example

In this example we create a database table with title, description and author name. Title link will be create in the asp .net GridView control. when we click on the title link it will be redirect to the dynamic page.

Create a table in database named as Articletable. Table looks like this.

CREATE TABLE [dbo].[Articletable](

      [ID] [int] NOT NULL,

      [Title] [varchar](200) NULL,

      [Description] [varchar](400) NULL,

      [Author] [varchar](50) NULL

)

Inserting data in the articletable.

INSERT INTO Articletable VALUES(1,'How to validate dropdownlist in asp.net','Here, we will learn how to validate a DropDownList in ASP.NET.','Rohatash Kumar');

GO

INSERT INTO Articletable VALUES(2,'Introduction to .NET Assemblies in VB.NET',' Here is a comprehensive introduction to .NET assemblies.','sunil Kumar');

go

INSERT INTO Articletable VALUES(3,'BinaryReader and BinaryWriter classes in VB.NET','In this article I will explain about BinaryReader and BinaryWriter Classes in VB.NET.','Deepak Kumar');

 

go

INSERT INTO Articletable VALUES(4,'StreamWriter class in VB.NET','This article shows how to create a new text file and write a string to it.','Rohatash Kumar');

go

select * from articletable;

OUTPUT

rewrite1 (1).jpg

Table1

Now in ASP. NET

  1. Open Visual Studio.
  2. Add two webForm to your website, name it Gridview.aspx and dynamicpage.aspx.

Now drag and drop a GridView control from the Toolbox on the gridview.aspx page.

Gridview.aspx

The form looks like this.

111.gif
 

Figure1

.CS code

Imports System.Data.SqlClient

Public Class Gridview

    Inherits System.Web.UI.Page

 

    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgsHandlesMe.Load

        GridView1.DataSource = GetData()

        GridView1.DataBind()

 

    End Sub

    Public Shared Function GenerateURL(ByVal Title As ObjectByVal strId As ObjectAs String

        Dim strTitle As String = Title.ToString()

 

        '#region Generate SEO Friendly URL based on Title

 

        strTitle = strTitle.Trim()

        strTitle = strTitle.Trim("-"c)

 

        strTitle = strTitle.ToLower()

        Dim chars As Char() = "$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray()

        strTitle = strTitle.Replace("c#""C-Sharp")

        strTitle = strTitle.Replace("vb.net""VB-Net")

        strTitle = strTitle.Replace("asp.net""Asp-Net")

        strTitle = strTitle.Replace(".""-")

        For i As Integer = 0 To chars.Length - 1

            Dim strChar As String = chars.GetValue(i).ToString()

            If strTitle.Contains(strChar) Then

                strTitle = strTitle.Replace(strChar, String.Empty)

            End If

        Next

        strTitle = strTitle.Replace(" ""-")

        strTitle = strTitle.Replace("--""-")

        strTitle = strTitle.Replace("---""-")

        strTitle = strTitle.Replace("----""-")

        strTitle = strTitle.Replace("-----""-")

        strTitle = strTitle.Replace("----""-")

        strTitle = strTitle.Replace("---""-")

        strTitle = strTitle.Replace("--""-")

        strTitle = strTitle.Trim()

        strTitle = strTitle.Trim("-"c)

        strTitle = "~/Article/" & strTitle & "-" & Convert.ToString(strId) & ".aspx"

        Return strTitle

    End Function

 

    Private Function GetData() As DataTable

        Dim strConn As String = ("Data Source=.; uid=sa; pwd=Password$2; database=userinfo")

        Dim conn As New SqlConnection(strConn)

        Dim da As New SqlDataAdapter("select Id,Title,Description,author from Articletable", conn)

        Dim ds As New DataSet()

        da.Fill(ds, "MyTestTable")

        Return ds.Tables("MyTestTable")

    End Function

End Class

dynamicpage.aspx

The form looks like this.


 

222.gif
 


 

Figure2

The ASP. NET code for the dynamicpage.aspx page.

<formid="form1"runat="server">

   <div>

       <h1><imgsrc="../Images/article.gif"/>Article</h1>

       <b>Title:</b><asp:LabelID="lblTitle"runat="server"Text="Label"Font-Bold="true"ForeColor="blue"></asp:Label><br/>

        AuthorName<b>:</b><asp:LabelID="lblauthor"runat="server"Text="Label"

           Font-Bold="true"ForeColor="blue"></asp:Label>

       <br/>

       <br/>

       <b>Description:</b><br/>

       <asp:LabelID="lblDescription"runat="server"Text="Label"></asp:Label><br/><br/>

       <br/><br/>

       &nbsp;</div>       

   </form>

.CS code

Imports System.Data.SqlClient

Public Class dynamicpage

    Inherits System.Web.UI.Page

 

    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgsHandlesMe.Load

        If Request.QueryString("MyTitleId"IsNot Nothing Then

            Dim strId As String = Request.QueryString("MyTitleId").ToString()

            DisplayArticle(strId)

        End If

 

    End Sub

    Private Sub DisplayArticle(ByVal strId As String)

        Dim strConn As String = ("Data Source=.; uid=sa; pwd=Password$2; database=userinfo")

        Dim conn As New SqlConnection(strConn)

        Dim da As New SqlDataAdapter("select Id,Title,Description,author from Articletable where Id=" & strId, conn)

        Dim ds As New DataSet()

        da.Fill(ds, "Articletable")

        lblTitle.Text = ds.Tables("Articletable").Rows(0)(1).ToString()

        lblDescription.Text = ds.Tables("Articletable").Rows(0)(2).ToString()

        lblauthor.Text = ds.Tables("Articletable").Rows(0)(3).ToString()

    End Sub

 

End Class

URLRewriter.dll

Now Add the reference of the URLRewriter.dll in the bin folder of the application witch is attached with the download file.

Add a image

Add Image in the images folder of the application that will be display with the title of dynamic page at run time witch is also attached with the download file.

Now run the application and test it.

333.gif
 

Figure3

Now click on the title link. to redirect the dynamic page.

Conclusion:

A GridView with hyperlink column is very useful for working with community website. If there is any mistake in this article then please notify me. I expect your valuable comments and feedback about this article.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.