Array of form objects?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Mason

    Array of form objects?

    Hi,

    I have 5 separate placeholders within a web form. Each contain varying
    amounts of textboxes and labels that were dynamically created and added.

    When retrieving the various textbox id's and text values I use a
    for..next loop to step through:

    For a =..
    CType(plcAttend ance2.Controls( a), TextBox).ID
    CType(plcAttend ance2.Controls( a), TextBox).Text
    Next

    Could someone pls show me how I can dynamically refer to each
    placeholder? Something like..

    For b = 1 to 5
    For a = ...
    CType(plcAttend ance[b].Controls(a), TextBox).ID
    CType(plcAttend ance[b].Controls(a), TextBox).Text
    Next
    Next

    ??

    This would save me a lot of time with coding!

    Any help much appreciated..



    *** Sent via Developersdex http://www.developersdex.com ***
  • societopia.net

    #2
    RE: Array of form objects?

    "John Mason" wrote:
    [color=blue]
    > Hi,
    >
    > I have 5 separate placeholders within a web form. Each contain varying
    > amounts of textboxes and labels that were dynamically created and added.
    >
    > When retrieving the various textbox id's and text values I use a
    > for..next loop to step through:
    >
    > For a =..
    > CType(plcAttend ance2.Controls( a), TextBox).ID
    > CType(plcAttend ance2.Controls( a), TextBox).Text
    > Next
    >
    > Could someone pls show me how I can dynamically refer to each
    > placeholder? Something like..
    >
    > For b = 1 to 5
    > For a = ...
    > CType(plcAttend ance[b].Controls(a), TextBox).ID
    > CType(plcAttend ance[b].Controls(a), TextBox).Text
    > Next
    > Next
    >
    > ??
    >
    > This would save me a lot of time with coding!
    >
    > Any help much appreciated..[/color]

    Use the typeof operator to programmaticall y identify the control type during
    run-time, e.g.,

    Dim cntrl As Control
    For Each cntrl In plcAttendance1. Parent.Controls
    If TypeOf cntrl Is PlaceHolder Then
    Dim childcntrl As Control
    For Each childcntrl In cntrl.Controls
    If TypeOf childcntrl Is TextBox Then
    Response.Write( CType(childcntr l, TextBox).ID)
    Response.Write( CType(childcntr l, TextBox).Text)
    End If
    Next
    End If
    Next

    ---


    Comment

    • John Mason

      #3
      RE: Array of form objects?

      Thanks for that. Is there a way of reiterating through a group of
      placeholders? I have 5 placeholders named...

      plcAttendance1
      plcAttendance2
      ..
      ..

      I would prefer to reiterate through all of them at once instead of
      writing separate code for each.

      Thanks!



      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • societopia.net

        #4
        Re: Array of form objects?

        If the placeholder controls are spread in different places on the page you
        need a recursive function to find them. For example the following function
        would recur through a collection of controls to find TextBox controls within
        PlaceHolder controls:


        private void FindMyControls( ControlCollecti on cntrlcol)
        {
        foreach (Control cntrl in cntrlcol)
        {
        if (cntrl is PlaceHolder) {
        foreach(Control childControl in cntrl.Controls) {
        Response.Write( ((TextBox)child Control).ID);
        }
        }
        else if (cntrl.HasContr ols())
        {
        FindMyControls( cntrl.Controls) ;
        }
        }
        }

        You can call this function as follows:
        FindMyControls( Page.Controls);

        ----


        "John Mason" <wollondilly@ho tmail.com> wrote in message
        news:urizwuMiFH A.3448@TK2MSFTN GP12.phx.gbl...[color=blue]
        > Thanks for that. Is there a way of reiterating through a group of
        > placeholders? I have 5 placeholders named...
        >
        > plcAttendance1
        > plcAttendance2
        > .
        > .
        >
        > I would prefer to reiterate through all of them at once instead of
        > writing separate code for each.
        >
        > Thanks!
        >
        >
        >
        > *** Sent via Developersdex http://www.developersdex.com ***[/color]


        Comment

        • John Mason

          #5
          Re: Array of form objects?

          Thanks very much www.societopia.net

          That is most helpful!

          *** Sent via Developersdex http://www.developersdex.com ***

          Comment

          Working...