Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > What's better, a hidden form field or viewstate?

Reply
Thread Tools

What's better, a hidden form field or viewstate?

 
 
Alan Silver
Guest
Posts: n/a
 
      06-30-2005
Hello,

I have a page that gets passed an ID in the query string. It then uses
this ID to pull info out of a database and populate controls on the
page. When the page is posted back, the query string is not going to be
there any more, so I need some way of storing the ID.

What's the best way of doing this? The obvious thought is a hidden form
field, but there doesn't seem to be a web control for this. Do I just
use an ordinary HTML control?

The other thought I had was to put the ID in the viewstate, but this
sounds a bit over the top, plus it has the risk that the value might be
dropped form the viewstate.

I don't want to store it in the Session as that times out.

Any comments? TIA

--
Alan Silver
(anything added below this line is nothing to do with me)
 
Reply With Quote
 
 
 
 
Tom.PesterDELETETHISSS@pandora.be
Guest
Posts: n/a
 
      06-30-2005
Viewstate is also put in a hidden form field so there is no big difference
other than that viewstate is easier.

Let me know if you have any more questions..

Cheers,
Tom Pester

> Hello,
>
> I have a page that gets passed an ID in the query string. It then uses
> this ID to pull info out of a database and populate controls on the
> page. When the page is posted back, the query string is not going to
> be there any more, so I need some way of storing the ID.
>
> What's the best way of doing this? The obvious thought is a hidden
> form field, but there doesn't seem to be a web control for this. Do I
> just use an ordinary HTML control?
>
> The other thought I had was to put the ID in the viewstate, but this
> sounds a bit over the top, plus it has the risk that the value might
> be dropped form the viewstate.
>
> I don't want to store it in the Session as that times out.
>
> Any comments? TIA
>



 
Reply With Quote
 
 
 
 
=?Utf-8?B?QW5keSBSb3Nl?=
Guest
Posts: n/a
 
      06-30-2005
I agree with the previous posts, viewstate is easiest.
If you did want to use a hidden field with a dynamic value you can add a
'Literal' web control to your form and set it's text property during the Page
Load to:

"<input type='hidden' id='hiddenField' value='" + ID + "'>";

"(E-Mail Removed)" wrote:

> Viewstate is also put in a hidden form field so there is no big difference
> other than that viewstate is easier.
>
> Let me know if you have any more questions..
>
> Cheers,
> Tom Pester
>
> > Hello,
> >
> > I have a page that gets passed an ID in the query string. It then uses
> > this ID to pull info out of a database and populate controls on the
> > page. When the page is posted back, the query string is not going to
> > be there any more, so I need some way of storing the ID.
> >
> > What's the best way of doing this? The obvious thought is a hidden
> > form field, but there doesn't seem to be a web control for this. Do I
> > just use an ordinary HTML control?
> >
> > The other thought I had was to put the ID in the viewstate, but this
> > sounds a bit over the top, plus it has the risk that the value might
> > be dropped form the viewstate.
> >
> > I don't want to store it in the Session as that times out.
> >
> > Any comments? TIA
> >

>
>
>

 
Reply With Quote
 
Alan Silver
Guest
Posts: n/a
 
      06-30-2005
>A hidden form field is used if you need to pass values between server and
>client. If all you need is just to persist a value between postbacks, use
>viewstate.


Thanks to both of you.

>Eliyahu
>
>"Alan Silver" <(E-Mail Removed)> wrote in message
>news:1FUWQQFv+$(E-Mail Removed)...
>> Hello,
>>
>> I have a page that gets passed an ID in the query string. It then uses
>> this ID to pull info out of a database and populate controls on the
>> page. When the page is posted back, the query string is not going to be
>> there any more, so I need some way of storing the ID.
>>
>> What's the best way of doing this? The obvious thought is a hidden form
>> field, but there doesn't seem to be a web control for this. Do I just
>> use an ordinary HTML control?
>>
>> The other thought I had was to put the ID in the viewstate, but this
>> sounds a bit over the top, plus it has the risk that the value might be
>> dropped form the viewstate.
>>
>> I don't want to store it in the Session as that times out.
>>
>> Any comments? TIA
>>
>> --
>> Alan Silver
>> (anything added below this line is nothing to do with me)

>
>


--
Alan Silver
(anything added below this line is nothing to do with me)
 
Reply With Quote
 
Hans Kesting
Guest
Posts: n/a
 
      06-30-2005
Alan Silver wrote:
> Hello,
>
> I have a page that gets passed an ID in the query string. It then uses
> this ID to pull info out of a database and populate controls on the
> page. When the page is posted back, the query string is not going to
> be there any more, so I need some way of storing the ID.
>
> What's the best way of doing this? The obvious thought is a hidden
> form field, but there doesn't seem to be a web control for this. Do I
> just use an ordinary HTML control?
>
> The other thought I had was to put the ID in the viewstate, but this
> sounds a bit over the top, plus it has the risk that the value might
> be dropped form the viewstate.
>
> I don't want to store it in the Session as that times out.
>
> Any comments? TIA


If it's just an ID, what about storing it in a cookie? That will keep
it's value even if the user clicks a link (which would lose the value
of hidden fields, such as viewstate). You can choose between
persisted and non-persisted cookies, depending on further requirements.

Hans Kesting


 
Reply With Quote
 
=?Utf-8?B?TWljaGFlbCBCYWx0aWM=?=
Guest
Posts: n/a
 
      06-30-2005
