473,426 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,426 software developers and data experts.

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

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)
Nov 19 '05 #1
12 7417
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" <al*********@nospam.thanx> wrote in message
news:1F**************@nospamthankyou.spam...
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)

Nov 19 '05 #2
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

Nov 19 '05 #3
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 + "'>";

"To********************@pandora.be" 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


Nov 19 '05 #4
>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" <al*********@nospam.thanx> wrote in message
news:1F**************@nospamthankyou.spam...
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)
Nov 19 '05 #5
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
"To********************@pandora.be" 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


Nov 19 '05 #6
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
Nov 19 '05 #7

"Alan Silver" <al*********@nospam.thanx> wrote in message
news:1F**************@nospamthankyou.spam...
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

Nov 19 '05 #8
>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)
Nov 19 '05 #9
>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)
Nov 19 '05 #10

"Alan Silver" <al*********@nospam.thanx> wrote in message
news:OZ**************@nospamthankyou.spam...
Also, you can't always be sure the user has cookies enabled.

Well, if you make use of the Session var, and it is used InProc, it uses a
Cookie to track the SessionId for the client :)

Mythran

Nov 19 '05 #11
Thats a session cookie and most people allow that kind. Its the persistent
cookies that get blokked.
Cheers,
Tom Pester
"Alan Silver" <al*********@nospam.thanx> wrote in message
news:OZ**************@nospamthankyou.spam...
Also, you can't always be sure the user has cookies enabled.

Well, if you make use of the Session var, and it is used InProc, it
uses a Cookie to track the SessionId for the client :)

Mythran

Nov 19 '05 #12
>> Also, you can't always be sure the user has cookies enabled.

Well, if you make use of the Session var, and it is used InProc, it
uses a Cookie to track the SessionId for the client :)


True, but I don't want to use cookies in this case anyway ;-)

Thanks for the reply

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ElanKathir | last post by:
Hi ! What is ViewState object ? What is use of that? and I wrote one Function in Server side and also Client Side. I want to run both one by one. Like Validation and Submit. Thanks &...
2
by: Me | last post by:
Is there anyway to control the style/layout of this field? ..NET doesn't generate an ID for it so you can create a style for it or manipulate it in code. I've looked on MSDN etc. but can't...
5
by: Thor W Hammer | last post by:
In ASP.NET 2.0, it seems to wrap a div around the hidden form fields such as __ViewState. How do you override this rendering?
4
by: Joe | last post by:
Hello All: I have two webforms (WebForm1.aspx and WebForm2.aspx) that inherit from a base class called BasePage.aspx. BasePage.aspx has no user interface but inherits System.Web.UI.Page...
4
by: Umut Tezduyar | last post by:
I want to get rid of the hidden field _VIEWSTATE. I tried these: I tried to disable ViewState property. I tried to override Page's RegisterHiddenField method. I tried to override Page's...
1
by: Harshal P | last post by:
Hello, I am settings hidden field value from the DayRender event hadler of the asp.net calendar control, when i try to read the value of hidden field from javascript, the value is empty string. If...
3
by: Stimp | last post by:
Is it possible to prevent the <input type="hidden" name="__VIEWSTATE" field from being generated in an aspx page? I'm running off Framework 1.1 The field is messing up a process that I need to...
1
by: John Dalberg | last post by:
What causes a server event to fire when something happens on the client? Say a user changed the gridview's page index, the server side PageIndexChanged event is fires. What makes...
1
by: dawidg | last post by:
I have an asp.net page with viewstate disabled. I have a control on the page that dynamically creates controls inside itself, no viewstate. The controls aren't created in Init, but in Load. With...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.