Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

BUG: Postback Event Not Called When RewritePath Uses Server.Transfer or Server.Execute


View products that this article applies to.

Symptoms

When you use the Server.Transfer or the Server.Execute method along with HttpContext.RewritePath in an ASP.NET page to transfer control to another ASP.NET page, the postback event on the receiving page is not raised.

↑ Back to the top


Workaround

To work around this problem, use one of these methods as detailed following this list:
  • Use Response.Redirect.
  • Add the Context.Rewrite path

Use Response.Redirect

Use Response.Redirect instead of Server.Transfer or context.Server.Execute in the ProcessRequest() method of HttpHandler.

The modified ProcessRequest() method appears as follows:

Microsoft Visual C# .NET Code

public void ProcessRequest(HttpContext context) 
{
	HttpRequest Request = context.Request;
	HttpResponse Response = context.Response;
	string redirurl = Request.RawUrl.Replace ("test.ashx/", "");
	context.RewritePath (redirurl);			
	Response.Redirect(redirurl);
}

Microsoft Visual Basic .NET Code

Public Sub ProcessRequest(ByVal context As HttpContext) _
    Implements IHttpHandler.ProcessRequest
    Dim Request As HttpRequest = context.Request
    Dim Response As HttpResponse = context.Response
    Dim redirurl As String = Request.RawUrl.Replace("test.ashx/", "")
    context.RewritePath(redirurl)      
    Response.Redirect(redirurl)
End Sub 

Add Context.Rewrite

Add the Context.Rewrite path in Application_BeginRequest event of the Global.asax file.

The modified Application_BeginRequest event appears as as follows:.

Visual C# .NET Code

protected void Application_BeginRequest(Object sender, EventArgs e)
{			
	string redirurl = Request.RawUrl.Replace ("test.ashx/", "");		
	this.Context.RewritePath(redirurl);
}

Visual Basic .NET Code

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    Dim redirurl As String
    redirurl = Request.RawUrl.Replace("test.ashx/", "")
    Me.Context.RewritePath(redirurl)
End Sub

↑ Back to the top


Status

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

↑ Back to the top


More information

Steps to Reproduce the Behavior

Create a Page with PostBack Events

  1. Create a new ASP.NET Web Application project and name the project RoundTripTest.
  2. Right-click WebForm1.aspx, and then click View HTML Source.
  3. Replace the existing code with the following code:
     
    <%@ Page %>
    <HTML>
       <HEAD>
          <title>WebForm1</title>
          <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
       </HEAD>
       <body>
          <A href="/RoundTripTest/test.ashx/TestWebForm.aspx">TestWebForm</A>
       </body>
    </HTML> 
    
  4. On the File menu, click Add New Item.
  5. Under Add New Item, click Web Form.
  6. Under Add New Item, in the Name box, Rename the form TestWebForm, and then click Open.
  7. Drag the TextBox control from the Toolbox onto the TestWebForm.aspx.
  8. Repeat step 7 to add another TextBox.
  9. Drag the Button control from the Toolbox onto the TestWebForm.aspx.
  10. Duble-click the Button and add the following code to the button-click event .

    Visual C# .NET Code
    private void Button1_Click(object sender, System.EventArgs e) {
    	if (TextBox2.Text.Equals (""))
    	 {
    		TextBox2.Text = TextBox1.Text;
    		TextBox1.Text = "";
    		}
    	else 
    	{
    		TextBox1.Text = TextBox2.Text;
    		TextBox2.Text = "";
    	}
    }
    Visual Basic .NET Code
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            If TextBox2.Text.Equals("") Then
                TextBox2.Text = TextBox1.Text
                TextBox1.Text = ""
            Else
                TextBox1.Text = TextBox2.Text
                TextBox2.Text = ""
            End If
    
    End Sub
  11. Add the code and modify the Page_Load event as follows:

    Visual C# .NET Code
    private void Page_Load(object sender, System.EventArgs e) {		
    	if (!IsPostBack) 
    	{
    		TextBox1.Text = "This is a sample text to copy";
    	}
    }
    Visual Basic .NET Code
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       If Not IsPostBack Then
          TextBox1.Text = "This is a sample text to copy"
       End If
    End Sub

