Thursday, March 29, 2012

Missing something basic

Hi, new to .Net and I seem to be missing something basic.

From I what I have read so far I have gained the impression that ViewState is used to describe the way a .Net form retains the data in controls on that form.

But I have just seen a bit of code that, after retrieving a ContactID from a database then says

Code:
ViewState["ContactID"] = ContactID;
but there is no control on the form called ContactID - so what is being set?

And ...

what is the correct way to do the following ...

Say I pass a companyID to a page like:
<a href="http://links.10026.com/?link=CompanyDetails.aspx?CoID=25">Show Details</a>

and, on that page I run a procedure that returns the company name - which I want to hang on to then regardless of how many times that page is posted back to.

My instincts are to retrieve the company name and stick it in a <asp:HiddenField> and retrieve it each time the page is posted back.

Is this okay?

CheersViewState is a dictionary that is serialized into an encoded string as the page is about render. This encoded string is sent to the client - as you can clearly see if you do a 'view..source' from IE after requesting an ASPX page.

When the client browser posts back to the server, that encoded string is used to populate a ViewState dictionary again. ViewState allows stateful storage of control data by putting that data directly in the html document passed back and forth. Once all the controls are loaded and initalized, the ViewState is inspected and control properties are set to the same 'state' as when the page was sent to the client.

ASP.NET server controls make use of this dictionary, and so can you. The framework doesn't care what adds items to viewstate. That said, you shouldn't pull a LOT of data in Viewstate because it has to be encoded to a string that is sent back and forth all the time.

There's also Session state, and Application state, and the HttpRuntime state.
Thanks for your reply.

So when I see code like this:

ViewState["ContactID"] = ContactID;

it means that the value of ContactID is being 'put into' ViewState and can be retrieved using Request("ContactID") - even though there is no control called 'ContactID'?
Yes, but you retrieve the value by using

if (ViewState["ContactID"] != null)
contactId = ViewState["ContactID"];

Request["ContactID"] would attempt to retrieve a value from the http <form> that was posted by the client.

So in the case of your hidden input field (which is all ViewState is underneath), you could retrieve its value the manual way by using Request["myHiddenInputField"].But the more acceptable ASP.NET way is to mark the hidden input field to run as a server control in your aspx markup, declare it in your related web page class (codebehind), and check its Value property.

aspx:
<form>
<input type="hidden" runat="server" id="myHiddenInput" />
</form>

and in the page_load event (C#):

class WebPage1
{
protected HtmlInputHidden myHiddenInput;

Page_Load
{
string lookAtMe = myHiddenInput.Value;
}

http://msdn2.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlinputhidden.aspx
Right - thanks for that. Got the picture now.

Cheers

0 comments:

Post a Comment