2013-03-06 46 views
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的後不工作在asp.net

但是經過一個Server.Transfer的這個頁面驗證停止運轉,形式不上的按鈕進行回傳點擊

代碼背後Pevious頁:

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

回答

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

+0

是否沒有解決此問題的方法? – vini 2013-03-06 07:00:18

+0

使用Response.Redirect無法完成您的工作? – vikbehal 2013-03-06 07:21:51

+0

我必須將多個值從一個頁面傳輸到另一個頁面,這就是爲什麼 – vini 2013-03-06 07:42:03

0

我只是避免Server.Transfer的,因爲它發生這種方法常常會導致某些功能無法正常工作od被使用。 server.transfer方法的唯一優點是它不需要來自客戶端的其他請求,這隻會在非常繁忙的服務器上發揮作用。

這是我推薦的。

  • 使用Response.Redirect()轉移到另一個頁面
  • 使用Session或應用程序或餅乾從一個頁面傳遞參數到另一個。