简体   繁体   English

表格在asp.net提交

[英]Form submit in asp.net

I have got two buttons, which both submit a form in asp.net. 我有两个按钮,都在asp.net中提交表单。

I need to know in the function below..What button was clicked..The login or register button. 我需要在下面的函数中知道..单击了什么按钮..登录或注册按钮。 And by that information, I want to trigger the one of the functions in the load event. 根据这些信息,我想触发load事件中的功能之一。

    protected void Page_Load(object sender, EventArgs e)
{
    AddCountries();
    AddAge();

      if (IsPostBack)
      {
          string pwd = LoginPassword.Text;
          string saltAsBase64 = "z3llWdYSA2DY3M4uNSpOQw==";
          string hash = HashPass.GenerateHash(pwd, saltAsBase64);

          if (Page.IsValid)
          {
              LoginUser(hash);
         /// Depending on what the user pressed..I need to trigger the function 
        // above or below
              RegisterUser(hash);
          }



      }
}

What if I have this in the button event: 如果我在按钮事件中遇到了该怎么办:

FormsAuthentication.RedirectFromLoginPage(currentUser.UserName, false); FormsAuthentication.RedirectFromLoginPage(currentUser.UserName,false);

will the redirection happen immediately after the button event? 重定向将在按钮事件之后立即发生吗? or will it trigger the page load event again, ignoring that redirection? 还是会忽略该重定向而再次触发页面加载事件?

If the buttons are server side controls <asp:button/> then you can handle the event of the (specific) button: 如果按钮是服务器端控件<asp:button/>则可以处理(特定)按钮的事件:

protected void Button1_Click(object sender, EventArgs e) {....}

vs.

protected void Button2_Click(object sender, EventArgs e) {......}

Which are raised after Page_Load see: ASP.Net Page Life Cycle Page_Load 之后引发哪些事件,请参见: ASP.Net页面生命周期

If you are using standard HTML <input type=submit /> and posting back to the same page, then you can inspect the Request.Form collection ( which you can also do with server side controls ). 如果使用标准HTML <input type=submit />并回发到同一页面,则可以检查Request.Form集合( 也可以使用服务器端控件进行检查 )。

UPDATE: 更新:

  • Page_Load is always raised on postback Page_Load 总是在回发时引发
  • client-side validation will prevent a postback 客户端验证将防止回发
  • if you are using server validation controls, check Page.IsValid in the handler before executing something 如果使用服务器验证控件,请在执行某些操作之前检查处理程序中的Page.IsValid
  • Depending on your needs, you can also inspect controls/values in Page_Load (checking for Page.IsPostBack - the point is you have options... 根据您的需要,您还可以检查Page_Load控件/值(检查Page.IsPostBack关键是您有选择...

So if you (instead) wanted to inspect on Page_Load : 因此,如果您(相反)想检查Page_Load

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
        if ((UserEmail.Text == "jchen@contoso.com") &&
         (UserPass.Text == "37Yj*99Ps"))
          {
            FormsAuthentication.RedirectFromLoginPage
               (UserEmail.Text, Persist.Checked);
          }
        else
          {
            ......
          }
    }
}

above code taken, and slightly modified, from MSDN 上面的代码是从MSDN上获取并稍作修改

UPDATE2: UPDATE2:

If you are using server validation controls, don't use Page_Load . 如果您使用服务器验证控件,请不要使用Page_Load Use the button handler and check for Page.IsValid . 使用按钮处理程序并检查Page.IsValid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM