I have a gridview generated from code behind. I have a link column. when i click to link column i want redirect to another page.
Thanks,
MB.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Sep 11, 2012, 10:49 AM
Using asp:HyperLink Control
test1. aspx code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test1.aspx.vb" Inherits="test1" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Pagetitle>
head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="g1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="Employee name" DataField="empname"/>
<asp:BoundField HeaderText="Employee Salary" DataField="empsal"/>
<asp:BoundField HeaderText="Employee Address" DataField="empaddress"/>
<asp:HyperLinkField HeaderText="View Details Information of Employee" DataNavigateUrlFields="empid" DataNavigateUrlFormatString="test2.aspx?details={0}" Text="Employee Details"/>
Columns>
<HeaderStyle ForeColor="#FF0066" />
<AlternatingRowStyle ForeColor="#CC6600" />
asp:GridView>
form>
body>
html>
test1.aspx.vb code
Imports System.Data
Imports System.Data.SqlClient
Partial Class test1
Inherits System.Web.UI.Page
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
Dim con As New SqlConnection(strConnString)
Dim str As String
Dim com As SqlCommand
Dim sqlda As SqlDataAdapter
Dim ds As DataSet
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
bind()
End Sub
Sub bind()
con.Open()
str = "select * from employee"
com = New SqlCommand(str, con)
Dim reader As SqlDataReader
reader = com.ExecuteReader()
g1.DataSource = reader
g1.DataBind()
con.Close()
End Sub
End Class
test2.aspx code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test2.aspx.vb" Inherits="test2" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Pagetitle>
head>
<body>
<form id="form1" runat="server">
<div>
<table border="1" style="border-collapse: collapse" cellspacing="1">
<tr>
<td width="77" height="16" align="left" ><b><font size="2" color="red">Empname:font>b>td>
<td width="77" height="16" align="left" ><b><font size="2"> <asp:Label ID="Label1" runat="server" Font-Bold="True">asp:Label><br />font>b>td>
tr>
<tr>
<td width="77" height="16" align="left" ><b><font size="2" color="red">Empaddress:font>b>td>
<td width="77" height="16" align="left" ><b><font size="2"> <asp:Label ID="Label2" runat="server" Font-Bold="True">asp:Label><br />font>b>td>
tr>
<tr>
<td width="77" height="16" align="left" ><b><font size="2" color="red">Empsal:font>b>td>
<td width="77" height="16" align="left" ><b><font size="2"> <asp:Label ID="Label3" runat="server" Font-Bold="True">asp:Label><br />font>b>td>
tr>
<tr>
<td width="77" height="16" align="left" ><b><font size="2" color="red">Empphone:font>b>td>
<td width="77" height="16" align="left" ><b><font size="2"> <asp:Label ID="Label4" runat="server" Font-Bold="True">asp:Label><br />font>b>td>
tr>
<tr>
<td width="77" height="16" align="left" ><b><font size="2" color="red">Empfax:font>b>td>
<td width="77" height="16" align="left" ><b><font size="2"> <asp:Label ID="Label5" runat="server" Font-Bold="True">asp:Label><br />font>b>td>
tr>
<tr>
<td width="77" height="16" align="left" ><b><font size="2" color="red">Empcity:font>b>td>
<td width="77" height="16" align="left" ><b><font size="2"> <asp:Label ID="Label6" runat="server" Font-Bold="True">asp:Label><br />font>b>td>
tr>
<tr>
<td width="77" height="16" align="left" ><b><font size="2" color="red">Empstate:font>b>td>
<td width="77" height="16" align="left" ><b><font size="2"> <asp:Label ID="Label7" runat="server" Font-Bold="True">asp:Label><br />font>b>td>
tr>
<tr>
<td width="77" height="16" align="left" ><b><font size="2" color="red">Empzip:font>b>td>
<td width="77" height="16" align="left" ><b><font size="2"> <asp:Label ID="Label8" runat="server" Font-Bold="True">asp:Label><br />font>b>td>
tr>
<tr>
<td width="77" height="16" align="left" ><b><font size="2" color="red">Emplic:font>b>td>
<td width="77" height="16" align="left" ><b><font size="2"> <asp:Label ID="Label9" runat="server" Font-Bold="True">asp:Label><br />font>b>td>
tr>
<tr>
<td width="77" height="16" align="left" ><b><font size="2" color="red">Empstatus:font>b>td>
<td width="77" height="16" align="left" ><b><font size="2"> <asp:Label ID="Label10" runat="server" Font-Bold="True">asp:Label><br />font>b>td>
tr>
table>
div>
form>
body>
html>
test2.aspx.vb code
Imports System.Data
Imports System.Data.SqlClient
Partial Class test2
Inherits System.Web.UI.Page
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
Dim con As New SqlConnection(strConnString)
Dim str As String
Dim com As SqlCommand
Dim sqlda As SqlDataAdapter
Dim ds As DataSet
Dim s1 As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
con.Open()
s1 = Request.QueryString(0)
str = "select * from employee where empid='" + s1 + "'"
com = New SqlCommand(Str, con)
Dim reader As SqlDataReader
reader = com.ExecuteReader
If reader.Read Then
Label1.Text = reader("empname")
Label2.Text = reader("empaddress")
Label3.Text = reader("empsal")
Label4.Text = reader("empphone")
Label5.Text = reader("empfax")
Label6.Text = reader("empcity")
Label7.Text = reader("empstate")
Label8.Text = reader("empzip")
Label9.Text = reader("emplic")
Label10.Text = reader("empstatus")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
Thanks
Sukesh MarlaPosted Sep 11, 2012, 8:12 AM
use TemplateField and in ItemTemplate add asp:HyperLink Control.
Check this is correct answer if it helped
Pradip PandeyPosted Sep 11, 2012, 7:51 AM
Hope it will help.