If you use a hidden field, you can access it on the aspx page code behind by
declaring it runat='server' and adding it to the list of controls in the
codebehind.

Like this (VB.NET example):

aspx page: <input type="hidden" name="hControlName" id="hControlName"
runat="server">

code behind: Protected WithEvents hControlName As
System.Web.UI.HtmlControls.HtmlInputHidden

dim ID as System.Int32 = CType(hControlName.Value, System.Int32)

--
Staff Consultant II - Enterprise Web Services - Cardinal Solutions Group

Future Business Model - National City Mortgage


"(E-Mail Removed)" wrote:

> Viewstate is also put in a hidden form field so there is no big difference
> other than that viewstate is easier.
>
> Let me know if you have any more questions..
>
> Cheers,
> Tom Pester
>
> > Hello,
> >
> > I have a page that gets passed an ID in the query string. It then uses
> > this ID to pull info out of a database and populate controls on the
> > page. When the page is posted back, the query string is not going to
> > be there any more, so I need some way of storing the ID.
> >
> > What's the best way of doing this? The obvious thought is a hidden
> > form field, but there doesn't seem to be a web control for this. Do I
> > just use an ordinary HTML control?
> >
> > The other thought I had was to put the ID in the viewstate, but this
> > sounds a bit over the top, plus it has the risk that the value might
> > be dropped form the viewstate.
> >
> > I don't want to store it in the Session as that times out.
> >
> > Any comments? TIA
> >

>
>
>

 
Reply With Quote
 
Mythran
Guest
Posts: n/a
 
      06-30-2005

"Alan Silver" <(E-Mail Removed)> wrote in message
news:1FUWQQFv+$(E-Mail Removed)...
> Hello,
>
> I have a page that gets passed an ID in the query string. It then uses
> this ID to pull info out of a database and populate controls on the page.
> When the page is posted back, the query string is not going to be there
> any more, so I need some way of storing the ID.
>
> What's the best way of doing this? The obvious thought is a hidden form
> field, but there doesn't seem to be a web control for this. Do I just use
> an ordinary HTML control?
>
> The other thought I had was to put the ID in the viewstate, but this
> sounds a bit over the top, plus it has the risk that the value might be
> dropped form the viewstate.
>
> I don't want to store it in the Session as that times out.
>
> Any comments? TIA
>
> --
> Alan Silver
> (anything added below this line is nothing to do with me)


The viewstate is a name-value pair collection class that is serialized and
written to the client as 1 field. I am not sure if it is compressed at all
(documentation doesn't say afaik). In any case, use the ViewState if all
you are doin' is persisting across postbacks. If you need to access the
data on the client-side using client-side script, you should use a hidden
field.

Another way, just to throw this out there, is to use a TextBox WebControl
and add the following to make it hidden (behave sorta like a hidden
control):

TextBox1.Attributes.Add("display", "none")

HTH,
Mythran

 
Reply With Quote
 
Eliyahu Goldin
Guest
Posts: n/a
 
      06-30-2005
A hidden form field is used if you need to pass values between server and
client. If all you need is just to persist a value between postbacks, use
viewstate.

Eliyahu

"Alan Silver" <(E-Mail Removed)> wrote in message
news:1FUWQQFv+$(E-Mail Removed)...
> Hello,
>
> I have a page that gets passed an ID in the query string. It then uses
> this ID to pull info out of a database and populate controls on the
> page. When the page is posted back, the query string is not going to be
> there any more, so I need some way of storing the ID.
>
> What's the best way of doing this? The obvious thought is a hidden form
> field, but there doesn't seem to be a web control for this. Do I just
> use an ordinary HTML control?
>
> The other thought I had was to put the ID in the viewstate, but this
> sounds a bit over the top, plus it has the risk that the value might be
> dropped form the viewstate.
>
> I don't want to store it in the Session as that times out.
>
> Any comments? TIA
>
> --
> Alan Silver
> (anything added below this line is nothing to do with me)



 
Reply With Quote
 
Alan Silver
Guest
Posts: n/a
 
      06-30-2005
>If it's just an ID, what about storing it in a cookie? That will keep
>it's value even if the user clicks a link (which would lose the value
>of hidden fields, such as viewstate). You can choose between persisted
>and non-persisted cookies, depending on further requirements.


Thanks, but I don't want to use cookies. The ID is only for use between
postbacks to the same page, so I don't need to persist it across other
pages. Also, you can't always be sure the user has cookies enabled.

Thanks for the reply

--
Alan Silver
(anything added below this line is nothing to do with me)
 
Reply With Quote
 
Alan Silver
Guest
Posts: n/a
 
      06-30-2005
>If you use a hidden field, you can access it on the aspx page code behind by
>declaring it runat='server' and adding it to the list of controls in the
>codebehind.


That's basically what I did, only in C#

Thanks for the reply.

--
Alan Silver
(anything added below this line is nothing to do with me)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Populate Hidden field on post back and retrieve value from Hidden Field Rick ASP .Net 3 04-13-2010 05:38 PM
Pass hidden form field value to another form field to insert in db GavMc ASP General 4 09-22-2005 06:33 PM
writing from hidden field to text field repost Roy Adams Javascript 3 12-21-2004 02:32 PM
writing from hidden field to text field Roy Adams Javascript 1 12-20-2004 03:47 AM
copy and paste form RTF document into field in asp form cause it to bypass field length and javascript validation - how to overcome? NotGiven ASP General 3 05-13-2004 12:15 AM



Advertisments