Solved

ViewState?

Posted on 2004-03-23
10
493 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ConsoleSql 1 34
fomat Json objects 6 28
WKHTMLTOPDF - --disable-smart-shrinking not working 10 30
ASP.NET 5 Templates 2 65
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

939 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

10 Experts available now in Live!

Get 1:1 Help Now