How to show dropdownlist item in another page in a lable by querystring in asp.net
i have a dropdownlist and the no.of items are present.my question is when i select the item ,this item will show in another page in a lable by querystring in asp.net with c# language with example and code?
ranjan sahooPosted Oct 25, 2012, 3:36 AM
Satyapriya NayakPosted Oct 25, 2012, 3:17 AM
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test1.aspx.cs" Inherits="Dropdownlist_querystring.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 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 + "");
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test2.aspx.cs" Inherits="Dropdownlist_querystring.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 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();
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
Alok UniyalPosted Oct 25, 2012, 3:13 AM
In first page:
Code Behind
string item=DDList.SelectedItem.Text;
Responce.Redirect("~/SecondPage?itemname="+item);
In second Page
Code Behind:
string item=Request.QueryString["itemname"];
label1.Text=item;