Bind name in gridview through linkbutton or hyperlink
I want to bind name in gridview through linkbutton or hyperlink.. when I click on hyperlink it redirects to an another form and data had to be fetched from sql server regarding this name of id and assingned data into appropriate controls in that form..
Satyapriya NayakPosted Feb 24, 2012, 10:20 PM
C sharp conversion code 2nd part
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test3.aspx.cs" Inherits="Display_related_records_gridview.test3" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Display_related_records_gridview
{
public partial class test3 : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
}
private void bindgrid()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
SqlDataReader reader;
reader = com.ExecuteReader();
g1.DataSource = reader;
g1.DataBind();
con.Close();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test4.aspx.cs" Inherits="Display_related_records_gridview.test4" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Display_related_records_gridview
{
public partial class test4 : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
string s1;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
s1 = Request.QueryString[0];
str = "select * from employee where empid='" + s1 + "'";
com = new SqlCommand(str, con);
SqlDataReader reader;
reader = com.ExecuteReader();
if (reader.Read())
{
Label1.Text = reader["empname"].ToString();
Label2.Text = reader["empaddress"].ToString();
Label3.Text = reader["empsal"].ToString();
Label4.Text = reader["empphone"].ToString();
Label5.Text = reader["empfax"].ToString();
Label6.Text = reader["empcity"].ToString();
Label7.Text = reader["empstate"].ToString();
Label8.Text = reader["empzip"].ToString();
Label9.Text = reader["emplic"].ToString();
Label10.Text = reader["empstatus"].ToString();
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
Thanks
If this post helps you mark it as answer
Satyapriya NayakPosted Feb 24, 2012, 6:12 AM
C sharp conversion code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test1.aspx.cs" Inherits="Display_related_records_gridview.test1" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Display_related_records_gridview
{
public partial class test1 : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
}
private void bindgrid()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
SqlDataReader reader;
reader = com.ExecuteReader();
g1.DataSource = reader;
g1.DataBind();
con.Close();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test2.aspx.cs" Inherits="Display_related_records_gridview.test2" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Display_related_records_gridview
{
public partial class test2 : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
string s1;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
try
{
con.Open();
s1 = Request.QueryString[0];
str = "select * from employee where empname='" + s1 + "'";
com = new SqlCommand(str, con);
g2.DataSource = com.ExecuteReader();
g2.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
con.Close();
}
}
}
Thanks
If this post helps you mark it as answer
vasan rajuPosted Feb 24, 2012, 5:14 AM
Satyapriya NayakPosted Feb 24, 2012, 4:41 AM
Try this...
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
Refer the links
http://www.vbdotnetheaven.com/uploadfile/satyapriyanayak/display-related-records-through-link-in-gridview-using-vb-ne/default.aspx
http://www.vbdotnetheaven.com/uploadfile/satyapriyanayak/display-related-records-through-link-in-gridview-using-vb-ne/default.aspx
Thanks
If this post helps you mark it as answer
SenthilkumarPosted Feb 24, 2012, 3:00 AM
Assign the Text to the LinkButton or HyperLink.
In the ItemDataBound, you have to write the following.
Either you can write the ItemCommand or ItemDataBound. But i suggest you to write the javascript stuff in itemdatabound.
LinkButton lbtnName = (LinkButton) e.Item.FindControl(lbtnName");
lbtnName.Attributes.Add("onclick","window.location.href=Names.aspx?Name="+lbtnName.Text.Trim());
This will do at the time of data bind and after rendering this page, when click on the linkbutton it will redirect to the other page.
There you can get the name from the query string and you can manipulate the data from the sql server.