【问题标题】:Submit button in ASP.netASP.net 中的提交按钮
【发布时间】:2016-06-06 12:09:18
【问题描述】:

我是 .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?)

谢谢。

【问题讨论】:

    标签: asp.net forms submit


    【解决方案1】:

    您必须向 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" />
    

    【讨论】:

    • 双击按钮后,我得到了代码: using System;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 System.Web.UI;使用 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) { } } } 我应该在这里做些什么改变,请解释原因。
    • 错误消失了吗? “我应该在此处进行哪些更改并解释原因”是什么意思?
    • 是的,错误消失了。谢谢....我不明白错误是如何解决的。是因为我删除了 OnClick 还是因为我在设计视图中双击了按钮?为什么我双击按钮时会出现一个新的“.cs”页面?
    • 双击是创建事件处理程序在这里查看更多,msdn.microsoft.com/en-us/library/6w2tb12s.aspx
    • 错误是由于缺少处理程序定义。
    【解决方案2】:
    <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>
    

    【讨论】:

    • @Midhun T:检查一下,让我知道
    • 你能解释一下你做了什么吗?
    • 双击提交按钮并放置代码 Sub submit(sender As Object, e As EventArgs) lbl1.Text="Your name is " & txt1.Text End Sub
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 2016-02-06
    相关资源
    最近更新 更多