Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net级联下拉列表始终选择第一项_C#_Asp.net_Cascading_Selectedindexchanged_Dropdownlistfor - Fatal编程技术网

C# asp.net级联下拉列表始终选择第一项

C# asp.net级联下拉列表始终选择第一项,c#,asp.net,cascading,selectedindexchanged,dropdownlistfor,C#,Asp.net,Cascading,Selectedindexchanged,Dropdownlistfor,我在一个页面中使用了三个下拉列表。我还使用if(!Page.IsPostBack)条件将数据绑定到pageload上的DDL1(DropDownList1)。在DDL1 selectedIndexChanged事件中,我正在将数据绑定到DDL2及其工作正常。但是,当我尝试对DDL3和DDL2执行相同的操作时(就像在DDL2的SelectedIndexChanged事件中将数据绑定到DDL2一样),始终只有在我选择random时,DDL2才会选择第一项,而且DDL2仍然是第一项。这里所有3个DDL

我在一个页面中使用了三个下拉列表。我还使用if(!Page.IsPostBack)条件将数据绑定到pageload上的DDL1(DropDownList1)。在DDL1 selectedIndexChanged事件中,我正在将数据绑定到DDL2及其工作正常。但是,当我尝试对DDL3和DDL2执行相同的操作时(就像在DDL2的SelectedIndexChanged事件中将数据绑定到DDL2一样),始终只有在我选择random时,DDL2才会选择第一项,而且DDL2仍然是第一项。这里所有3个DDL都是AutoPostback true,启用了Vistate

这是我的代码:

ASPX-代码..

<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>
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”时,它工作得很好
感谢大家的支持…

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

您可以尝试在这两个和上使用相同的事件方法,具体取决于调用方填充的内容


利用这两个(objectsender,EventArgs e):D

这可能是多个自动回发事件,因此当它第一次触发时,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) { }
    }
公共列表列表1{get;set;}
公共列表列表2{get;set;}
公共列表列表3{get;set;}
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!Page.IsPostBack)
{
尝试
{
list1=新列表{“a”、“b”、“c”};
DropDownList1.DataSource=list1;
DropDownList1.DataBind();
}
捕获(异常){}
}
}
受保护的void DropDownList1\u SelectedIndexChanged(对象发送方,事件参数e)
{
尝试
{
list2=新列表{“1a”、“1b”、“1c”};
DropDownList2.DataSource=list2;
DropDownList2.DataBind();
}
捕获{}
}
受保护的void DropDownList2\u SelectedIndexChanged(对象发送方,事件参数e)
{
尝试
{
list3=新列表{“2a”、“2b”、“2c”};
DropDownList3.DataSource=list3;
DropDownList3.DataBind();
}
捕获(异常){}
}

我刚刚在下拉列表中删除了默认的
选项
,删除了
viewstatemode
,并使用了数据绑定而不是列表。

尝试使用此代码,对我来说很好

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) { }
    }
公共列表列表1{get;set;}
公共列表列表2{get;set;}
公共列表列表3{get;set;}
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!Page.IsPostBack)
{
尝试
{
list1=新列表{“a”、“b”、“c”};
DropDownList1.DataSource=list1;
DropDownList1.DataBind();
}
捕获(异常){}
}
}
受保护的void DropDownList1\u SelectedIndexChanged(对象发送方,事件参数e)
{
尝试
{
list2=新列表{“1a”、“1b”、“1c”};
DropDownList2.DataSource=list2;
DropDownList2.DataBind();
}
捕获{}
}
受保护的void DropDownList2\u SelectedIndexChanged(对象发送方,事件参数e)
{
尝试
{
list3=新列表{“2a”、“2b”、“2c”};
DropDownList3.DataSource=list3;
DropDownList3.DataBind();
}
捕获(异常){}
}


我刚刚在下拉列表中删除了默认的
选项
,删除了
viewstatemode
,并使用了数据绑定而不是列表。

尝试将您的下拉列表放在
UpdatePanel
您好,感谢您的评论。我已经尝试使用updatePanel,但也有相同的结果检查我的更新答案是否已解决-当我忽略DDL的“DataValueField”时效果很好,感谢您的支持…尝试将您的下拉列表放在
updatePanel
嗨,感谢您的评论。我已经尝试使用updatePanel,但也有相同的结果检查我的更新答案是否已解决-当我忽略DDL的“DataValueField”时效果很好,感谢大家的支持…嗨,感谢大家对我的问题感兴趣。我尝试了为两个ddl使用相同的事件方法,直到结果是相同的受保护的void ddl_SelectedIndexChanged(object sender,EventArgs e){if(sender==DropDownList 1){}或者if(sender==DropDownList 2){}}尝试以下方法:var senderDdl=sender as DropDownList;这使typecast从一个对象转换到dropdownlist对象,换句话说,使“发送者”知道它实际上是什么。这将为您提供调用方对象(ddl1/ddl2/…)中所有属性的副本。您好,Darko,感谢您的支持,当我忽略DDL的“DataValueField”时,它工作得很好……您好,感谢您对我的问题感兴趣。我尝试了为两个ddl使用相同的事件方法,直到结果是相同的受保护的void ddl_SelectedIndexChanged(object sender,EventArgs e){if(sender==DropDownList 1){}或者if(sender==DropDownList 2){}}尝试以下方法:var senderDdl=sender as DropDownList;这使typecast从一个对象转换到dropdownlist对象,换句话说,使“发送者”知道它实际上是什么。这将为您提供调用方对象(ddl1/ddl2/…)中所有属性的副本。嗨,Darko,由于您的支持,当我忽略DDL的“DataValueField”时,它工作得很好…Shaminder,它的工作方式与我所需的@List相同,但当使用数据绑定时,它仍然是一样的,我没有