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

Discussion in 'ASP .Net' started by Alan Silver, Jun 30, 2005.

  1. Alan Silver

    Alan Silver Guest

    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, Jun 30, 2005
    #1
    1. Advertising

  2. Alan Silver

    Guest

    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
    >
     
    , Jun 30, 2005
    #2
    1. Advertising

  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 + "'>";

    "" 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
    > >

    >
    >
    >
     
    =?Utf-8?B?QW5keSBSb3Nl?=, Jun 30, 2005
    #3
  4. Alan Silver

    Alan Silver Guest

    >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" <> wrote in message
    >news:1FUWQQFv+$...
    >> 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)
     
    Alan Silver, Jun 30, 2005
    #4
  5. Alan Silver

    Hans Kesting Guest

    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
     
    Hans Kesting, Jun 30, 2005
    #5
  6. 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


    "" 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
    > >

    >
    >
    >
     
    =?Utf-8?B?TWljaGFlbCBCYWx0aWM=?=, Jun 30, 2005
    #6
  7. Alan Silver

    Mythran Guest

    "Alan Silver" <> wrote in message
    news:1FUWQQFv+$...
    > 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
     
    Mythran, Jun 30, 2005
    #7
  8. 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" <> wrote in message
    news:1FUWQQFv+$...
    > 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)
     
    Eliyahu Goldin, Jun 30, 2005
    #8
  9. Alan Silver

    Alan Silver Guest

    >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)
     
    Alan Silver, Jun 30, 2005
    #9
  10. Alan Silver

    Alan Silver Guest

    >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)
     
    Alan Silver, Jun 30, 2005
    #10
  11. Alan Silver

    Mythran Guest

    "Alan Silver" <> wrote in message
    news:...
    > 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
     
    Mythran, Jun 30, 2005
    #11
  12. Alan Silver

    Guest

    Thats a session cookie and most people allow that kind. Its the persistent
    cookies that get blokked.


    Cheers,
    Tom Pester

    > "Alan Silver" <> wrote in message
    > news:...
    >
    >> 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
    >
     
    , Jun 30, 2005
    #12
  13. Alan Silver

    Alan Silver Guest

    >> 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)
     
    Alan Silver, Jun 30, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Umut Tezduyar
    Replies:
    4
    Views:
    3,393
    Ravi Ambros Wallau
    Feb 15, 2006
  2. Stimp
    Replies:
    3
    Views:
    2,800
    Scott M.
    Sep 10, 2006
  3. Rick
    Replies:
    3
    Views:
    8,594
    Alexey Smirnov
    Apr 13, 2010
  4. Jeff
    Replies:
    5
    Views:
    1,222
  5. GavMc
    Replies:
    4
    Views:
    382
    Evertjan.
    Sep 22, 2005
Loading...

Share This Page