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) | | | | re: What's better, a hidden form field or viewstate?
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" <alan-silver@nospam.thanx> wrote in message
news:1FUWQQFv+$wCFwET@nospamthankyou.spam...[color=blue]
> 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)[/color] | | | | re: What's better, a hidden form field or viewstate?
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
[color=blue]
> 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
>[/color] | | | | re: What's better, a hidden form field or viewstate?
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 + "'>";
"Tom.PesterDELETETHISSS@pandora.be" wrote:
[color=blue]
> 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
>[color=green]
> > 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
> >[/color]
>
>
>[/color] | | | | re: What's better, a hidden form field or viewstate?
>A hidden form field is used if you need to pass values between server and[color=blue]
>client. If all you need is just to persist a value between postbacks, use
>viewstate.[/color]
Thanks to both of you.
[color=blue]
>Eliyahu
>
>"Alan Silver" <alan-silver@nospam.thanx> wrote in message
>news:1FUWQQFv+$wCFwET@nospamthankyou.spam...[color=green]
>> 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)[/color]
>
>[/color]
--
Alan Silver
(anything added below this line is nothing to do with me) | | | | re: What's better, a hidden form field or viewstate?
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
"Tom.PesterDELETETHISSS@pandora.be" wrote:
[color=blue]
> 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
>[color=green]
> > 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
> >[/color]
>
>
>[/color] | | | | re: What's better, a hidden form field or viewstate?
Alan Silver wrote:[color=blue]
> 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[/color]
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 | | | | re: What's better, a hidden form field or viewstate?
"Alan Silver" <alan-silver@nospam.thanx> wrote in message
news:1FUWQQFv+$wCFwET@nospamthankyou.spam...[color=blue]
> 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)[/color]
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 | | | | re: What's better, a hidden form field or viewstate?
>If it's just an ID, what about storing it in a cookie? That will keep[color=blue]
>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.[/color]
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) | | | | re: What's better, a hidden form field or viewstate?
>If you use a hidden field, you can access it on the aspx page code behind by[color=blue]
>declaring it runat='server' and adding it to the list of controls in the
>codebehind.[/color]
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) | | | | re: What's better, a hidden form field or viewstate?
"Alan Silver" <alan-silver@nospam.thanx> wrote in message
news:OZjYnxO19BxCFwLt@nospamthankyou.spam...[color=blue]
> Also, you can't always be sure the user has cookies enabled.[/color]
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 | | | | re: What's better, a hidden form field or viewstate?
Thats a session cookie and most people allow that kind. Its the persistent
cookies that get blokked.
Cheers,
Tom Pester
[color=blue]
> "Alan Silver" <alan-silver@nospam.thanx> wrote in message
> news:OZjYnxO19BxCFwLt@nospamthankyou.spam...
>[color=green]
>> Also, you can't always be sure the user has cookies enabled.
>>[/color]
> 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
>[/color] | | | | re: What's better, a hidden form field or viewstate?
>> Also, you can't always be sure the user has cookies enabled.[color=blue]
>
>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 :)[/color]
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) |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,358 network members.
|