Solved

ViewState?

Posted on 2004-03-23
10
491 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now