C# 如何在asp.net c中从另一个dropdownlist中选择项目后将项目加载到dropdownlist中#

C# 如何在asp.net c中从另一个dropdownlist中选择项目后将项目加载到dropdownlist中#,c#,asp.net,C#,Asp.net,我的表单中有四个下拉列表框,我想根据从第一个下拉列表中选择的项目将项目加载到三个下拉列表中,我的第一个下拉列表包含以下项目 金额, 派克靴, 基本计量单位 无论我从第一个下拉列表中选择什么,我都希望将相同的选定项加载到其余三个下拉列表中 我已经尝试了以下代码,但它没有按预期工作 protected void ddl_UOM_SelectedIndexChanged(object sender, EventArgs e) { string uom_Name = ddl_UOM.Selecte

我的表单中有四个下拉列表框,我想根据从第一个下拉列表中选择的项目将项目加载到三个下拉列表中,我的第一个下拉列表包含以下项目

金额, 派克靴, 基本计量单位

无论我从第一个下拉列表中选择什么,我都希望将相同的选定项加载到其余三个下拉列表中

我已经尝试了以下代码,但它没有按预期工作

protected void ddl_UOM_SelectedIndexChanged(object sender, EventArgs e)
{
   string uom_Name = ddl_UOM.SelectedItem.Value;
   ddl_UOM2.Items.Add(uom_Name);
   ddl_UOM3.Items.Add(uom_Name);
   ddl_UOM4.Items.Add(uom_Name);
}

请提供帮助。

您需要使用Ajax控件工具包的级联下拉列表

下面是类似的例子:

下面是演示:

以下是教程链接:


希望本资料对您有所帮助。

您应该尝试使用JS中的PageMethods在任何时候以您喜欢的任何方式加载下拉列表。

可能是您没有将AutoPostBack属性设置为true。请尝试以下代码示例:

ASPX:

<asp:DropDownList runat="server" ID="parentDropDown" OnSelectedIndexChanged="parentDropDown_SelectedIndexChanged" AutoPostBack="true">
    <asp:ListItem Text="1" Value="1"></asp:ListItem>
    <asp:ListItem Text="2" Value="2"></asp:ListItem>
    <asp:ListItem Text="3" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList runat="server" ID="child1">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="child2">
</asp:DropDownList>
protected void parentDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
    string uom_Name = parentDropDown.SelectedValue;
    child1.Items.Add(uom_Name);
    child2.Items.Add(uom_Name);
}
protected void parentDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
    string uom_Name = parentDropDown.SelectedValue;

    child1.Items.Clear();
    child2.Items.Clear();

    child1.Items.Add(uom_Name);
    child2.Items.Add(uom_Name);
}
<asp:DropDownList ID="ddl_UOM" runat="server" OnSelectedIndexChanged="ddl_UOM_SelectedIndexChanged" AutoPostBack="true">
        <asp:ListItem Text="Amount" Value="Amount"></asp:ListItem>
        <asp:ListItem Text="PAC" Value="PAC"></asp:ListItem>
        <asp:ListItem Text="Base" Value="Base"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl_UOM2" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddl_UOM3" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddl_UOM4" runat="server"></asp:DropDownList>
如果要在添加之前从子项中删除现有项,则:

<asp:DropDownList runat="server" ID="parentDropDown" OnSelectedIndexChanged="parentDropDown_SelectedIndexChanged" AutoPostBack="true">
    <asp:ListItem Text="1" Value="1"></asp:ListItem>
    <asp:ListItem Text="2" Value="2"></asp:ListItem>
    <asp:ListItem Text="3" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList runat="server" ID="child1">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="child2">
</asp:DropDownList>
protected void parentDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
    string uom_Name = parentDropDown.SelectedValue;
    child1.Items.Add(uom_Name);
    child2.Items.Add(uom_Name);
}
protected void parentDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
    string uom_Name = parentDropDown.SelectedValue;

    child1.Items.Clear();
    child2.Items.Clear();

    child1.Items.Add(uom_Name);
    child2.Items.Add(uom_Name);
}
<asp:DropDownList ID="ddl_UOM" runat="server" OnSelectedIndexChanged="ddl_UOM_SelectedIndexChanged" AutoPostBack="true">
        <asp:ListItem Text="Amount" Value="Amount"></asp:ListItem>
        <asp:ListItem Text="PAC" Value="PAC"></asp:ListItem>
        <asp:ListItem Text="Base" Value="Base"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl_UOM2" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddl_UOM3" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddl_UOM4" runat="server"></asp:DropDownList>

在添加新项之前,必须将AutoPostBack=“true”设置为第一个下拉列表,并清除其他下拉列表项

ASPX:

<asp:DropDownList runat="server" ID="parentDropDown" OnSelectedIndexChanged="parentDropDown_SelectedIndexChanged" AutoPostBack="true">
    <asp:ListItem Text="1" Value="1"></asp:ListItem>
    <asp:ListItem Text="2" Value="2"></asp:ListItem>
    <asp:ListItem Text="3" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList runat="server" ID="child1">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="child2">
</asp:DropDownList>
protected void parentDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
    string uom_Name = parentDropDown.SelectedValue;
    child1.Items.Add(uom_Name);
    child2.Items.Add(uom_Name);
}
protected void parentDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
    string uom_Name = parentDropDown.SelectedValue;

    child1.Items.Clear();
    child2.Items.Clear();

    child1.Items.Add(uom_Name);
    child2.Items.Add(uom_Name);
}
<asp:DropDownList ID="ddl_UOM" runat="server" OnSelectedIndexChanged="ddl_UOM_SelectedIndexChanged" AutoPostBack="true">
        <asp:ListItem Text="Amount" Value="Amount"></asp:ListItem>
        <asp:ListItem Text="PAC" Value="PAC"></asp:ListItem>
        <asp:ListItem Text="Base" Value="Base"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl_UOM2" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddl_UOM3" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddl_UOM4" runat="server"></asp:DropDownList>
在ddl\U UOM\U SelectedIndexChanged()中编写代码后,从页面加载()调用ddl\U UOM\U SelectedIndexChanged()方法


使用ajax控制工具套件的级联下拉菜单什么不起作用?您是否收到任何错误?第一次加载到ddl时,我再次从第一个ddl中选择了不同的项,结果是一样的,没有发生任何更改。如果您没有Javascript代码,为什么有Javascript标记?可能是您缺少AutoPostback属性。看看我的答案。级联下拉比使用javascript和发送ajax调用要好得多。在这种情况下,@DatRid级联下拉也会起作用,他希望所选项目添加到其他下拉中。啊,nvm,我删除了我的评论,误读了它,我很抱歉:D如果他还没有尝试使用Javascript,他应该删除Javascript标记。是的,你是对的,但是如果下拉列表是服务器端的,因为它的web表单使用Javascript在drodown中添加项目会导致问题,因为视图state@user3515164很高兴帮助你。别忘了投票,并把它标为答案。很抱歉,它有一些问题,问题是当我们第一次选择第一个项目时,页面加载时,它没有加载到其他ddl中。您的页面加载中是否有代码?确保在页面加载,如果你是绑定你的下拉列表,那么它应该在!isPostback是的,我的页面中有代码_load@SpiderCode我绑定的下拉列表,它在!IsPostBack确保它在
中,如果(!IsPostBack){…}
它有一些问题,问题是当我们第一次在第一个项目中选择时,当页面加载时,它没有加载到其他ddl中。谢谢@Charbel boutrosy您必须将ddl_uU selectedIndex中的代码添加到page_加载函数中。您为什么对此投反对票?这是一个合法的答案,非常有力。