GridViewOneToAnotherPage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewOneToAnotherPage.aspx.cs" Inherits="GridViewOneToAnotherPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Send Grid View Data From One Page To Another Page</h1>
<p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:CommandField HeaderText="SELECT" ShowSelectButton="True" />
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="LabelId" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="NAME">
<ItemTemplate>
<asp:Label ID="LabelName" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AGE">
<ItemTemplate>
<asp:Label ID="LabelAge" runat="server" Text='<%# Eval("age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="IMAGE">
<ItemTemplate>
<asp:Image ID="Image1" Height="100" Width="100" runat="server" ImageUrl='<%# Eval("image") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="Gray" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
</p>
</div>
</form>
</body>
</html>
GridViewOneToAnotherPage.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class GridViewOneToAnotherPage : System.Web.UI.Page
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
bindGridView();
}
}
void bindGridView()
{
SqlConnection con = new SqlConnection(cs);
string query = "select * from [GridView]";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
DataTable data = new DataTable();
sda.Fill(data);
GridView1.DataSource = data;
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
Label lblid = (Label)row.FindControl("LabelId");//id of lable
Label lblname = (Label)row.FindControl("LabelName");
Label lblage = (Label)row.FindControl("LabelAge");
Image img = (Image)row.FindControl("Image1");//id of image
//It is use for transfer data of query string not image
//Response.Redirect("GridViewOneToAnotherPage1.aspx?id=" +lblid.Text+"&name="+lblname.Text+"&age="+lblage.Text);
Session["id"] = lblid.Text;
Session["name"] = lblname.Text;
Session["age"] = lblage.Text;
Session["img"] = img.ImageUrl.ToString();
Response.Redirect("GridViewOneToAnotherPage1.aspx");
}
}
GridViewOneToAnotherPage1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewOneToAnotherPage1.aspx.cs" Inherits="GridViewOneToAnotherPage1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 100px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Send Grid View Data From One Page To Another Page 1</h1>
<br />
<table class="auto-style1">
<tr>
<td class="auto-style2">ID</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">NAME</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">AGE</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2"> IMAGE</td>
<td>
<asp:Image ID="Image1" Width="100" runat="server" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
GridViewOneToAnotherPage1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class GridViewOneToAnotherPage1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//It is use for query string only
//TextBox1.Text = Request.QueryString["id"];
//TextBox2.Text = Request.QueryString["name"];
//TextBox3.Text = Request.QueryString["age"];
if (Session["id"] != null && Session["name"] != null && Session["age"] != null && Session["img"] != null)
{
TextBox1.Text = Session["id"].ToString();
TextBox2.Text = Session["name"].ToString();
TextBox3.Text = Session["age"].ToString();
Image1.ImageUrl = Session["img"].ToString();
}
else
{
Response.Redirect("GridViewOneToAnotherPage.aspx");
}
}
}
0 Comments