Invalid ViewState

G

Guest

Hello,

I have a page with two link buttons. When I click on the first one I call
server.execute("target.aspx") to view target.aspx on the original page.
Then I click on the second link button and in this case I redirect to
another page, HOWEVER, I get Invalid ViewState exception. I know the problem
stems from server.execute but don't know why. Any Ideas?

Thank you.
 
K

Kevin Spencer

When you call Server.Execute, the second page is processed as part of the
first page. If the second page has any Controls in it, this will affect
(corrupt) the ViewState, as both pages have ViewState. I would recommend
using a UserControl instead of a second page instead. You can dynamically
load the UserControl if you need to.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
G

Guest

Thank you, I'll give it a try.

Kevin Spencer said:
When you call Server.Execute, the second page is processed as part of the
first page. If the second page has any Controls in it, this will affect
(corrupt) the ViewState, as both pages have ViewState. I would recommend
using a UserControl instead of a second page instead. You can dynamically
load the UserControl if you need to.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
G

Guest

Hi Kevin,

How do you load a usercontrol dynamically?

Why is that when on this page if I click on on a hyperlinkcolumn I don't get
that Invalid ViewState error but I do if I want to redirect by clicking on a
linkbutton?
 
G

Guest

Hi Kevin,

How do you load a usercontrol dynamically?

Why is that when on this page if I click on on a hyperlinkcolumn I don't get
that Invalid ViewState error but I do if I want to redirect by clicking on a
linkbutton?
 
K

Kevin Spencer

Hi Nad,
How do you load a usercontrol dynamically?

Use the Page.LoadControl() method. It takes a virtual path as its argument,
and returns a reference to the Control. Example:

MyControl = LoadControl("/DirName/AnotherDirName/ControlName.ascx")
Why is that when on this page if I click on on a hyperlinkcolumn I don't
get
that Invalid ViewState error but I do if I want to redirect by clicking on
a
linkbutton?

It's not what button you click. It's what function you call.
Response.Redirect redirects the client browser to request the second page.
Server.Execute adds the executed page's output to the current Page's
Response.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
G

Guest

Hi Kevin,

Thank you for your reply. I think I didn't explain my second question well.

I have a datagrid row with 2 linkbutton columns and a hyperlinkbutton column.
If I click on the first linkbutton column which calls request.execute and
then I click on the second linkbutton I get invalid viewstate error. But if I
click on the hyperlinkbutton column it's ok and it takes me to the specified
web page.
Why is that?
 
K

Kevin Spencer

I understood the question. Here's your original message (the salient part):

Note that only one of the buttons you mentioned calls Server.Execute. The
other calls Response.Redirect. At least according to what you said.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
K

Kevin Spencer

Maybe I'm not understanding you very well. It occurs to me that you may be
saying that if you don't click on the button that calls Server.Execute, and
then click on the hyperlink button, you don't get the Invalid ViewState
error, but if you do click on the first button (which calls Server.Execute)
and THAN click on the same page with the hyperlink button, you DO get the
error. Is that correct? If so, the answwer is still the same. Let me
explain. when you call Server.Execute, you don't get the Invalid ViewState
until you do another PostBack. Why? Because the ViewState is evaluated after
a PostBack, which doesn't occur until you click some element that causes a
PostBack. IOW, when you click the first button, it causes a PostBack. The
ViewState is fine, and it passes the test. Then the app adds the second page
to the first, and returns it to the browser. Now, on the browser side
everything is fine (sort of: at least the browser can parse the document).
But when you do a PostBack, you have a corrupt ViewState which was produced
by your last PostBack followed by Server.Execute.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
G

Guest

Kevin,

Thank you for the time you took to reply.

You misunderstood.

Here are two scenarios that I perform SEPERATELY.

Scenario 1.
a. I open the page and click on the first linkbutton (with call to
server.execute)
b. I click on the 2nd linkbutton which redirects to another page
result: Invalid ViewState

Scenario 2.
a. I open the page and click on the first linkbutton (with call to
server.execute)
b. I click on the hyperlinkbutton (which redirects to another page)
result: the page opens and there are no errors

Why are the results inconsistent?
 
G

Guest

Sorry to bother you Kevin but I am not able to fix the invalid viewstate by
loading a usercontrol dynamically, here's what I'm doing:

1. In the page_load of the usercontrol I write:
StringWriter swrTarget = new StringWriter();
Server.Execute("myPage.aspx", swrTarget);
myLiteral.Text = swrTarget.ToString();

2. AND in the ItemCommand of the original page's datagrid control I write:

myUserControl userControl = (myUserControl) LoadControl("myUserControl.ascx");
myPanel.Controls.Add(userControl);

BUT I still get the same error.
 
K

Kevin Spencer

If you get rid of ALL Server.Execute code, and replace it with
dynamically-loaded UserControls, I'm willing to bet dollars to doughnuts
that your problems will go away.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top