为什么在ASP.NET中使用AJAX后我的按钮代码不起作用?

时间:2016-06-15 06:47:36

标签: c# asp.net ajax

我在asp.net中有一个按钮来清除文本框,我使用了如下的ajax:

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button2" runat="server" Font-Bold="False" Font-Names="Tahoma" Font-Size="16px" ForeColor="DarkRed" Height="40px" OnClick="Button2_Click" Text="Clear Form" Width="165px" />
</ContentTemplate>
</asp:UpdatePanel>

同样,此按钮具有以下C#代码:

protected void Button2_Click(object sender, EventArgs e)
    {
        txtFirstName.Text = string.Empty;
        txtLastName.Text = string.Empty;
        txtEmail.Text = string.Empty;
        txtSubject.Text = string.Empty;
        txtMessage.Text = string.Empty;
    }

但是,执行此程序时,上述C#代码不起作用!换句话说,单击按钮后文本框不会清除! 请告诉我为什么会这样?!

3 个答案:

答案 0 :(得分:0)

您需要修改您的updatepanel.Kindly将所有Label和TextBox控件放在UpdatePanel外部的Update Panel和Button Events中,并添加按钮ID的Trigger。

AsyncPostBackTrigger或PostBackTrigger

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
  // HERE  YOUR TEXTBOX AND LABEL CONTROLS 
</ContentTemplate>
 <Triggers>
      <asp:AsyncPostBackTrigger ControlID="Button2"
  </Triggers>
</asp:UpdatePanel>
<asp:Button ID="Button2" runat="server" Font-Bold="False" Font-Names="Tahoma" Font-Size="16px" ForeColor="DarkRed" Height="40px" OnClick="Button2_Click" Text="Clear Form" Width="165px" />

答案 1 :(得分:0)

您应将文本框置于更新面板中

 <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
                    <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
                    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                    <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
                    <asp:TextBox ID="txtMessage" runat="server"></asp:TextBox>

                    <asp:Button ID="Button2" runat="server" Font-Bold="False" Font-Names="Tahoma" Font-Size="16px" ForeColor="DarkRed" Height="40px" OnClick="Button2_Click" Text="Clear Form" Width="165px" />
                </ContentTemplate>
            </asp:UpdatePanel>

答案 2 :(得分:0)

我试过了,按钮代码正常工作 .aspx代码

t = Test.new
t.test

# -> Output:
# hello
# world

t.stacked_methods "stacked methods"

# -> Output:
# stacked methods
# hello
# world
# and not broken
# at all

t.throw_exception
# -> Output:
# will be executed even if exception is thrown
# deferable.rb:45:in `throw': uncaught throw #<Exception: Exception> (UncaughtThrowError)
#         from deferable.rb:45:in `throw_exception'
#         from deferable.rb:18:in `call'
#         from deferable.rb:18:in `block in deferable'
#         from deferable.rb:59:in `<main>'

.aspx.cs页面编码

<div>
     <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
                    <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
                    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                    <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
                    <asp:TextBox ID="txtMessage" runat="server"></asp:TextBox>


                    <asp:Button ID="Button2" runat="server" Font-Bold="False" Font-Names="Tahoma" Font-Size="16px" ForeColor="DarkRed" Height="40px" OnClick="Button2_Click" Text="Clear Form" Width="165px" />
                </ContentTemplate>
            </asp:UpdatePanel>
    </div>