Link to home
Start Free TrialLog in
Avatar of alain123
alain123

asked on

ViewState?


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?


Avatar of TheAvenger
TheAvenger
Flag of Switzerland image

In the Page_Load method:

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

It works fine, no problems at all.
Avatar of alain123
alain123

ASKER

how about inside a class.cs after IsPostBack...?
How do you access the ViewState from your class.cs?
like this

string SQL;
SQL =(string)ViewState["GridSql"];
ViewState is not a part of your class, so you cannot access it like this. You have to assign it somehow
tks, avenger.. but somehow how?
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";

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)





ASKER CERTIFIED SOLUTION
Avatar of TheAvenger
TheAvenger
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yep, that was it... thanks a lot!