Microsoft KB Archive/817036

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

Article ID: 817036

Article Last Modified on 7/28/2003

-

APPLIES TO


 * Microsoft ASP.NET 1.1

-





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.



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 (&quot;test.ashx/&quot;, &quot;&quot;); 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(&quot;test.ashx/&quot;, &quot;&quot;) 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 (&quot;test.ashx/&quot;, &quot;&quot;); 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(&quot;test.ashx/&quot;, &quot;&quot;) Me.Context.RewritePath(redirurl) End Sub



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



Create a Page with PostBack Events
 Create a new ASP.NET Web Application project and name the project RoundTripTest . Right-click WebForm1.aspx, and then click View HTML Source.  Replace the existing code with the following code: <%@ Page %>   WebForm1   TestWebForm</A> </HTML> </li> On the File menu, click Add New Item.</li> Under Add New Item, click Web Form.</li> Under Add New Item, in the Name box, Rename the form TestWebForm, and then click Open.</li> Drag the TextBox control from the Toolbox onto the TestWebForm.aspx.</li> Repeat step 7 to add another TextBox.</li> Drag the Button control from the Toolbox onto the TestWebForm.aspx.</li>  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 (&quot;&quot;)) {       TextBox2.Text = TextBox1.Text; TextBox1.Text = &quot;&quot;; }   else {       TextBox1.Text = TextBox2.Text; TextBox2.Text = &quot;&quot;; } }

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(&quot;&quot;) Then TextBox2.Text = TextBox1.Text TextBox1.Text = &quot;&quot; Else TextBox1.Text = TextBox2.Text TextBox2.Text = &quot;&quot; End If

End Sub </li>  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 = &quot;This is a sample text to copy&quot;; } }

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 = &quot;This is a sample text to copy&quot; End If End Sub </li></ol>

Create a Class with Server.Execute or Server.Transfer
<ol> On the File menu, click Add New Item.</li> Click Class under Add New Item.</li> Rename the class name as ContentHandler in the the Name box under Add New Item, and then click Open.</li>  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 (&quot;test.ashx/&quot;, &quot;&quot;); 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(&quot;test.ashx/&quot;, &quot;&quot;) 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)     </li></ol>

Define HttpHandler in the .ashx File
<ol> On the File menu, click Add New Item.</li> Type test.ashx in Name box under Add New Item, and then click Open.</li>  Replace the existing code in test.ashx with the following code:

Visual C# .NET Code
<%@ WebHandler language=&quot;c#&quot; Class=&quot;Handler&quot; Debug=&quot;true&quot; %>

using RoundTripTest;

public class Handler : ContentHandler { }

Visual Basic .NET Code
<%@ WebHandler language=&quot;vb&quot; Class=&quot;Handler&quot; Debug=&quot;true&quot; %>

Imports RoundTripTest

Public Class Handler Inherits ContentHandler End Class </li></ol>

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.

<div class="references_section">