我在查找如何调用我在.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?
我希望在单击add_pass按钮时运行C#方法。关于如何实现这一目标的任何想法?
答案 0 :(得分:5)
您似乎正在使用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代码替换整个代码,它将类似于以下部分:
<强> YourPage.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
}
}
}
答案 1 :(得分:0)
如果您将onclick事件添加到您的代码中,它应该适用于
<input type="submit" value="Add Password" id="add_pass" onclick="AddPassword_Click" alt="Add Password" border="0">
当您的代码在服务器端运行时,请使用onserverclick=...
答案 2 :(得分:0)
如果你想保留输入类型按钮,那么你可以使用runat =&#34; server&#34;和onclick =&#34; MyMethod&#34;。例如:
< 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,