在ButtonClick之后,DropDownList将Index重置为0

时间:2017-06-07 15:08:35

标签: c# asp.net web-applications webforms

我是ASP.NET新手,我正在尝试将我的WinForm应用程序转换为ASP.NET。

我的情况如下:

我有一个带有项目的DropDownList(这些是在另一个按钮点击时生成的)。现在,当我单击Submit-Button时,我希望将所选项目索引写入Label。 - 相反,它只是将DropDownList重置为第一个索引,并选择一个写入标签。

我已经阅读了有关此内容的所有其他帖子,但有些内容无效,所以我带着具体问题来到这里。在WinForms中,这不是问题,但在ASP中它的工作方式有所不同。

我希望有人可以帮助我。

这是我的代码:

https?

C#:

        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Button_Submit" EventName="Click" />
            </Triggers>
            <ContentTemplate>
                <asp:DropDownList ID="DropDownList_Players" runat="server"></asp:DropDownList>
                <asp:Label ID="SPClabel" runat="server" Text="Label"></asp:Label>
                <asp:Button ID="Button_Submit" runat="server" Text="Set Player" OnClick="Button_Set_Click" />
            </ContentTemplate>
        </asp:UpdatePanel>

1 个答案:

答案 0 :(得分:0)

试试这个。使用selecteditem更改选定的索引

protected void Button_Set_Click(object sender, EventArgs e)
{
    SPClabel.Text = Convert.ToString(DropDownList_Players.SelectedItem);
}
相关问题