Asp.net级联下拉列表总是选择第一个项目

本文关键字:选择 第一个 项目 net 级联 下拉列表 Asp | 更新日期: 2023-09-27 18:10:44

我在一个页面中使用了三个下拉列表。并且我使用if(!Page.IsPostBack)条件将数据绑定到页面加载上的DDL1(DropDownList1)。在DDL1 selectedIndexChanged Event上,我将数据绑定到DDL2,它工作得很好。但当我试图对DDL3做同样的事情时DDL2(就像在DDL2的SelectedIndexChanged事件上将数据绑定到DDL2一样)总是只有当我选择随机时DDL2才选择第一项,DDL2仍然是第一项。这里所有3个ddl都是AutoPostback-true, Vistate-Enabled

下面是我的代码:

ASPX - Code.

<form id="form1" runat="server">
<div>    
    <br />
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        <asp:ListItem Value="0">Select</asp:ListItem>
    </asp:DropDownList>
    <br />
    <br />
    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
        <asp:ListItem Value="0">Select</asp:ListItem>
    </asp:DropDownList>
    <br />
    <br />
    <asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True">
        <asp:ListItem Value="0">Select</asp:ListItem>
    </asp:DropDownList>
    <br />    
</div>
</form>

cs代码…

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

编辑:- 当我忽略ddl的'DataValueField'时,它工作得很好

Asp.net级联下拉列表总是选择第一个项目

这可以是多个自动回退事件,因此当它第一次触发时,ddl1会在第二个事件中覆盖所选项目。当第二个事件发生时,它被重置为默认状态。

您可以尝试在两者上使用相同的事件方法,并取决于调用者填充的东西。

使用这两个(对象发送者,EventArgs e):D

试着使用这个代码,它的工作对我来说很好。

aspx

<form id="form1" runat="server">
    <div>
    <asp:DropDownList  ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" >
    </asp:DropDownList>
    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" >
    </asp:DropDownList> 
    <asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True" >
    </asp:DropDownList>
    </div>
    </form>

cs

    public List<string> list1 { get; set; }
    public List<string> list2 { get; set; }
    public List<string> list3 { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            try
            {
                list1 = new List<string> { "a", "b", "c" };
                DropDownList1.DataSource = list1;
                DropDownList1.DataBind();
            }
            catch (Exception) { }
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            list2 = new List<string> { "1a", "1b", "1c" };
            DropDownList2.DataSource = list2; 
            DropDownList2.DataBind();
        }
        catch { }
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            list3 = new List<string> { "2a", "2b", "2c" };
            DropDownList3.DataSource = list3;
            DropDownList3.DataBind();
        }
        catch (Exception) { }
    }

我只是在下拉菜单中删除了您的默认options,并删除了viewstatemode,并使用您的数据绑定而不是列表。