Any Way to Limit Instances of a Web User Control Element?

Discussion in 'ASP .Net' started by Jonathan Wood, May 4, 2009.

  1. I'm creating a Web User Control. It needs to include a few component
    controls. But one of those component controls, a HiddenField control, should
    only appear once on the entire page (at least with that name).

    I'm not sure this is doable, but does anyone know if there's any way to
    limit the number of instances on a page of a component of a Web User Control
    to one, even if there are multiple instances of the Web User Control?

    Thanks for any thoughts.

    Jonathan
    Jonathan Wood, May 4, 2009
    #1
    1. Advertising

  2. Jonathan Wood

    Joe Fawcett Guest

    Can't you just implement INamingContainer on the user control and then the
    hidden field will always have a unique name?

    --

    Joe Fawcett (MVP - XML)
    http://joe.fawcett.name
    "Jonathan Wood" <> wrote in message
    news:...
    > I'm creating a Web User Control. It needs to include a few component
    > controls. But one of those component controls, a HiddenField control,
    > should only appear once on the entire page (at least with that name).
    >
    > I'm not sure this is doable, but does anyone know if there's any way to
    > limit the number of instances on a page of a component of a Web User
    > Control to one, even if there are multiple instances of the Web User
    > Control?
    >
    > Thanks for any thoughts.
    >
    > Jonathan
    >
    Joe Fawcett, May 4, 2009
    #2
    1. Advertising

  3. Howdy Jonathan!

    Pop this logic somewhere in your user control.

    If (Not
    Me.Page.ClientScript.IsStartupScriptRegistered("HiddenFieldIsRegistered"))
    Then
    Dim hdn_Main As New HiddenField
    With hdn_Main
    .ID = "hdn_Main"
    End With
    Me.Page.Form.Controls.Add(hdn_Main)
    Me.Page.ClientScript.RegisterStartupScript(Me.GetType(),
    "HiddenFieldIsRegistered", "Yes", True)
    End If

    "Jonathan Wood" wrote:

    > I'm creating a Web User Control. It needs to include a few component
    > controls. But one of those component controls, a HiddenField control, should
    > only appear once on the entire page (at least with that name).
    >
    > I'm not sure this is doable, but does anyone know if there's any way to
    > limit the number of instances on a page of a component of a Web User Control
    > to one, even if there are multiple instances of the Web User Control?
    >
    > Thanks for any thoughts.
    >
    > Jonathan
    >
    >
    William Niver, May 4, 2009
    #3
  4. I'm not familiar with INamingContainer but I need exactly one instance of
    the HiddenField with a pre-determined name.

    Thanks.

    Jonathan

    "Joe Fawcett" <> wrote in message
    news:...
    > Can't you just implement INamingContainer on the user control and then the
    > hidden field will always have a unique name?
    >
    > --
    >
    > Joe Fawcett (MVP - XML)
    > http://joe.fawcett.name
    > "Jonathan Wood" <> wrote in message
    > news:...
    >> I'm creating a Web User Control. It needs to include a few component
    >> controls. But one of those component controls, a HiddenField control,
    >> should only appear once on the entire page (at least with that name).
    >>
    >> I'm not sure this is doable, but does anyone know if there's any way to
    >> limit the number of instances on a page of a component of a Web User
    >> Control to one, even if there are multiple instances of the Web User
    >> Control?
    >>
    >> Thanks for any thoughts.
    >>
    >> Jonathan
    >>

    >
    >
    >
    Jonathan Wood, May 4, 2009
    #4
  5. <g>

    You know, I kept thinking last night that what I needed was something like
    ClientScript except for HTML instead of javascript!

    I just put this together and it appears to be working perfectly.

    I did get an error that "Yes" was undefined so I changed it to
    Page.ClientScript.RegisterStartupScript(typeof(Page),
    "HiddenFieldIsRegistered", "", false); and that seems okay.

    Also, I like working with ClientScript because I plan to use it here for
    other stuff in my control as well. (I'm thinking of writing client script to
    copy the control-specific value to the one-and-only hidden field when my
    control is clicked.)

    Thanks again!

    Jonathan

    "William Niver" <> wrote in message
    news:D...
    > Howdy Jonathan!
    >
    > Pop this logic somewhere in your user control.
    >
    > If (Not
    > Me.Page.ClientScript.IsStartupScriptRegistered("HiddenFieldIsRegistered"))
    > Then
    > Dim hdn_Main As New HiddenField
    > With hdn_Main
    > .ID = "hdn_Main"
    > End With
    > Me.Page.Form.Controls.Add(hdn_Main)
    > Me.Page.ClientScript.RegisterStartupScript(Me.GetType(),
    > "HiddenFieldIsRegistered", "Yes", True)
    > End If
    >
    > "Jonathan Wood" wrote:
    >
    >> I'm creating a Web User Control. It needs to include a few component
    >> controls. But one of those component controls, a HiddenField control,
    >> should
    >> only appear once on the entire page (at least with that name).
    >>
    >> I'm not sure this is doable, but does anyone know if there's any way to
    >> limit the number of instances on a page of a component of a Web User
    >> Control
    >> to one, even if there are multiple instances of the Web User Control?
    >>
    >> Thanks for any thoughts.
    >>
    >> Jonathan
    >>
    >>
    Jonathan Wood, May 4, 2009
    #5
  6. one thing to be wary of when using the typeof(page) when using the script
    registration is that it won't work when inside of an updating update panel
    except on the initial page load.

    After that, there is no page object to attach it to, so it bombs. In those
    cases, attach it to a control which is included in the update and all will be
    well and good.

    William

    "Jonathan Wood" wrote:

    > <g>
    >
    > You know, I kept thinking last night that what I needed was something like
    > ClientScript except for HTML instead of javascript!
    >
    > I just put this together and it appears to be working perfectly.
    >
    > I did get an error that "Yes" was undefined so I changed it to
    > Page.ClientScript.RegisterStartupScript(typeof(Page),
    > "HiddenFieldIsRegistered", "", false); and that seems okay.
    >
    > Also, I like working with ClientScript because I plan to use it here for
    > other stuff in my control as well. (I'm thinking of writing client script to
    > copy the control-specific value to the one-and-only hidden field when my
    > control is clicked.)
    >
    > Thanks again!
    >
    > Jonathan
    >
    > "William Niver" <> wrote in message
    > news:D...
    > > Howdy Jonathan!
    > >
    > > Pop this logic somewhere in your user control.
    > >
    > > If (Not
    > > Me.Page.ClientScript.IsStartupScriptRegistered("HiddenFieldIsRegistered"))
    > > Then
    > > Dim hdn_Main As New HiddenField
    > > With hdn_Main
    > > .ID = "hdn_Main"
    > > End With
    > > Me.Page.Form.Controls.Add(hdn_Main)
    > > Me.Page.ClientScript.RegisterStartupScript(Me.GetType(),
    > > "HiddenFieldIsRegistered", "Yes", True)
    > > End If
    > >
    > > "Jonathan Wood" wrote:
    > >
    > >> I'm creating a Web User Control. It needs to include a few component
    > >> controls. But one of those component controls, a HiddenField control,
    > >> should
    > >> only appear once on the entire page (at least with that name).
    > >>
    > >> I'm not sure this is doable, but does anyone know if there's any way to
    > >> limit the number of instances on a page of a component of a Web User
    > >> Control
    > >> to one, even if there are multiple instances of the Web User Control?
    > >>
    > >> Thanks for any thoughts.
    > >>
    > >> Jonathan
    > >>
    > >>

    >
    >
    William Niver, May 4, 2009
    #6
  7. Okay, perhaps I'll change it to this.GetType(). I thought I read somewhere
    that there were issues doing anything but Page in most cases. Unfortunately,
    I don't recall why. There seemed to be some confusion regarding this.

    Thanks.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com
    http://www.softcircuits.com/blog/

    "William Niver" <> wrote in message
    news:...
    > one thing to be wary of when using the typeof(page) when using the script
    > registration is that it won't work when inside of an updating update panel
    > except on the initial page load.
    >
    > After that, there is no page object to attach it to, so it bombs. In
    > those
    > cases, attach it to a control which is included in the update and all will
    > be
    > well and good.
    >
    > William
    >
    > "Jonathan Wood" wrote:
    >
    >> <g>
    >>
    >> You know, I kept thinking last night that what I needed was something
    >> like
    >> ClientScript except for HTML instead of javascript!
    >>
    >> I just put this together and it appears to be working perfectly.
    >>
    >> I did get an error that "Yes" was undefined so I changed it to
    >> Page.ClientScript.RegisterStartupScript(typeof(Page),
    >> "HiddenFieldIsRegistered", "", false); and that seems okay.
    >>
    >> Also, I like working with ClientScript because I plan to use it here for
    >> other stuff in my control as well. (I'm thinking of writing client script
    >> to
    >> copy the control-specific value to the one-and-only hidden field when my
    >> control is clicked.)
    >>
    >> Thanks again!
    >>
    >> Jonathan
    >>
    >> "William Niver" <> wrote in message
    >> news:D...
    >> > Howdy Jonathan!
    >> >
    >> > Pop this logic somewhere in your user control.
    >> >
    >> > If (Not
    >> > Me.Page.ClientScript.IsStartupScriptRegistered("HiddenFieldIsRegistered"))
    >> > Then
    >> > Dim hdn_Main As New HiddenField
    >> > With hdn_Main
    >> > .ID = "hdn_Main"
    >> > End With
    >> > Me.Page.Form.Controls.Add(hdn_Main)
    >> > Me.Page.ClientScript.RegisterStartupScript(Me.GetType(),
    >> > "HiddenFieldIsRegistered", "Yes", True)
    >> > End If
    >> >
    >> > "Jonathan Wood" wrote:
    >> >
    >> >> I'm creating a Web User Control. It needs to include a few component
    >> >> controls. But one of those component controls, a HiddenField control,
    >> >> should
    >> >> only appear once on the entire page (at least with that name).
    >> >>
    >> >> I'm not sure this is doable, but does anyone know if there's any way
    >> >> to
    >> >> limit the number of instances on a page of a component of a Web User
    >> >> Control
    >> >> to one, even if there are multiple instances of the Web User Control?
    >> >>
    >> >> Thanks for any thoughts.
    >> >>
    >> >> Jonathan
    >> >>
    >> >>

    >>
    >>
    Jonathan Wood, May 5, 2009
    #7
    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. Dhruv
    Replies:
    6
    Views:
    726
    Dhruv
    Jul 2, 2003
  2. John Wohlbier
    Replies:
    2
    Views:
    358
    Josiah Carlson
    Feb 22, 2004
  3. VK
    Replies:
    8
    Views:
    494
    Joseph Kesselman
    Oct 31, 2006
  4. Avi
    Replies:
    2
    Views:
    628
    Arne Vajhøj
    Apr 12, 2008
  5. Replies:
    8
    Views:
    448
    James Stroud
    Jan 29, 2009
Loading...

Share This Page