Question

The code behind event for a form submit button is not executing.

<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="true" Inherits="_Default" %>

<form id="form1" runat="server">
<input id="Submit1" type="submit" value="Update" runat="server" onclick="btnclick_Click" />
<asp:TextBox ID="tbStatus" enableViewState="true" runat="server" TextMode="MultiLine" 
    Width="296px" Height="67px" ReadOnly="True"></asp:TextBox>
 </form>

Code behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
          tbStatus.Text = "Status: Page loaded";
    }
}

protected void btnclick_Click(object sender, EventArgs e)
{
   tbStatus.Text += "\nButton clicked. ";
}

The btnclick_Click is never executing. If I instead change Page_Load to this:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
          tbStatus.Text = "Status: Page loaded";
    }
} else {
      tbStatus.Text += "\nUpdated. (Page_Load PostBack). ";
}

Then the textarea is updated twice, on the third sudmit I ran into the error message:

The state information is invalid for this page and might be corrupted.

I just want the button submit event to execute. Thanks in advance for any information on what I am doing wrong!

Was it helpful?

Solution 2

Try changing the to asp.net button

<asp:Button ID="Submit1" runat="server" Text="Update" OnClick="btnClick_Click"/>

Code behind:

protected void btnclick_Click(object sender, EventArgs e)
{
   tbStatus.Text += Environment.NewLine + "Button clicked. ";
}

OTHER TIPS

I think you should use server control itself to call page method.If you still want to continue with html input try

<input id="Submit1 "runat="server" type="button" onserverclick="btnclick_Click" />

For More on HtmlButton.OnServerClick

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top