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

In this blog we will know when we select any item from the dropdownlist its related values will be displayed in the other webpage.

 

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>

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

 

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();

                    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);

            }

        }

    }

}