exception in page_load

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

This is a button click event and it works:
private void btnSubmit_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
string myPar = txtBox.Text;
SwitchToPage(myPar);
}

I needed to switch the page if there is a parameter provided in the URL. The
following does not work on the same page although myPar is the same in both
cases, SwitchToPage return exception as "A control cannot modify its
parents' control collections"

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
string myPar=Request.QueryString["MyPar"];
if(myPar!=null)
{
SwitchToPage(myPar);
}
}
}

Any idea where the problem might be? Is there any other way I can realize
the same thing?
 
Thank you very much for the reply. There are lots of things going on in
SwitchToPage’s page_load including users verification and that is not my code
I can not release it. I am just wondering although I am passing the same
string what would be the reason the system does not work.
I am also guessing the page is not loaded completely when I call
SwitchToPage method in the Page_load, is there any other layer I can call
this method. Just before page display?


Jon said:
Hi,

Might be best to supply the SwitchToPage method?

Jon

JIM.H. said:
Hello,

This is a button click event and it works:
private void btnSubmit_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
string myPar = txtBox.Text;
SwitchToPage(myPar);
}

I needed to switch the page if there is a parameter provided in the URL. The
following does not work on the same page although myPar is the same in both
cases, SwitchToPage return exception as "A control cannot modify its
parents' control collections"

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
string myPar=Request.QueryString["MyPar"];
if(myPar!=null)
{
SwitchToPage(myPar);
}
}
}

Any idea where the problem might be? Is there any other way I can realize
the same thing?
 
What is the exception? Are you getting a null value exception? It's probably
because you're checking for a null valuue incorrectly.

Don't try to set myPar to be the value of the querystring item without first
checking the querystring item
try this:

if(Request.QueryString["MyPar"] != null)
{
SwitchToPage(Request.QueryString["MyPar"].ToString());
}

Trying to assign a querystring item as a value to something when the
querystring item doesn't exist will throw a null exception. You have to test
the collection item first before assigning it as a value to something.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
Thank you very much for your reply Mark. I get the following exception. "A
control cannot modify its parents' control collections" which is fired by a
main class that is written in house which I do not have control over it.

I am sure I send a string which is not null to SwitchToPage() since I see it
on the debug mode. My guess is I should be switching this new page once
everything is loaded. Is there any way once page is loaded assumed that the
login button is clicked. When I click button it works so that would be a
solution for me.


Mark Fitzpatrick said:
What is the exception? Are you getting a null value exception? It's probably
because you're checking for a null valuue incorrectly.

Don't try to set myPar to be the value of the querystring item without first
checking the querystring item
try this:

if(Request.QueryString["MyPar"] != null)
{
SwitchToPage(Request.QueryString["MyPar"].ToString());
}

Trying to assign a querystring item as a value to something when the
querystring item doesn't exist will throw a null exception. You have to test
the collection item first before assigning it as a value to something.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage



JIM.H. said:
Hello,

This is a button click event and it works:
private void btnSubmit_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
string myPar = txtBox.Text;
SwitchToPage(myPar);
}

I needed to switch the page if there is a parameter provided in the URL.
The
following does not work on the same page although myPar is the same in
both
cases, SwitchToPage return exception as "A control cannot modify its
parents' control collections"

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
string myPar=Request.QueryString["MyPar"];
if(myPar!=null)
{
SwitchToPage(myPar);
}
}
}

Any idea where the problem might be? Is there any other way I can realize
the same thing?
 
If you don't have control of a class written in house and it throwing an
error then speak with the class author or catch the error then deal with it.
How can you debug a black box?

SA


JIM.H. said:
Thank you very much for your reply Mark. I get the following exception. "A
control cannot modify its parents' control collections" which is fired by
a
main class that is written in house which I do not have control over it.

I am sure I send a string which is not null to SwitchToPage() since I see
it
on the debug mode. My guess is I should be switching this new page once
everything is loaded. Is there any way once page is loaded assumed that
the
login button is clicked. When I click button it works so that would be a
solution for me.


Mark Fitzpatrick said:
What is the exception? Are you getting a null value exception? It's
probably
because you're checking for a null valuue incorrectly.

Don't try to set myPar to be the value of the querystring item without
first
checking the querystring item
try this:

if(Request.QueryString["MyPar"] != null)
{
SwitchToPage(Request.QueryString["MyPar"].ToString());
}

Trying to assign a querystring item as a value to something when the
querystring item doesn't exist will throw a null exception. You have to
test
the collection item first before assigning it as a value to something.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage



JIM.H. said:
Hello,

This is a button click event and it works:
private void btnSubmit_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
string myPar = txtBox.Text;
SwitchToPage(myPar);
}

I needed to switch the page if there is a parameter provided in the
URL.
The
following does not work on the same page although myPar is the same in
both
cases, SwitchToPage return exception as "A control cannot modify its
parents' control collections"

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
string myPar=Request.QueryString["MyPar"];
if(myPar!=null)
{
SwitchToPage(myPar);
}
}
}

Any idea where the problem might be? Is there any other way I can
realize
the same thing?
 
Back
Top