asp.net级联下拉列表总是选择第一项 [英] asp.net cascading dropdownlists always selects first item

查看:80
本文介绍了asp.net级联下拉列表总是选择第一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个页面中使用了三个DropDownLists。我使用if(!Page.IsPostBack)条件在pageload上将数据绑定到DDL1(DropDownList1)。在DDL1 selectedIndexChanged事件我将数据绑定到DDL2并且其工作正常。但是当我尝试对DDL3& DDL2(类似于DDL2的SelectedIndexChanged事件上的DDL2的绑定数据)总是DDL2仅在我选择随机时选择第一项,同时DDL2仍然是第一项。这里所有3个DDL都是AutoPostback-true,Vistate-Enabled。



这是我的代码 -

ASPX代码: -

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

<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代码: -



.CS Code :-

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) { }
}
}

推荐答案

您需要在ASP.Net中查看Cascading DropDownLists的这篇文章。您将清楚如何使用ASP.NET中的多个DDlists

在ASP.Net中创建级联DropDownLists [ ^ ]
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[^]


这篇关于asp.net级联下拉列表总是选择第一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