Iterate through a reference of controls | Newbie | | Join Date: Aug 2006
Posts: 17
| | |
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(myUID, TextBox).Text) <> "") then
do something
End if
next
|  | Expert | | Join Date: Mar 2008 Location: Essex, UK
Posts: 197
| | | re: Iterate through a reference of controls
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?
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,150
| | | re: Iterate through a reference of controls
You would need to use .FindControl(string id) if you are on a webpage.
If you are in a windows form, you will need to create the functionality of .FindControl
| | Newbie | | Join Date: Aug 2006
Posts: 17
| | | re: Iterate through a reference of controls
Plater,
Thanks for the reply. Still a bit confused about the exact syntax.
Dim myUID As String
myUID = "txtUID" & i.ToString
Page.FindControl(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.
|  | Moderator | | Join Date: Mar 2007 Location: Canada
Posts: 757
| | | re: Iterate through a reference of controls Quote:
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.FindControl(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.FindControl requires a string parameter - the name of the control: - Dim MyObj As TextBox = DirectCast(Page.FindControl("txtUID"), TextBox)
Do you see the difference?
| | Newbie | | Join Date: Aug 2006
Posts: 17
| | | re: Iterate through a reference of controls
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(myUID), TextBox).Text)
|  | Moderator | | Join Date: Mar 2007 Location: Canada
Posts: 757
| | | re: Iterate through a reference of controls Quote:
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(myUID), TextBox).Text) Assuming that Page.FindControl(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) - DirectCast(Page.FindControl(myUID), TextBox).Text = "Hello World"
| | Newbie | | Join Date: Aug 2006
Posts: 17
| | | re: Iterate through a reference of controls
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_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlMember.SelectedIndexChanged
doCalc()
fires okay for: - 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:DropDownList ID="ddlDel" runat="server" AutoPostBack="true" OnSelectedIndexChanged="doDelCalc" >
Sub doDelCalc(ByVal sender As Object, ByVal e As System.EventArgs)
doCalc()
End Sub
| | Newbie | | Join Date: Aug 2006
Posts: 17
| | | re: Iterate through a reference of controls
Got the delegate datagrid dropdownlist to fire. Now, last piece is getting the footer to update. Anybody got any pointers?
Thanks so much.
|  | Moderator | | Join Date: Mar 2007 Location: Canada
Posts: 757
| | | re: Iterate through a reference of controls Quote:
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...
| | Newbie | | Join Date: Aug 2006
Posts: 17
| | | re: Iterate through a reference of controls
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.AlternatingItem Or i.ItemType = ListItemType.Item) Then
varTotal += 30
ElseIf i.ItemType = ListItemType.Footer Then
i.Cells(4).Text = varTotal.ToString
End If
Next
End sub
|  | Moderator | | Join Date: Mar 2007 Location: Canada
Posts: 757
| | | re: Iterate through a reference of controls Quote:
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.AlternatingItem Or i.ItemType = ListItemType.Item) Then
varTotal += 30
ElseIf i.ItemType = ListItemType.Footer Then
i.Cells(4).Text = varTotal.ToString
End If
Next
End sub And DoCalc is being fired? At first glance that code looks like it should work...
| | Newbie | | Join Date: Aug 2006
Posts: 17
| | | re: Iterate through a reference of controls
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....
| | Newbie | | Join Date: Aug 2006
Posts: 17
| | | re: Iterate through a reference of controls
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?
|  | Moderator | | Join Date: Mar 2007 Location: Canada
Posts: 757
| | | re: Iterate through a reference of controls Quote:
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?
| | Newbie | | Join Date: Aug 2006
Posts: 17
| | | re: Iterate through a reference of controls
It appears that
doCalc() will allow me to iterate through the datagrid rows and change cells but not access the footer? I tried to change the text in a text box at the "ElseIf i.ItemType = ListItemType.Footer Then" point and it didn't work. The doCalc() doesn't explicitly call the datagrideventargs cause I want to call it from multiple sources (textbox textchanged, dropdownlist selected indexes, etc.) Can I explictly call the footer in code without chaning the doCalc()?
Cheers, and many thanks,
Janet
|  | Moderator | | Join Date: Mar 2007 Location: Canada
Posts: 757
| | | re: Iterate through a reference of controls Quote:
Originally Posted by janetb It appears that
doCalc() will allow me to iterate through the datagrid rows and change cells but not access the footer? I tried to change the text in a text box at the "ElseIf i.ItemType = ListItemType.Footer Then" point and it didn't work. The doCalc() doesn't explicitly call the datagrideventargs cause I want to call it from multiple sources (textbox textchanged, dropdownlist selected indexes, etc.) Can I explictly call the footer in code without chaning the doCalc()?
Cheers, and many thanks,
Janet Hmm... I've not worked with DataGrid in forever and I'm assuming this is .NET 1.1 as I've only got GridView in both 2005/2008 in Web Applications and DataGridView in both in Windows apps...
With a GridView you would do something like:
DirectCast(MyGridView.FooterRow.FindControl("TextO bjectToUpdate"), TextBox).Text = "NewValue".
Can you do this type of thing with the DataGrid? Sadly, I don't have access to the tools to help you out with this...
|  | Similar .NET Framework bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|