473,378 Members | 1,394 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,378 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 7413
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.