.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

When selected any item from the dropdown list all records related to this will be displayed in a gri

Posted By:satyapriyanayak       Posted Date: October 31, 2013    Points: 40    Category: Visual Studio    URL: http://www.dotnetspark.com  

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



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend