Submit button in ASP.net

23,172

You have to provide the server side OnClick handler to OnClick and probably submit is not defined as handler. This MSDN Button.OnClick documentation tell how OnClick event handler is attached with button. You also need to provide the ID to your button.

Remove the OnClick attribute from button. Open the form in VS designer Double click the button in VS designer it will generate handler for you. You can find mode in How to: Create Event Handlers in ASP.NET Web Forms Pages

After generating event handler you would have something like.

Code behind

void yourButtonId_Click(Object sender, EventArgs e)
{

}

HTML (aspx)

<asp:Button ID="yourButtonId" OnClick="yourButtonId_Click" Text="submit" runat="server" />
Share:
23,172
Phoenix.1993
Author by

Phoenix.1993

Updated on July 17, 2022

Comments

  • Phoenix.1993
    Phoenix.1993 almost 2 years

    I am new to .NET and I am trying out basic functionalities. I got an error when I placed submit button. Please go through the code and let me know if the Submit button syntax I used is wrong.

    Code :

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>My First web page</title>
    </head>
    <body>
        <form id="form1" runat="server" >
        <div style="position:absolute">
    
    
            First Name :<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <br/>
            Last Name :<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:Button OnClick="submit" Text="submit" runat="server" />
        </div>
        </form>
    </body>
    </html>
    

    Error is :

    Compilation Error
    
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 
    
    Compiler Error Message: CS1061: 'ASP.webform1_aspx' does not contain a definition for 'submit' and no extension method 'submit' accepting a first argument of type 'ASP.webform1_aspx' could be found (are you missing a using directive or an assembly reference?)
    

    Thank You.

  • dhruv jadia
    dhruv jadia over 8 years
    @Midhun T : check this and let me know
  • Phoenix.1993
    Phoenix.1993 over 8 years
    can u please explain what you did?
  • dhruv jadia
    dhruv jadia over 8 years
    double click on submit button and place code Sub submit(sender As Object, e As EventArgs) lbl1.Text="Your name is " & txt1.Text End Sub
  • Phoenix.1993
    Phoenix.1993 over 8 years
    after double clicking on the button, i got the code : using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void buttonId_Click(object sender, EventArgs e) { } } } What changes should I make here and please explain the reason.
  • Adil
    Adil over 8 years
    Is error gone? what do you mean by "What changes should I make here and please explain the reason"?
  • Phoenix.1993
    Phoenix.1993 over 8 years
    Yes the error is gone. Thank You.... I didn't understand how the error was resolved. was it because I removed the OnClick or was it because I double clicked on the button in design view? why did a new '.cs'page come up when I double clicked on the button?
  • Adil
    Adil over 8 years
    Double click was to create event handler see more here, msdn.microsoft.com/en-us/library/6w2tb12s.aspx
  • Adil
    Adil over 8 years
    Error was due to missing handler definition.