ASP.net 中的提交按钮

Submit button in ASP.net

我是 .NET 的新手,正在尝试基本功能。放置提交按钮时出现错误。请查看代码,如果我使用的提交按钮语法有误,请告诉我。

代码:

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

错误是:

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

谢谢。

您必须向 OnClick 提供服务器端 OnClick 处理程序,并且提交可能未定义为处理程序。 MSDN Button.OnClick documentation 说明 OnClick 事件处理程序如何与按钮关联。您还需要为按钮提供 ID

从按钮中删除 OnClick 属性。在 VS 设计器中打开表单 双击 VS 设计器中的按钮,它将为您生成处理程序。您可以在 How to: Create Event Handlers in ASP.NET Web Forms Pages

中找到模式

生成事件处理程序后,您会得到类似的东西。

代码隐藏

void yourButtonId_Click(Object sender, EventArgs e)
{

}

HTML (aspx)

<asp:Button ID="yourButtonId" OnClick="yourButtonId_Click" Text="submit" runat="server" />
<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>