Create a Class with Server.Execute or Server.Transfer

  1. On the File menu, click Add New Item.
  2. Click Class under Add New Item.
  3. Rename the class name as ContentHandler in the the Name box under Add New Item, and then click Open.
  4. Replace the existing code in the ContentHandler class file with the following code:

    Visual C# .NET Code
    using System;
    using System.Web;
    
    namespace RoundTripTest {	
    	public class ContentHandler : IHttpHandler
    	{
    		public void ProcessRequest(HttpContext context) 
    		{
    			HttpRequest Request = context.Request;
    			HttpResponse Response = context.Response;
    			string redirurl = Request.RawUrl.Replace ("test.ashx/", "");
    			context.RewritePath (redirurl);
    			context.Server.Transfer(redirurl);
    	//		context.Server.Execute(redirurl);		
    		}
    		public bool IsReusable 
    		{
    			get
    			{
    				return true;
    			}
    		}
    	}
    }
    

    Note To test with Server.Execute, replace
    context.Server.Transfer(redirurl);
    with
    context.Server.Execute(redirurl);		
    Visual Basic .NET Code
    Imports System
    Imports System.Web
    Public Class ContentHandler
        Implements IHttpHandler
    
        Public Sub ProcessRequest(ByVal context As HttpContext) _
        Implements IHttpHandler.ProcessRequest
            Dim Request As HttpRequest = context.Request
            Dim Response As HttpResponse = context.Response
            Dim redirurl As String = Request.RawUrl.Replace("test.ashx/", "")
            context.RewritePath(redirurl)
            context.Server.Transfer(redirurl)
         '  context.Server.Execute(redirurl)      
    
        End Sub
    
        ' Override the IsReusable property.        
        Public ReadOnly Property IsReusable() As Boolean _
        Implements IHttpHandler.IsReusable
    
            Get
                Return True
            End Get
        End Property
    End Class
    
    
    Note To test with Server.Execute, replace
    context.Server.Transfer(redirurl)
    with
    context.Server.Execute(redirurl)		

Define HttpHandler in the .ashx File

  1. On the File menu, click Add New Item.
  2. Type test.ashx in Name box under Add New Item, and then click Open.
  3. Replace the existing code in test.ashx with the following code:

    Visual C# .NET Code
     
    <%@ WebHandler language="c#" Class="Handler" Debug="true" %>
    
    using RoundTripTest;
    
    public class Handler : ContentHandler  
    {
    }
    Visual Basic .NET Code
     
    <%@ WebHandler language="vb" Class="Handler" Debug="true" %>
    
    Imports RoundTripTest
    
    Public Class Handler
          Inherits ContentHandler     
    End Class

Test the Application

  1. On the Debug menu, click Start to run the application. The URL http://localhost/RoundTripTest/WebForm1.aspx appears.
  2. Click the link TestWebForm to display the TestWebForm.aspx.
  3. Click Test.

    Verify that the test in the first text box is not swapped onto the second text box. This explains that the postback event on the receiving page is not recognized and that button click events are not thrown.

↑ Back to the top


References

For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
320439 PRB: "Error Executing Child Request" Error Message When You Use Server.Transfer or Server.Execute in ASP.NET Page
320976 PRB: Server.Transfer Allows Unauthorized Pages to Be Displayed
219294 How to Use the Server.Transfer Method

↑ Back to the top


Keywords: KB817036, kbbug, kbweb, kbwebforms, kbstate

↑ Back to the top

Article Info
Article ID : 817036
Revision : 5
Created on : 7/28/2003
Published on : 7/28/2003
Exists online : False
Views : 406