Select item from dropdownlist all records related to this will be displayed in a grid view without refreshing

In this blog we will know when selected any item from the dropdown list all records related to this will be displayed in a grid view without refreshing the whole webpage.

 

Table Creation

 

Create table Contacts (id varchar(50),name varchar(50),city varchar(50))

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!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:ScriptManager ID="ScriptManager1" runat="server">

    </asp:ScriptManager>

  

        <asp:UpdatePanel ID="up1" runat="server">

           <ContentTemplate >

           <asp:Label ID="Label1" runat="server" Text="Choose Name" BackColor="#FFFF99"

                   ForeColor="Red" Width="100px"></asp:Label>

                  

               <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"

                   onselectedindexchanged="DropDownList1_SelectedIndexChanged" >

               </asp:DropDownList>

        </ContentTemplate>

 

      <Triggers>

       <asp:AsyncPostBackTrigger ControlID ="DropDownList1" />

      </Triggers>

   </asp:UpdatePanel>

        <br />

 

  <asp:UpdatePanel ID="up2" runat="server">     

     <ContentTemplate >

    <asp:GridView ID="GridView1" runat="server">

    </asp:GridView>

   

      

     </ContentTemplate>

     <Triggers >

      <asp:AsyncPostBackTrigger ControlID ="GridView1" />

        </Triggers>

     </asp:UpdatePanel>

        <br />

    </div>

    </form>

</body>

</html>

 

 

using System;

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;

public partial class _Default : System.Web.UI.Page

{

    string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    SqlDataAdapter ad = new SqlDataAdapter();

    SqlCommand cmd = new SqlCommand();

    SqlDataAdapter sqlda;

    DataSet ds;

    string str;

  

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

    {

        SqlConnection conn = new SqlConnection(connStr);

        conn.Open();

        str = "select * from Contacts where name like '" + DropDownList1.SelectedValue + "'";

        cmd = new SqlCommand(str, conn);

        sqlda = new SqlDataAdapter(cmd);

        ds = new DataSet();

        sqlda.Fill(ds, "Contacts");

        conn.Close();

        GridView1.DataSource = ds;

        GridView1.DataMember = "Contacts";

        GridView1.DataBind();

    }

    protected void Page_Load(object sender, EventArgs e)

    {

        SqlConnection con = new SqlConnection(connStr);

        if (!IsPostBack)

        {

            DropDownList1.Items.Add("Choose name");

            con.Open();

            str = "select name from Contacts";

            cmd = new SqlCommand(str, con);

            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())

            {

                DropDownList1.Items.Add(reader["name"].ToString());

            }

            reader.Close();

            con.Close();

        }

    }

}

 

 

 

Ebook Download
View all
Learn
View all