Passing Data From One Page To Another using QueryString.
Page 1 .aspx page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How To Pass Data From One Page To Another Using Query String </title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
text-align: right;
}
.style3
{
text-align: left;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center">
<table class="style1">
<tr>
<td class="style2">
ID
</td>
<td class="style3">
<asp:TextBox ID="txtid" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Fname
</td>
<td class="style3">
<asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Lname
</td>
<td class="style3">
<asp:TextBox ID="txtlname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Gender
</td>
<td class="style3">
<asp:RadioButtonList ID="rdbgender" runat="server"RepeatDirection="Horizontal">
<asp:ListItem Text="Male" />
<asp:ListItem Text="Female" />
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="style2">
City
</td>
<td class="style3">
<asp:DropDownList ID="ddlcity" runat="server">
<asp:ListItem Text="Select" />
<asp:ListItem Text="Mumbai" />
<asp:ListItem Text="Delhi" />
<asp:ListItem Text="Chennai" />
<asp:ListItem Text="Kolkata" />
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
<asp:Button ID="btnRedirect" runat="server"Text="Redirect To Page 2" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Page 1 .cs File :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ViewState : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnRedirect_Click(object sender, EventArgs e)
{
// Here I M Passing Data From Page 1.aspx to Page 2.aspx
Response.Redirect("Page 2.aspx?id=" + txtid.Text.Trim() +"&fname=" + txtfname.Text.Trim() + "&lname=" + txtlname.Text.Trim() +"&gender=" + rdbgender.SelectedItem.Text + "&city=" + ddlcity.SelectedItem.Text);
}
}
Here I M Passing data
Page 2 .aspx Page :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4"ForeColor="#333333"
GridLines="None" Height="112px" style="text-align: center"Width="781px">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True"ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True"ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White"HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True"ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1"/>
<SortedDescendingCellStyle BackColor="#E9EBEF"/>
<SortedDescendingHeaderStyle BackColor="#4870BE"/>
</asp:GridView>
</div>
</form>
</body>
</html>
Page 2 .cs File :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Page_2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Here I M Fetching Data from Url of Page 2 into the variables(id,fname,lname,gender,city)
// through Request.QueryString
string id = Request.QueryString["id"];
string fname = Request.QueryString["fname"];
string lname = Request.QueryString["lname"];
string gender= Request.QueryString["gender"];
string city= Request.QueryString["city"];
// After taking value from variable I need to Show In Grid,
// So,I first need to create table,and that can be done DataTable Class
// So to use DataTable Class, you need to Include
//using System.Data; namespace
DataTable dt = new DataTable();
//After adding DataTable, Below I Need to add a column
dt.Columns.Add("Id");
dt.Columns.Add("First Name");
dt.Columns.Add("Last Name");
dt.Columns.Add("Gender");
dt.Columns.Add("City");
//Now here I M adding Rows
dt.Rows.Add(id, fname, lname, gender, city);
//Here GridView1 DataSource is dt;
GridView1.DataSource = dt;
//After taking Values Bind It To the Grid
GridView1.DataBind();
}
}
The Final Output Will Be Like This :
No comments:
Post a Comment