Iterate through a reference of controls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • janetb
    New Member
    • Aug 2006
    • 19

    Iterate through a reference of controls

    If I have 3 text boxes with ids of txtBox1, txtBox2, txtBox3, how does one reference them for the text values?

    Dim varStart as integer=1
    Dim varEnd as integer=3
    Dim myUID As TextBox

    for i = varStart to varEnd
    myUID.ID = ("txtUID" & i.ToString)
    If (Trim(CType(myU ID, TextBox).Text) <> "") then
    do something
    End if
    next
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    I'm a bit confused by your question. Generally you would just reference them txtBox1.Text, txtBox2.Text and so on...

    Can you elaborate a bit more?

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      You would need to use .FindControl(st ring id) if you are on a webpage.
      If you are in a windows form, you will need to create the functionality of .FindControl

      Comment

      • janetb
        New Member
        • Aug 2006
        • 19

        #4
        Plater,
        Thanks for the reply. Still a bit confused about the exact syntax.

        Dim myUID As String
        myUID = "txtUID" & i.ToString
        Page.FindContro l(CType(myUID, TextBox))

        above tells me string cannot be converted to textbox

        Dim myUID As TextBox
        myUID.ID = "txtUID" & i.ToString

        above stops me before I can reference using .findControl that the Object reference not set to an instance of an object.

        Can you tell me what you mean? Thanks so much.

        Comment

        • balabaster
          Recognized Expert Contributor
          • Mar 2007
          • 798

          #5
          Originally posted by janetb
          Plater,
          Thanks for the reply. Still a bit confused about the exact syntax.

          Dim myUID As String
          myUID = "txtUID" & i.ToString
          Page.FindContro l(CType(myUID, TextBox))

          above tells me string cannot be converted to textbox

          Dim myUID As TextBox
          myUID.ID = "txtUID" & i.ToString

          above stops me before I can reference using .findControl that the Object reference not set to an instance of an object.

          Can you tell me what you mean? Thanks so much.

          The CType should wrap the FindControl, not the other way around... right now you're trying to convert a string to a textbox and then you're telling the code to go find that object... Page.FindContro l requires a string parameter - the name of the control:

          Code:
          Dim MyObj As TextBox = DirectCast(Page.FindControl("txtUID"), TextBox)
          Do you see the difference?

          Comment

          • janetb
            New Member
            • Aug 2006
            • 19

            #6
            Yeah, I do get it now. Thanks for the explanation. That really helps. But, why doesn't this work? And/or how would you get it to work to reference and/or set the value of the text box? Thanks so much.

            Dim myUID As String = "txtUID" & i.ToString
            Response.Write( DirectCast(Page .FindControl(my UID), TextBox).Text)

            Comment

            • balabaster
              Recognized Expert Contributor
              • Mar 2007
              • 798

              #7
              Originally posted by janetb
              Yeah, I do get it now. Thanks for the explanation. That really helps. But, why doesn't this work? And/or how would you get it to work to reference and/or set the value of the text box? Thanks so much.

              Dim myUID As String = "txtUID" & i.ToString
              Response.Write( DirectCast(Page .FindControl(my UID), TextBox).Text)
              Assuming that Page.FindContro l(myUID) returns a control which it may not if it's embedded within an UpdatePanel or some other naming container control...for instance a user control, then the control ID may not actually be "txtUID"& i (which you don't need ToString() on)...

              So, if this is returning the control, then you should be able to write it out to the page as you are... my guess therefore is that the underlying name of the control isn't what you think it is.

              In order to go the other way (to write to the textbox)

              Code:
              DirectCast(Page.FindControl(myUID), TextBox).Text = "Hello World"

              Comment

              • janetb
                New Member
                • Aug 2006
                • 19

                #8
                Never could get it right, so I'm coming at it a different way.

                Created a datagrid, and want to change the text in a column and a footer cell based on changes in two dropdownlists. The first dropdownlist changes the text in the fourth column fine, but I can't get it to change the footer text. Each columm in the datagrid has a <footertemplate > The second dropdownlist is in the datagrid, but doesn't seem to fire the doCalc sub.

                Protected Sub ddlMember_Selec tedIndexChanged (ByVal sender As Object, ByVal e As System.EventArg s) Handles ddlMember.Selec tedIndexChanged
                doCalc()

                fires okay for:

                Code:
                Sub doCalc()
                Dim varTotal As Integer = 0
                For Each i As DataGridItem In dgList.Items
                If i.ItemType = ListItemType.AlternatingItem Or i.ItemType = istItemType.Item then
                Select Case ddlMember.SelectedIndex
                   Case 1
                If CType(i.Cells(3).FindControl("ddlDel"), DropDownList).SelectedIndex = 0 Then
                varTotal += 10
                CType(i.Cells(4).FindControl("lblCost"), Label).Text = "10"
                Else
                CType(i.Cells(4).FindControl("lblCost"), Label).Text = "15"
                varTotal += 15
                End If
                yahda yahda
                end case
                      ElseIf i.ItemType = ListItemType.Footer Then
                           i.Cells(4).Text = varTotal.ToString
                End If

                The following never seems to fire:

                <asp:DropDownLi st ID="ddlDel" runat="server" AutoPostBack="t rue" OnSelectedIndex Changed="doDelC alc" >

                Sub doDelCalc(ByVal sender As Object, ByVal e As System.EventArg s)
                doCalc()
                End Sub

                Comment

                • janetb
                  New Member
                  • Aug 2006
                  • 19

                  #9
                  Got the delegate datagrid dropdownlist to fire. Now, last piece is getting the footer to update. Anybody got any pointers?

                  Thanks so much.

                  Comment

                  • balabaster
                    Recognized Expert Contributor
                    • Mar 2007
                    • 798

                    #10
                    Originally posted by janetb
                    Got the delegate datagrid dropdownlist to fire. Now, last piece is getting the footer to update. Anybody got any pointers?

                    Thanks so much.
                    Make sure DoCalc() is fired in or after the event is raised. Events are fired after the page load so if DoCalc() is fired in the page load and then the event runs, DoCalc() is never going to update your footer...

                    Comment

                    • janetb
                      New Member
                      • Aug 2006
                      • 19

                      #11
                      Okay balablaster, you've been great and I'm sorry to ask, but I'm confused. The doCalc is fired at will after the page has rendered when the client does something on the screen. So, the datagrid, column cells are changed, but the footer cells are not changing.

                      Sub doCalc()
                      Dim varTotal As Integer = 0
                      For Each i As DataGridItem In dgList.Items
                      If (i.ItemType = ListItemType.Al ternatingItem Or i.ItemType = ListItemType.It em) Then
                      varTotal += 30
                      ElseIf i.ItemType = ListItemType.Fo oter Then
                      i.Cells(4).Text = varTotal.ToStri ng
                      End If
                      Next
                      End sub

                      Comment

                      • balabaster
                        Recognized Expert Contributor
                        • Mar 2007
                        • 798

                        #12
                        Originally posted by janetb
                        Okay balablaster, you've been great and I'm sorry to ask, but I'm confused. The doCalc is fired at will after the page has rendered when the client does something on the screen. So, the datagrid, column cells are changed, but the footer cells are not changing.

                        Sub doCalc()
                        Dim varTotal As Integer = 0
                        For Each i As DataGridItem In dgList.Items
                        If (i.ItemType = ListItemType.Al ternatingItem Or i.ItemType = ListItemType.It em) Then
                        varTotal += 30
                        ElseIf i.ItemType = ListItemType.Fo oter Then
                        i.Cells(4).Text = varTotal.ToStri ng
                        End If
                        Next
                        End sub
                        And DoCalc is being fired? At first glance that code looks like it should work...

                        Comment

                        • janetb
                          New Member
                          • Aug 2006
                          • 19

                          #13
                          Yep, anytime the dropdownlist outside the datagrid changes, or one of the textboxes within the datagrid changes, or the dropdownlist in one of the datagrid cells changes, I fire the doCalc and the cells in the fourth column of the datagrid change on the fly. But, I can't get the footer to show any changes....

                          Comment

                          • janetb
                            New Member
                            • Aug 2006
                            • 19

                            #14
                            I can reference a textbox outside of the datagrid and change THAT at the time the calculation is done. But I can't get the text in a footer cell or a label text located in a footer cell to change. That help?

                            Comment

                            • balabaster
                              Recognized Expert Contributor
                              • Mar 2007
                              • 798

                              #15
                              Originally posted by janetb
                              I can reference a textbox outside of the datagrid and change THAT at the time the calculation is done. But I can't get the text in a footer cell or a label text located in a footer cell to change. That help?
                              If you breakpoint on the ElseIf does it step in there? What if you add a watch on i.Cells(4)? Does it show an object or no?

                              Comment

                              Working...