Error executing child request for...

  • Thread starter Thread starter Daniel Groh
  • Start date Start date
D

Daniel Groh

Hi,
I've written a C# Server.Transfer from WebForm1.aspx to WebForm2.aspx
In both i'm using the same user control but with different params, for
example;

In the user control:

if(Request.ServerVariables["SCRIPT_NAME"]==Server.MapPath("WebForm1.aspx")){

lblTitle.Text = "Page 1";

}else{

if(Request.ServerVariables["SCRIPT_NAME"]==Server.MapPath("WebForm1.aspx")){

lblTitle.Text = "Page 2";

}

}

I works perfectly, but when i have the Server.Transfer("WebForm2.aspx"); it
keeps the Page 1 title from the WebForm1.aspx.
And if i try to instance my User control in WebForm2.aspx i have this error
in WebForm1.aspx: Error executing child request for WebForm2.aspx.
It changes to Page 2 title only when i use
Response.Redirect("WebForm2.aspx");

What is happening ?

Thanks in advance
 
Daniel Groh said:
I works perfectly, but when i have the Server.Transfer("WebForm2.aspx"); it
keeps the Page 1 title from the WebForm1.aspx.
And if i try to instance my User control in WebForm2.aspx i have this error
in WebForm1.aspx: Error executing child request for WebForm2.aspx.
It changes to Page 2 title only when i use
Response.Redirect("WebForm2.aspx");

What is happening ?

Response.Redirect asks the browser to make a new request for the
specified page. Server.Transfer causes the server to return the output
from the specified page within the current response.
 
Ok but why it's keeping the same user control from my Page1.aspx page ?
I added a Label in my Page2.aspx and everythink ok...it show, but still
keeps in the url bar Page1.aspx and should show Page2.aspx.

=/
 
Ok but why it's keeping the same user control from my Page1.aspx page ?
I added a Label in my Page2.aspx and everythink ok...it show, but still
keeps in the url bar Page1.aspx and should show Page2.aspx.

To be honest, I don't know why your control is causing an exception to
be thrown. Put a try/catch block around the code in the control, and set
a breakpoint in the catch block. It's a separate issue from what shows
in the title bar, though. As far as the browser is concerned, it is
looking at WebForm1.aspx. The Server.Transfer has happened on the server
side, and the browser knows nothing of it.

What you are trying to do with Request.ServerVariables["SCRIPT_NAME"]
will not work with Server.Transfer, because it will always return the
page the browser requested, not the one you transferred to. You would be
better off doing something like this:

public interface ITitledPage
{
string PageTitle{get;}
}

public class WebForm1 : System.Web.UI.Page, ITitledPage
{
.....

public string PageTitle
{
get
{
return "Page 1";
}
}

.....
}

public class WebForm2 : System.Web.UI.Page, ITitledPage
{
.....

public string PageTitle
{
get
{
return "Page 2";
}
}

.....
}

and in your webcontrol:

ITitledPage page = this.Page as ITitledPage;
if(page!=null)
{
this.lblTitle.Text = page.PageTitle;
}
 
Back
Top