Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am using three DropDownLists in a page. And I am binding data to DDL1(DropDownList1) on pageload using if(!Page.IsPostBack) condition too. And on DDL1 selectedIndexChanged Event I am binding data to DDL2 and its working fine. But when I try to do the same to DDL3 & DDL2 (Like binding data to DDL2 on SelectedIndexChanged Event of DDL2) always DDL2 selects the first item only if I select random also DDL2 still goes first item. Here All 3 DDLs are AutoPostback-true, Vistate-Enabled.

Here is My Code --
ASPX Code :-
ASP.NET
<form id="form1"  runat="server">
<div>    
    <br />
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" ViewStateMode="Enabled">
        <asp:ListItem Value="0">Select</asp:ListItem>
    </asp:DropDownList>
    <br />
    <br />
    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" ViewStateMode="Enabled">
        <asp:ListItem Value="0">Select</asp:ListItem>
    </asp:DropDownList>
    <br />
    <br />
    <asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True" ViewStateMode="Enabled">
        <asp:ListItem Value="0">Select</asp:ListItem>
    </asp:DropDownList>
    <br />    
</div>
</form>


.CS Code :-
C#
public partial class getcolumns : System.Web.UI.Page
{
private SqlConnection con;
private SqlDataAdapter da;
private DataTable dt;
private DataSet ds;

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        try
        {
            string query = " SELECT name, dbid FROM sys.sysdatabases where dbid > 4 order by name ";
            con = new SqlConnection(ConfigurationManager.AppSettings["godb"].ToString());
            da = new SqlDataAdapter(query, con);
            dt = new DataTable();
            ds = new DataSet();
            da.Fill(dt);
            DropDownList1.DataSource = dt.DefaultView.ToTable(true, "name", "dbid");
            DropDownList1.DataValueField = "dbid";
            DropDownList1.DataTextField = "name";
            DropDownList1.DataBind();
        }
        catch (Exception) { }
    }
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        DropDownList2.Items.Clear();
        string query = " SELECT TABLE_CATALOG, TABLE_NAME FROM " + DropDownList1.SelectedItem + ".INFORMATION_SCHEMA.tables ";
        con = new SqlConnection(ConfigurationManager.AppSettings["godb"].ToString());
        da = new SqlDataAdapter(query, con);
        dt = new DataTable();
        ds = new DataSet();
        da.Fill(ds);
        DropDownList2.DataSource = ds; // dt.DefaultView.ToTable(true, "TABLE_NAME", "TABLE_CATALOG");
        DropDownList2.DataValueField = "TABLE_CATALOG";
        DropDownList2.DataTextField = "TABLE_NAME";
        DropDownList2.DataBind();
        DropDownList2.Items.Insert(0, new ListItem("--Select--", "0"));

        //for (int i = 0; i < dt.Rows.Count; i++)
        //{
        //    DropDownList2.Items.Add(new ListItem(dt.Rows[i]["TABLE_NAME"].ToString(), dt.Rows[i]["TABLE_CATALOG"].ToString()));
        //}
    }
    catch { }
}

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        DropDownList3.Items.Clear();
        string query = " SELECT TABLE_NAME, COLUMN_NAME FROM  " + DropDownList1.SelectedItem + ".INFORMATION_SCHEMA.columns where TABLE_NAME = '" + DropDownList2.SelectedItem + "' ";
        con = new SqlConnection(ConfigurationManager.AppSettings["godb"].ToString());
        da = new SqlDataAdapter(query, con);
        dt = new DataTable();
        da.Fill(dt);
        DropDownList3.DataSource = dt.DefaultView.ToTable(true, "TABLE_NAME", "COLUMN_NAME");
        DropDownList3.DataValueField = "TABLE_NAME";
        DropDownList3.DataTextField = "COLUMN_NAME";
        DropDownList3.DataBind();
    }
    catch (Exception) { }
}
}
Posted
Comments
Herman<T>.Instance 1-Sep-15 9:15am    
Add if (!Page.isPostback) in the try statement around the code. You know always clear all items and reload the data
Pavan Kumar 1-Sep-15 9:35am    
hi, already I am using if(!Page.isPostback) for DDL1 data-bindings & DDL2/DDL3 @ SelectedIbdexChanged Event till the same result
Herman<T>.Instance 1-Sep-15 9:40am    
in your methods DropDownList1_SelectedIndexChanged (and the other selectedindexchanged) I don't see that in your code. ;)
Herman<T>.Instance 1-Sep-15 9:43am    
And use AJAX ControlToolkit around the 3 defined dropdownlists. That prevents the reposting blank screen
Pavan Kumar 1-Sep-15 9:46am    
Thanks for your support, Its working when I ignore 'DataValueField'

1 solution

You need to see this article for Cascading DropDownLists in ASP.Net. You will clear idea how to work with Multiple DDlists in ASP.NET
Creating Cascading DropDownLists in ASP.Net[^]
 
Share this answer
 
Comments
Pavan Kumar 1-Sep-15 9:43am    
Same thing I done in my code except Visibility & I checked downloaded files from that link still the result is same
Pavan Kumar 1-Sep-15 9:46am    
Thanks for your support, Its working when I ignore 'DataValueField'
Schatak 2-Sep-15 0:34am    
Your Welcome :) Accept the solution or mark it solved. So that other knows that it was solved.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900