表格在asp.net中提交 [英] Form submit in asp.net

查看:65
本文介绍了表格在asp.net中提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个按钮,都在asp.net中提交了一个表单.

I have got two buttons, which both submit a form in asp.net.

我需要在下面的功能中知道.单击了什么按钮.登录或注册按钮.通过这些信息,我想触发load事件中的功能之一.

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.

    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?

推荐答案

如果按钮是服务器端控件< asp:button/> ,那么您可以处理(特定)事件按钮:

If the buttons are server side controls <asp:button/> then you can handle the event of the (specific) button:

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

vs.

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

之后 Page_Load 引发的内容,请参见: ASP.Net页面生命周期

Which are raised after Page_Load see: ASP.Net Page Life Cycle

如果您使用的是标准HTML < input type = submit/> 并回发到同一页面,则可以检查 Request.Form 集合(,您也可以使用服务器端控件).

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).

更新:

  • Page_Load 在回发时始终提高
  • 客户端验证将防止回发
  • 如果您使用服务器验证控件,请在执行某些操作之前先检查处理程序中的 Page.IsValid
  • 根据您的需要,您还可以检查 Page_Load 中的控件/值(检查 Page.IsPostBack -关键是您可以选择...
  • Page_Load is always raised on postback
  • client-side validation will prevent a postback
  • if you are using server validation controls, check Page.IsValid in the handler before executing something
  • 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 :

So if you (instead) wanted to inspect on 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
          {
            ......
          }
    }
}

上面的代码是从 MSDN

UPDATE2:

如果使用服务器验证控件,请不要使用 Page_Load .使用按钮处理程序并检查 Page.IsValid .

If you are using server validation controls, don't use Page_Load. Use the button handler and check for Page.IsValid.

这篇关于表格在asp.net中提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