如果下拉列表自动回发属性为 true,则选择它自动选择索引 1 的任何值

本文关键字:选择 索引 任何值 true 下拉列表 属性 如果 | 更新日期: 2023-09-27 18:31:47

当我选择第一个下拉列表时,我有 3 个下拉列表 当我选择第三个下拉列表时,填充第二个和第三个下拉列表时,它会自动选择索引 1 下拉列表 2nd 和 3rd。 我想对第三个下拉列表选定的索引更改做一些工作。 这是我的ASPX代码

body>
<form id="form1" runat="server">
<div>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
        <asp:ListItem Selected="True">select</asp:ListItem>
    </asp:DropDownList>
    <asp:DropDownList ID="DropDownList2" runat="server">
    </asp:DropDownList>
    <asp:DropDownList ID="DropDownList3" runat="server"  AutoPostBack="True" 
        onselectedindexchanged="DropDownList3_SelectedIndexChanged">
    </asp:DropDownList>
</div>
</form>

我的 cs 代码是

HotelBAL objhotel = new HotelBAL();
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //objhotel.searchHotelRoomType(DropDownList1, "1");
    }
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    string id = DropDownList1.SelectedItem.Text;
    objhotel.searchHotelOccupancy(DropDownList2, id);
    objhotel.searchHotelMealPlan(DropDownList3, id);
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
}

如果下拉列表自动回发属性为 true,则选择它自动选择索引 1 的任何值

请确保您正在填写下拉列表

if (!Page.IsPostBack)
    {
          //LoadDropdownListHere();
    }
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 
    onselectedindexchanged="DropDownList2_SelectedIndexChanged">
    <asp:ListItem Selected="True">select</asp:ListItem>
    <asp:ListItem>1</asp:ListItem>
    <asp:ListItem>2</asp:ListItem>
    <asp:ListItem>3</asp:ListItem>
</asp:DropDownList>

在下拉列表中添加默认值。

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string id = DropDownList1.SelectedItem.Text;
        if(!id.Equals("select"))
         {
           objhotel.searchHotelOccupancy(DropDownList2, id);
           objhotel.searchHotelMealPlan(DropDownList3, id);
         }
    }

试试这个..