Click here to Skip to main content
12,448,003 members (83,892 online)
Rate this:
 
Please Sign up or sign in to vote.
i hv a button,dropdownlist,and gridview. and i want that when i select any option dropdownlist . then the releted data is show into gridview .i hv a problem that tha data show in gridview but gridview d'nt affect with the data list..
my code is

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Net.Mail;
using System.IO;
using System.ComponentModel;
using System.Drawing;
using System.Web.SessionState;
 
namespace AccountManagement
{
    public partial class Account : System.Web.UI.Page
    {
        SqlConnection con;
       SqlDataAdapter da;
        SqlDataReader dr;
        //DataSet ds1;
       // String Sql;
        //int Count;
        DataTable dt;
        SqlCommand cmd;
        protected void Page_Load(object sender, EventArgs e)
        {
            string s = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            con = new SqlConnection(s);
            if (!IsPostBack)
            {
                bindgrid();
                bindlst();
            }
            con.Open();
        }
 
        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    bindgrid();
        //    GridView1.Visible = true;
            
        //}
        public void bindgrid()
        {
            cmd=new SqlCommand ("select distinct Fname,Lname from Client_registration_tbl",con);
            //ds1 = new DataSet();
            con.Open();
            dr = cmd.ExecuteReader();
            //da.Fill(ds1);
            GridView1.DataSource = dr;
            GridView1.DataBind();
            GridView1.Visible = true;
            con.Close();
 
        }
 
        public void bindlst()
        {
            da = new SqlDataAdapter("select distinct Fname,Lname from Client_registration_tbl", con);
            dt = new DataTable();
            da.Fill(dt);
            DropDownList1.DataSource = dt;
            DropDownList1.DataBind();
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select distinct Fname,Lname from Client_registration_tbl" + DropDownList1.SelectedItem.Text + "", con);
            dt = new DataTable();
            da.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
           // bindgrid();
            //GridView1.Visible = true;
        }
 
      
 

    }
}
Posted 13-Sep-12 19:34pm
Updated 13-Sep-12 21:25pm
v3
Comments
Vani Kulkarni 14-Sep-12 1:42am
   
You have already posted this question here[^].Please do not repost. Update your existing question using Improve Question.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

2 Options:
1. Use Dropdown selectedIndexchange event (with AutoPostback set to true) on server side to find the value selected in the dropdownlist. based on the value selected, get the data from database and display in a grid.
2. Use AJAX to fire a XMLHttpRequest/Callback/PageMethod from client side to server on drodown change and get the data back as a response which can be used to bind with the XML.

Try both and learn on.

Option 1 is straightforward. Option 2 needs knowledge on ways to interact with server form client, read here:
1. XMLHttpRequest[^]
2. Callback[^]
3. WebService call[^]
4. PageMethod[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160811.3 | Last Updated 14 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100