在输入类型提交按钮上调用 C# 方法

Calling a C# method on a input type submit button

本文关键字:调用 方法 按钮 输入 类型 提交      更新时间:2023-09-26

我在弄清楚如何调用我在.CS文件中编写的方法时遇到了一些麻烦,该方法将更新数据库中的用户密码。

这是我目前的表格:

 <form method="post" action="#" runat="server" name="edit_password_form">
    <div id="edit_password_popup">
        <table width="390" align="center" padding="10">
            <tr>
                <td><span class="error_field">*</span>Password:<br>
                    <input type="password" name="password_1" id="password_1" size="19" maxlength="30" autocapitalize="off" autocorrect="off" autocomplete="off">
                </td>
                <td><span class="error_field">*</span>Confirm Password:<br>
                    <input type="password" name="password_2" id="password_2" size="19" maxlength="30" autocapitalize="off" autocorrect="off" autocomplete="off">
                </td>
            </tr>
             <tr>
                <td><input type="submit" value="Add Password" id="add_pass" alt="Add Password" border="0"></td>
                <td><input type="button" value="No Thanks" id="no_thanks" alt="No Thanks" border="0"></td>
             </tr>
        </table>
    </div>
</form>

我是否应该在.aspx文件中使用 c# 方法而不是.aspx.cs?

我希望 C# 方法在单击add_pass按钮时运行。关于如何实现这一目标的任何想法?

您似乎正在使用 ASP.NET Web 窗体(因为您有一个实际的 ASPX 文件)。

如果是这种情况,ASP.NET 会为您处理相当多的内容,并且实际上会对自身执行POST,从而允许在代码隐藏中访问其内容。

您可以考虑将现有的submit按钮替换为实际<asp:Button>,该指向发布表单时要在代码隐藏中点击的特定方法:

<asp:Button ID="AddPassword" Text="Add Password" ... OnClick="AddPassword_Click"><asp:Button>

OnClick 事件将处理将正在单击的按钮映射到aspx.cs文件中如下所示的事件:

protected void AddPassword_Click(object sender, EventArgs e)
{
        // This code will be executed when your AddPassword Button is clicked
}

如果将整个代码替换为相关的 ASP.NET 代码,它将类似于以下部分:

你的页面.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="YourPage.aspx.cs" Inherits="YourProject.YourPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Password Checking</title>
</head>
<body>
<!-- This will post back to your YourPage.aspx.cs code-behind -->
<form id="form1" runat="server" name="edit_password_form">
<div id="edit_password_popup">
    <table width="390" align="center" padding="10">
        <tr>
            <td><span class="error_field">*</span>Password:<br />
                <!-- Replaced <input> elements with matching <asp:TextBox> controls -->
                <asp:TextBox ID="Password1" runat="server" TextMode="Password" size="19" maxlength="30" autocapitalize="off" autocorrect="off" autocomplete="off"></asp:TextBox>
            </td>
            <td><span class="error_field">*</span>Confirm Password:<br>
                <asp:TextBox ID="Password2" runat="server" TextMode="Password" size="19" maxlength="30" autocapitalize="off" autocorrect="off" autocomplete="off"></asp:TextBox>
            </td>
         </tr>
         <tr>
            <td>
                <!-- Updated submit button to proper <asp:Button> -->
                <asp:Button ID="AddPassword" runat="server" Text="Add Password" OnClick="AddPassword_Click" alt="Add Password" border="0"><asp:Button>
            </td>
            <td>
                <input type="reset" value="No Thanks" id="no_thanks" alt="No Thanks" border="0" />
            </td>
         </tr>
    </table>
</div>
</form>
</body>
</html>

你的页面.aspx.cs

public partial class YourPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    // This will get called when your form is submitted
    protected void AddPassword_Click(object sender, EventArgs e)
    {
        // When your page is posted back, compare your passwords
        if(String.Equals(Password1.Text,Password2.Text))
        {
            // Your passwords are equal, do something here
        }
        else
        {
            // They aren't equal, do something else
        }
    }
}
如果将

onclick 事件添加到代码中,它应该可以

<input type="submit" value="Add Password" id="add_pass" onclick="AddPassword_Click" alt="Add Password" border="0">

当代码在服务器端运行时,请使用onserverclick=...

如果你想保留输入类型按钮,那么你可以使用runat="server"和onclick="MyMethod"。例如:

< input type="submit" runat="server" value="Add Password" id="add_pass" alt="Add Password" onclick="Add_Pass_Click" on border="0" >

,并使用处理程序:

using MyClass.cs; 
protected void Add_Pass_Click(object sender, EventArgs e)
{
    MyClass.DoSomeStuff(); //maybe, it's static
}

也就是说,如果我理解你的问题。参考: http://www.codeproject.com/Questions/571190/howplustopluscodeplusinputplustypeplusbuttonpluscl ,