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 to this 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> ");
            }
        }
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 に答える