在没有PostBack的情况下更改标签文本(使用更新面板)

You*_*sef 7 c# asp.net updatepanel

我创建了一个ASP.NET网站.我想要做的是根据下拉列表中选择的项目使标签更改其内容.我尝试了这个,但它不起作用:

下拉列表如下所示:

<asp:DropDownList ID="DropDown1" runat="server" >
    <asp:ListItem Value="a"></asp:ListItem>
    <asp:ListItem Value="b"></asp:ListItem>
    onselectedindexchanged="DropDown1_SelectedIndexChanged"
</asp:DropDownList>
Run Code Online (Sandbox Code Playgroud)

标签:

<asp:Label ID="Label1" Text="" runat="server"/>
Run Code Online (Sandbox Code Playgroud)

我想这样做而不必使用PostBack.

我试图使用ajax Update面板像这样:

<asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">        
    <Triggers>
        <asp:AsyncPostBackTrigger controlid="DropDown1"                                       EventName="SelectedIndexChanged" />
    </Triggers>
    <ContentTemplate>
        <asp:Label ID="Label1" Text="" runat="server"/>
    </ContentTemplate>
</asp:UpdatePanel>
Run Code Online (Sandbox Code Playgroud)

并且在代码后面DropDown1_SelectedIndexChanged事件中:

protected void DropDown1_SelectedIndexChanged(object sender, EventArgs e)
{
    Label1.Text = DropDown1.SelectedValue;
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用.

任何人都可以帮助我吗?

非常感谢您的帮助

sik*_*der 9

这是你的解决方案..

用下面的一个替换你的下拉式aspx控件..

  <asp:DropDownList ID="DropDown1" runat="server" onselectedindexchanged="DropDown1_SelectedIndexChanged" AutoPostBack="true">
                <asp:ListItem Value="a"></asp:ListItem>
                <asp:ListItem Value="b"></asp:ListItem>
           </asp:DropDownList>

<asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
    <ContentTemplate>
        <asp:Label ID="Label1" Text="test" runat="server"/>
    </ContentTemplate>

    <Triggers>
        <asp:AsyncPostBackTrigger controlid="DropDown1" EventName="SelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel>
Run Code Online (Sandbox Code Playgroud)


Ben*_*son 5

您需要启用autopostback并将事件处理程序定义放在正确的位置:

<asp:DropDownList ID="DropDown1" runat="server" onselectedindexchanged="DropDown1_SelectedIndexChanged" AutoPostBack="true">
                                <asp:ListItem Value="a"></asp:ListItem>
                                <asp:ListItem Value="b"></asp:ListItem>
                            </asp:DropDownList>
Run Code Online (Sandbox Code Playgroud)