簡體   English   中英

在輸入類型提交按鈕上調用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文件而不是.aspx.cs中使用c#方法嗎?

我希望單擊add_pass按鈕時運行C#方法。 關於如何實現這一目標的任何想法?

似乎您正在使用ASP.NET Web窗體(因為您有一個實際的ASPX文件)。

如果是這種情況,ASP.NET會為您處理大量此類操作,並且實際上將對自身執行POST ,從而使它的內容可在您的代碼隱藏中訪問。

您可以考慮用實際的<asp:Button>替換現有的submit按鈕,該<asp:Button>指向在表單發布時插入代碼后方的特定方法:

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

OnClick事件將處理將您單擊的Button映射到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
        }
    }
}

如果您將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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM