0
   <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 到此页面验证停止工作并且表单在按钮单击时进行回发之后

上一页背后的代码:

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

2 回答 2

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

于 2013-03-06T06:59:18.347 回答
0

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

这是我推荐的。

  • 使用 Response.Redirect() 转移到另一个页面
  • 使用会话或应用程序或 Cookie 将参数从一个页面传输到另一个页面。
于 2013-03-06T10:40:30.377 回答