简体   繁体   English

在asp.net中的server.transfer之后,验证不起作用

[英]Validations not working after server.transfer in asp.net

   <td align="left" width="15%" class="body_txt" style="padding-right: 2px; padding-left: 25px;">
            <asp:Label ID="lblFirstName" runat="server" Text="First Name:"></asp:Label>
        </td>
        <td width="35%" align="left" style="padding-left: 25px;">
            <asp:TextBox ID="txtFirstName" Width="175px" MaxLength="50" runat="server" CssClass="txt_bx"></asp:TextBox>
            <asp:RegularExpressionValidator ID="RegularExpressionValidatorFirstName" ControlToValidate="txtFirstName"
                SetFocusOnError="true" runat="server" ErrorMessage="*" EnableClientScript="true" ValidationExpression="([a-z]|[A-Z])*"
                ForeColor="Black"></asp:RegularExpressionValidator>
        </td>
        <td width="15%" class="body_txt" align="left" style="padding-right: 2px; padding-left: 25px;">
            <asp:Label ID="lblLastName" runat="server" Text="Last Name:"></asp:Label>
        </td>
        <td width="35%" align="left" style="padding-left: 25px">
            <asp:TextBox ID="txtLastName" Width="175px" MaxLength="50" runat="server" CssClass="txt_bx"></asp:TextBox>
            <asp:RegularExpressionValidator ID="RegularExpressionValidatortxtLastName"  EnableClientScript="true" ControlToValidate="txtLastName"
                SetFocusOnError="true" runat="server" ErrorMessage="*" ValidationExpression="([a-z]|[A-Z])*"
                ForeColor="Black"></asp:RegularExpressionValidator>
        </td>

My Validations are working fine when i got to this page after Response.Redirect 我在Response.Redirect后进入此页面时,我的验证工作正常

However after a Server.Transfer to this page validations stop working and the form does a postback on Button click 但是,在Server.Transfer转移到此页面之后,验证将停止工作,并且该表单会在Button单击时进行回发

Code Behind Pevious Page: 上一页后面的代码:

  protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < lstEmployee.Rows.Count; i++)
            {
                GridViewRow row = lstEmployee.Rows[i];
                bool isChecked = ((RadioButton)row.FindControl("RowSelector")).Checked;

                if (isChecked)
                {
                    iEmployeeId = Convert.ToInt32(row.Cells[0].Text);
                    hdnEmployeeId.Value = Convert.ToString(iEmployeeId);
                    Server.Transfer("EmployeeMaint.aspx", true);
                }
            }
        }

Code Behind of Landing Page: 着陆页背后的代码:

 protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {


            if (iEmployeeId != 0)
            {
                UpdateEmployeeDetails(iEmployeeId);
                Response.Write("<script> alert('User Updated sucessfully. ');window.location.href='Employee.aspx'; </script> ");
            }
            else
            {
                InsertEmployeeDetails();
                Response.Write("<script> alert('User Added sucessfully. ');window.location.href='Employee.aspx'; </script> ");
            }
        }

原因可能是:

As Response.Redirect informs Client(Browser) and then Redirect the page, so validation work is working fine(as validation is done on client side)

whereas Server.Transfer will directly transfer to requested page without informing client, avoiding extra round trip so validation isn't done

Also Since we are not informing client -> web address on the client won't change

I'd just avoid server.transfer since it happens very often that some functionality is just not working properly when this method is used. 我只是避免使用server.transfer,因为使用此方法时,某些功能经常无法正常工作。 The only advantage of server.transfer method is that it doesn't require another request from the client which makes a difference only on very busy servers. server.transfer方法的唯一优点是它不需要来自客户端的其他请求,这仅在非常繁忙的服务器上有所作为。

Here is what I recommend. 这是我的建议。

  • Use Response.Redirect() to transfer to another page 使用Response.Redirect()转移到另一个页面
  • Use Session or Application or Cookies to transfer parameters from one page to another. 使用会话或应用程序或Cookies将参数从一页转移到另一页。

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

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