RegisterHiddenField question.

Discussion in 'ASP .Net' started by ashish, Mar 10, 2005.

  1. ashish

    ashish Guest

    If i want to add a hidden field on the page, how can i check whether
    that hidden field exists ?

    for example if i do

    If Page.FindControl("myhiddenfield") Is Nothing Then
    Page.RegisterHiddenField("myhiddenfield", "hidden")
    End If


    Dim myHiddenField As HtmlInputHidden = Page.FindControl("myhiddenfield")
    ** this line would throw error


    how can i know that the hidden field is there before trying to add it ?

    TIA
    -ashish
     
    ashish, Mar 10, 2005
    #1
    1. Advertising

  2. ashish

    Karl Seguin Guest

    Ashish:
    Page.RegisterHiddenField doesn't create a server side control, it just
    creates a plain-old <input type="hidden">..so
    Page.FindControl("myhiddenField") will never find anything....

    If you call RegisterHiddenField twice with the same id, it'll only register
    the field once, so:
    Page.RegisterHiddenField("myhiddenfield", "hidden")
    Page.RegisterHiddenField("myhiddenfield", "hidden")

    there'll only be 1 myHiddenField

    There's no way to tell which hidden fields have been registered this
    way...on postback you can use Request.Form("myhiddenField") to get the
    information though

    You could always create a server control via:
    dim myHiddenField as new HtmlInputHidden()
    somePlaceHolder.Controls.Add(myHiddenField)


    Karl


    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)


    "ashish" <> wrote in message
    news:...
    > If i want to add a hidden field on the page, how can i check whether
    > that hidden field exists ?
    >
    > for example if i do
    >
    > If Page.FindControl("myhiddenfield") Is Nothing Then
    > Page.RegisterHiddenField("myhiddenfield", "hidden")
    > End If
    >
    >
    > Dim myHiddenField As HtmlInputHidden = Page.FindControl("myhiddenfield")
    > ** this line would throw error
    >
    >
    > how can i know that the hidden field is there before trying to add it ?
    >
    > TIA
    > -ashish
    >
     
    Karl Seguin, Mar 10, 2005
    #2
    1. Advertising

  3. If you register the same field twice with two different values, the first
    value takes precedence.

    Page.RegisterHiddenField( "myHiddenField", "value1" );
    Page.RegisterHiddenField( "myHiddenField", "value2" );

    "value1" will be rendered. The second call is ignored.

    bill

    "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    wrote in message news:...
    > Ashish:
    > Page.RegisterHiddenField doesn't create a server side control, it just
    > creates a plain-old <input type="hidden">..so
    > Page.FindControl("myhiddenField") will never find anything....
    >
    > If you call RegisterHiddenField twice with the same id, it'll only

    register
    > the field once, so:
    > Page.RegisterHiddenField("myhiddenfield", "hidden")
    > Page.RegisterHiddenField("myhiddenfield", "hidden")
    >
    > there'll only be 1 myHiddenField
    >
    > There's no way to tell which hidden fields have been registered this
    > way...on postback you can use Request.Form("myhiddenField") to get the
    > information though
    >
    > You could always create a server control via:
    > dim myHiddenField as new HtmlInputHidden()
    > somePlaceHolder.Controls.Add(myHiddenField)
    >
    >
    > Karl
    >
    >
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
    > http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    > come!)
    >
    >
    > "ashish" <> wrote in message
    > news:...
    > > If i want to add a hidden field on the page, how can i check whether
    > > that hidden field exists ?
    > >
    > > for example if i do
    > >
    > > If Page.FindControl("myhiddenfield") Is Nothing Then
    > > Page.RegisterHiddenField("myhiddenfield", "hidden")
    > > End If
    > >
    > >
    > > Dim myHiddenField As HtmlInputHidden = Page.FindControl("myhiddenfield")
    > > ** this line would throw error
    > >
    > >
    > > how can i know that the hidden field is there before trying to add it ?
    > >
    > > TIA
    > > -ashish
    > >

    >
    >
     
    William F. Robertson, Jr., Mar 11, 2005
    #3
    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. Matthew Wieder
    Replies:
    1
    Views:
    383
    Yan-Hong Huang[MSFT]
    Jul 22, 2003
  2. MeDhanush
    Replies:
    1
    Views:
    923
    Natty Gur
    Aug 28, 2003
  3. Kevin Spencer

    RegisterHiddenField and trace

    Kevin Spencer, Oct 21, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    2,090
    Erik Cruz
    Oct 22, 2003
  4. Francis
    Replies:
    0
    Views:
    679
    Francis
    Nov 3, 2003
  5. Cliff Harris

    Re-RegisterHiddenField

    Cliff Harris, Nov 5, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    404
    Cliff Harris
    Nov 5, 2003
Loading...

Share This Page