0

在此站点上 ,当下拉列表仅包含一项时,单击它不会导致回发

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DropDownList1.Items.Add("a");

        DropDownList2.Items.Add("a");
        DropDownList2.Items.Add("b");

    }
}


protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    Response.Write(DropDownList1.Text);//does not work ????????????
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
    Response.Write(DropDownList2.Text);

}

在这个网站上

4

5 回答 5

1

DropDownList1_SelectedIndexChanged 永远不会触发,因为您在 DropDownList1 中只有一项,因此索引永远不会更改。

更新

你可以做的是向下拉列表1添加一个空值,就像这样

<asp:DropDownList runat="server" ID="DropDownList1">
    <asp:ListItem Value="0" Text="Choose option" Selected="true" />
    <asp:ListItem Value="1" Text="a" />
</asp:DropDownList>
于 2011-08-09T10:19:11.027 回答
0

DropDownList1_SelectedIndexChanged不会触发,因为所选索引已经是a. DropDownList Events可以在此处找到DropDownList Events的列表。事件DataBound应该触发,你可以在哪里做Response.Write(DropDownList1.Text);

于 2011-08-09T10:25:52.267 回答
0

也许您可以在索引 0 处添加一个新项目,上面写着“选择”,当用户更改选择时,它会导致回发...

DropDownList1.Items.Add("SELECT");
DropDownList1.Items.Add("a");
于 2011-08-09T10:26:58.597 回答
0

asp:DropDownList呈现AutoPostBack="true"为 htmlselect标记,该标记具有客户端onchange事件和自动生成的 java-script 函数来处理此客户端onchange事件。

即,如果您有:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
    </asp:DropDownList>

呈现的页面源如下所示:

<select name="DropDownList1" onchange="javascript:setTimeout('__doPostBack(\'DropDownList1\',\'\')', 0)" id="DropDownList1">

如您所见,必须触发postback客户端事件。仅当 html标记有两个或更多可选时才会出现onchange客户端。onchangeselectoption

我看到解决方案已经发布,只是想详细解释一下会很好。

asp:ListItem简而言之,控制必须不止一个asp:DropDownList

于 2011-08-09T10:48:15.400 回答
0

我想要一个类似的东西。下面的代码为我做了。确保它是您SelectedIndexChanged方法中的第一个代码。

//fixes error when postback 
    if (DropDownList.SelectedIndex == 0)
    {
       DropDownList.SelectedIndex = 1;
    }
于 2012-01-09T00:55:21.520 回答