ASP.NET DropDownList OnSelectedIndexChanged事件未触发

时间:2022-12-02 14:03:41

I'm trying to use some AJAX and ASP.Net together to enable me to run functions without having to refresh the whole page but i've stumbled across a problem in doing this

我正在尝试使用一些AJAX和ASP.Net来使我能够运行功能而无需刷新整个页面,但我偶然发现了这样做的问题

Here's my code

这是我的代码

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:DropDownList ID="ddl1" runat="server" OnSelectedIndexChanged="update1" />

        <asp:TextBox runat="server" ID="txt1" />

    </ContentTemplate>
</asp:UpdatePanel>

And here's my code behind

这是我背后的代码

 Sub update1(ByVal sender As Object, ByVal e As EventArgs)

    txt1.Text = Now.ToString

End Sub

The event doesn't fire because I don't have AutoPostBack="True" on my ddl but adding that to the ddl will postback the whole page.

该事件不会触发,因为我的ddl上没有AutoPostBack =“True”,但是将其添加到ddl将会回发整个页面。

Is there a way to avoid using AutoPostBack="True" so that it only updates the panel?

有没有办法避免使用AutoPostBack =“True”,以便它只更新面板?

I know I can use an asp:Button to get around this but i'd really like to be able to use a ddl with OnSelectedIndexChanged

我知道我可以使用asp:Button来解决这个问题,但我真的希望能够使用带有OnSelectedIndexChanged的ddl

Thanks

谢谢

2 个解决方案

#1


7  

If you want to avoid to send the whole viewstate to the server, you should look at callbacks.

如果要避免将整个视图状态发送到服务器,则应该查看回调。

Instead, if you want to avoid a refresh of the entire page, but with postback, this should work:

相反,如果你想避免刷新整个页面,但是使用回发,这应该有效:

<asp:DropDownList ID="ddl1" runat="server" OnSelectedIndexChanged="update1" AutoPostBack="True"  />

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <Triggers>
         <asp:AsyncPostbackTrigger ControlID="ddl1" EventName="SelectedIndexChanged" />
    </Triggers>
    <ContentTemplate>
        <asp:TextBox runat="server" ID="txt1" />
    </ContentTemplate>
</asp:UpdatePanel>

#2


0  

Try creating a new page with same codes and different page name. Worked for me

尝试使用相同的代码和不同的页面名称创建新页面。为我工作

#1


7  

If you want to avoid to send the whole viewstate to the server, you should look at callbacks.

如果要避免将整个视图状态发送到服务器,则应该查看回调。

Instead, if you want to avoid a refresh of the entire page, but with postback, this should work:

相反,如果你想避免刷新整个页面,但是使用回发,这应该有效:

<asp:DropDownList ID="ddl1" runat="server" OnSelectedIndexChanged="update1" AutoPostBack="True"  />

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <Triggers>
         <asp:AsyncPostbackTrigger ControlID="ddl1" EventName="SelectedIndexChanged" />
    </Triggers>
    <ContentTemplate>
        <asp:TextBox runat="server" ID="txt1" />
    </ContentTemplate>
</asp:UpdatePanel>

#2


0  

Try creating a new page with same codes and different page name. Worked for me

尝试使用相同的代码和不同的页面名称创建新页面。为我工作