在asp.net中的server.transfer之后,验证无效

时间:2013-03-06 06:49:35

标签: c# asp.net validation server.transfer

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

当我在Response.Redirect

之后到达此页面时,我的验证工作正常

然而,在Server.Transfer to this page validations停止工作后,表单会在Button上点击回复

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);
                }
            }
        }

登陆页面背后的代码:

 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> ");
            }
        }

2 个答案:

答案 0 :(得分:1)

原因可能是:

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

答案 1 :(得分:0)

我只是避免使用server.transfer,因为在使用此方法时,某些功能通常无法正常工作。 server.transfer方法的唯一优点是它不需要来自客户端的另一个请求,这只会在非常繁忙的服务器上产生影响。

这是我推荐的。

  • 使用Response.Redirect()转移到另一个页面
  • 使用会话或应用程序或Cookie将参数从一个页面传输到另一个页面。