2016-04-26 65 views
1

我在計算如何調用我在.CS文件中寫入的方法時遇到一些問題,該方法將更新數據庫中的用戶密碼。在輸入類型提交按鈕上調用C#方法

這是我目前的形式:

<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按鈕時運行。有關我如何實現這一目標的任何想法?

+0

首先,將表單元素中的動作更改爲實際的後端端點。然後在該端點的代碼隱藏中,您可以處理髮布的數據。 – ohiodoug

+0

那麼行動路徑是aspx.cs文件? – Learn12

+0

不,操作路徑是.aspx文件。也就是說,如果你沒有做任何URL重寫。 – ohiodoug

回答

4

看起來好像你正在使用ASP.NET Web窗體(因爲你有一個實際的ASPX文件)。

如果是這種情況,ASP.NET會爲您處理相當多的事情,並且實際上會自行執行POST,這可以讓您的代碼隱藏內容。

當表單張貼你可以考慮與實際指向一個特定的方法替換現有的submit按鈕,在你的命中後臺代碼:

<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代碼替換整個代碼,它看起來像以下部分(S):

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 
     } 
    } 
} 
+0

謝謝Rion。這工作得很好,給了我更多的內幕知識! – Learn12

0

如果onclick事件添加到您的代碼,它應該工作到

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

當你的代碼在服務器端運行時使用onserverclick=...

0

如果你想保留輸入類型的按鈕,那麼你可以使用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,