SohJel SohJel - 5 months ago 7
Ajax Question

Why my button code doesn't work after using AJAX in ASP.NET?

I have a button in asp.net to clear textboxes and I used ajax as below:

<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>


As well, this button has following C# code:

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;
}


However, foregoing C# code doesn't work when I execute this program!; In other words, textboxes don't clear after I click on button!
Please tell me why it happens?!

Answer

I have tried and the button code is working .aspx code

<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>

.aspx.cs page coding

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;
        }
Comments