4
Answers

hyperlink in Gridview to Redirect values to another Page

Photo of Aamir Khan

Aamir Khan

12y
3.2k
1
Dear Friends,
i have a requirement, there is an Web form named Form2 in which there is gridview
the gridview contains columns(kit Name,Kit Type,Kit Units).
the following values comes in Gridview Form from a Dropdown value 'CTNO'.
which is been field in a Previous Form named Form1 in Application.
I want a link in Form2 Gridview, which will redirect all the details to Form1 in readONly format.
how can it be done!!!

pl reply.
aamir

Answers (4)

1
Photo of Jignesh Trivedi
NA 61.3k 14.2m 12y
hi aamir,

As per my understanding you que is "you have two aspx pages called a.aspx and b.aspx, and a.aspx containing gridview with hyperlink, if any one click on that hyperlink than system will redirect to b.aspx and b.aspx also contain grid view".

If i am right that follow step given below.
1) A.aspx - create grid view with column hyperlink.
        <asp:GridView runat="server" ID="GridView1" ShowFooter="true" AutoGenerateColumns="true"> 
  <Columns>
     <asp:HyperLinkField DataTextField="sname" DataNavigateUrlFields="sid" DataNavigateUrlFormatString="b.aspx?Id={0}"
     ....
  </Columns>
...
...
</asp:GridView>

here "DataNavigateUrlFields" pass property name of Id and DataTextField pass property name of Display name and DataNavigateUrlFormatString pass url.
2) now on page load of b.aspx you write your grid bind logic.
3) you can get value selected value of grid row by Request.QueryString["Id"]

hope this will help you.
1
Photo of Satyapriya Nayak
NA 53k 8m 12y
Hi Aamir,


Try this...


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test3.aspx.cs" Inherits="Display_related_records_gridview.test3" %>

<!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 Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <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="test4.aspx?details={0}" Text="Employee Details"/>
    </Columns>
        <HeaderStyle ForeColor="#FF0066" />
        <AlternatingRowStyle ForeColor="#CC6600" />
    </asp:GridView>
    </div>
    </form>
</body>
</html>



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" %>

<!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 Page</title>
</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">&nbsp;<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">&nbsp;<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">&nbsp;<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">&nbsp;<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">&nbsp;<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">&nbsp;<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">&nbsp;<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">&nbsp;<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">&nbsp;<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">&nbsp;<asp:Label ID="Label10" runat="server" Font-Bold="True"></asp:Label><br /></font></b></td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>




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
0
Photo of Aamir Khan
NA 273 311.9k 12y
Thank you Satyapriya & jignesh for their helpful reply. Regards, Aamir
0
Photo of Aamir Khan
NA 273 311.9k 12y
Dear Friends,
how to fill a gridview on selection of a Hyperlink in Gridview of another Form.

Regards,
aamir