我在弄清楚如何调用我在 .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 文件而不是 .aspx.cs 中使用 c# 方法?
我希望 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>
YourPage.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
}
}
}
关于javascript - 在输入类型提交按钮上调用 C# 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36874872/