在输入类型提交按钮上调用 C# 方法
Calling a C# method on a input type submit button
我在弄清楚如何调用我在.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 ,
相关文章:
- 未捕获错误:无法在初始化之前调用方法;
- 通过ajax从客户端调用C#方法来执行C#方法
- 如何避免在angular上多次调用方法;s ng重复
- React路由器错误-'无法调用方法'getRouteAtDepth'的未定义'
- 从window.onbeforeunload调用方法背后的代码
- 当输入字段为空时,如何在angular中调用方法
- 函数调用方法有什么用
- 多次调用方法后返回相同promise的模式
- 当从Chrome扩展动态注入JS时,从onload()内部调用方法
- 茉莉花 - 未调用方法
- jQuery如何在原型中调用方法
- ng显示“;调用方法“;不起作用
- Odoo销售点如何访问模型并使用JS调用方法
- 从React调用方法.JS州
- TinyMCE验证给出错误:无法调用方法'getContent'的未定义
- 向模板实例变量传递调用方法调用的结果时出现异常
- 如何在对象中调用方法
- 检查对话框是否为 Open 会引发“初始化前无法在对话框上调用方法”错误
- 在输入类型提交按钮上调用 C# 方法
- 通过类选择器单独调用方法