Select any item from the dropdownlist its related values will be displayed in the other webpage.

Satyapriyanayak
Posted by Satyapriyanayak under ASP.NET category on | Points: 40 | Views : 653
test1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test1.aspx.cs" Inherits="Dropdownlist_querystring.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 Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</div>
</form>
</body>
</html>

test1.aspx.cs
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 Dropdownlist_querystring
{
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)
{
binddrop();
}
}
private void binddrop()
{

DropDownList1.AutoPostBack = true;
SqlConnection con = new SqlConnection(strConnString);

if (!IsPostBack)
{
DropDownList1.Items.Add("Choose empid");
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
DropDownList1.Items.Add(reader["empid"].ToString());
}
reader.Close();
con.Close();
}
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("test2.aspx?empid=" + DropDownList1.SelectedItem.Text + "");
}
}
}


test2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test2.aspx.cs" Inherits="Dropdownlist_querystring.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 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>
</table>
</div>
</form>
</body>
</html>

test2.aspx.cs
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 Dropdownlist_querystring
{
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)
{
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();
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}

Comments or Responses

Login to post response