2

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.

0

2 Answers 2

4

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" />
Sign up to request clarification or add additional context in comments.

5 Comments

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.
Is error gone? what do you mean by "What changes should I make here and please explain the reason"?
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?
Double click was to create event handler see more here, msdn.microsoft.com/en-us/library/6w2tb12s.aspx
Error was due to missing handler definition.
0
<script  runat="server">
Sub submit(sender As Object, e As EventArgs)
   lbl1.Text="Your name is " & txt1.Text
End Sub
</script>

<!DOCTYPE html>
<html>
<body>

<form runat="server">
Enter your name:
<asp:TextBox id="txt1" runat="server" />
<asp:Button OnClick="submit" Text="Submit" runat="server" />
<p><asp:Label id="lbl1" runat="server" /></p>
</form>

</body>
</html>

3 Comments

@Midhun T : check this and let me know
can u please explain what you did?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.