Solved

ViewState?

Posted on 2004-03-23
10
496 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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 125 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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