如果dropdownlist autopostback属性为true,则选择任何值,它自动选择索引1

时间:2013-09-17 09:27:21

标签: c# asp.net .net

我有3个下拉列表,当我选择第一个下拉列表填充第2个和第3个时,当我选择第3个下拉列表时,它是自动选择索引1下拉第2个和第3个。我想在第3次下拉选择的索引更改上做一些工作。这是我的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)
{

}

2 个答案:

答案 0 :(得分:1)

请确保您填写

中的下拉列表
if (!Page.IsPostBack)
    {
          //LoadDropdownListHere();
    }

答案 1 :(得分:0)

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

试试这个..