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>

My Validations are working fine when i got to this page after Response.Redirect

However after a Server.Transfer to this page validations stop working and the form does a postback on Button click

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> ");
            }
        }
vini
  • 4,657
  • 24
  • 82
  • 170

2 Answers2

1

The reason could be:

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

vikbehal
  • 1,486
  • 3
  • 21
  • 48
0

I’d just avoid server.transfer since it happens very often that some functionality is just not working properly when this method is used. 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.

Here is what I recommend.

  • Use Response.Redirect() to transfer to another page
  • Use Session or Application or Cookies to transfer parameters from one page to another.
Joel Cuff
  • 1,008
  • 11
  • 7