Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ViewState?

Posted on 2004-03-23
10
Medium Priority
?
500 Views
Last Modified: 2010-04-15

ViewState["Sql"] = SqlStatement;

load the form ...(dynamically)

submit button...

 reload the form.....(dynamically)

string SQL =(string)ViewState["Sql"]; --> empty ... does view state only work with regular forms or would it work with dinamically recreated forms?


0
Comment
Question by:alain123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 20

Expert Comment

by:TheAvenger
ID: 10661418
In the Page_Load method:

if (this.IsPostBack)
      Response.Output.Write (ViewState["Sql"]);
else
      ViewState["Sql"] = "test";

It works fine, no problems at all.
0
 
LVL 1

Author Comment

by:alain123
ID: 10661614
how about inside a class.cs after IsPostBack...?
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 10661638
How do you access the ViewState from your class.cs?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:alain123
ID: 10667907
like this

string SQL;
SQL =(string)ViewState["GridSql"];
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 10668010
ViewState is not a part of your class, so you cannot access it like this. You have to assign it somehow
0
 
LVL 1

Author Comment

by:alain123
ID: 10682653
tks, avenger.. but somehow how?
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 10682719
I don't know. It is not possible to compile the solution if you have just calls to ViewState from a class. You can make it like this:

using System;
using System.Web.UI;
using System.Diagnostics;

class MyClass {
  public StateBag viewState = null;
  ...
  public void SomeMethod () {
    // Do something with the view state
    Trace.WriteLine (this.viewState["SQL"]);
  }
  ...
}

And in the Page_Load:
if (this.IsPostBack) {
    MyClass mc = new MyClass();
    mc.viewState = this.ViewState;
    mc.SomeMethod();
}
else
     ViewState["Sql"] = "test";

0
 
LVL 1

Author Comment

by:alain123
ID: 10708140
hello again Avenger, about this view state issue,

how come i do this on when i load my form ViewState["XMLFORM"] = XMLDocResult.InnerXml;

and on   override protected void OnInit(EventArgs e)
            {

                  InitializeComponent();

                  sSQL = (string)ViewState["XMLFORM"];  --> is null?

(not from inside a class, this is all on the code behnid of the same page)





0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 500 total points
ID: 10708193
Hmm, just a shot (without being sure): as I mentioned to you in a previous post, ASP.NET processes the ViewState AFTER the OnInit of the form. Try to move the code in the Page_Load (you should reattach the Page_Load if you deleted that code) and see if it works there. If it does, consider moving all your initialization code to Page_Load (recommended by all authors/tutorials).
0
 
LVL 1

Author Comment

by:alain123
ID: 10708682
yep, that was it... thanks a lot!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question