I’m trying to come up with a way to loop thru all the controls ( textboxes, check boxes, dropdown lists,etc) on an aspx page. I tried converting some of the solutions given on line for C# to VB but nothing works.
I use the
for all c in me.controls
id = c.id
next
loop but don’t get any of the controls on the page. I have also tried this with the same result:
Sub checkcontrol(ByRef Parent As Control)
Dim c As Control
Dim x As Integer = Parent.Controls.Count
For Each c In Parent.Controls
If c.GetType.ToString = "ASP.site_master" Then
checkcontrol(c)
ElseIf c.GetType() Is GetType(TextBox) Then
'is it a Text Box?
Dim t As TextBox = c
Debug.Print("textBox " & c.ID)
ElseIf c.GetType() Is GetType(DropDownList) Then
'is it a dropdown list?
Dim d As DropDownList = c
Debug.Print("DropDown Box " & c.ID)
' d.ClearSelection()
ElseIf c.GetType() Is GetType(CheckBox) Then
Debug.Print("check Box " & c.ID)
ElseIf c.GetType() Is GetType(RadioButton) Then
Debug.Print("Radio Button " & c.ID)
End If
Next
End sub
The first type I get is "ASP.site_master" since the page is linked to the Site Master.I’m on Visual studio community 2017 and running in the debug mode.
What am I doing wrong?
2
Answers
There could be more layers. Always look for child controls:
For fun:
And then use it like this:
Actually, I become super tried of writing code to take a data table row, send to controls on the web page. Let user edit, and then send the values of the controls back to the database. Coming from VBA, vb, vb.net desktop, then it seemed like a silly waste of time to write such code over and over.
So, I wrote a routine do loop the controls. However, on a given page, I often need more then one section (one set of values might come from one table, and another section comes from another table).
So, I assume the grouping of controls I want to work with is placed in a div.
So, say I want to edit a hotel information.
And I don’t want to wear out my keyboard.
And I don’t want to write the same code over and over every time I want to edit a record?
well, we build 2 routines.
Routine to send a data row to the web page – (but, we use a "div" to control what parts).
Routine to send web page back to data row.
And then routine to send data row (data table) back to database.
The only other part? How do we data bind the controls? Well, we can just out of the blue add any old markup to each control. So, I just use "f" and that means the data column name.
The end result?
We actually wind up with a "mini" frame work to now edit data, do so with out messy binding expressions, and do so without messy templates, or anything.
Just simple code, simple markup, and you now have a full CRUD system.
Example:
Simple grid view display some hotels:
Ok, code to load:
And we now have this:
So far, nothing new – dead simple. (don’t worry, the helper routines I’ll share in a bit).
Ok, so, in above we dropped in a plane jane asp.net button into the grid view (don’t bother with the built-in events for the GV – they are more pain then help).
Now, normally, to add a click even to a button, you just double click on such buttons, but since buttons (or any control) inside of a GV does not allow this?
Then you use markup, type in onclick="". You get/see this:
so, choose create new event. Now, we get to use a simple click event in the gridview (this trick works for all controls – drop downs or whatever). This trick saves us world poverty, and we now get to enjoy and use a simple standard button click event for the GV – no messy on row command and that world poverty’s causing efforts.
When we click on a row, we will:
hide the GV, display our div to edit the row. Use our handy page looping controls routine (your current question), and fill out the div.
So, first our div we drop below the grid – this is our edit div.
say this:
Now, in above, I decided to use just a html button, and the reason why is EASE in which to add icons to the buttons. (but, plane jane asp.net buttons could also have been used).
Noting really special in above EXCEPT for our adding of "f". That’s the data base column mapping!!! Really simple, and a gazillion times less work then say using a databound object like repeater or whatever.
So, now the button code click:
Once again, the above code – very simple, very plain.
We now see this when I hit edit on a given row:
And the save button code:
Again, note how simple the code is. note that I COULD HAVE combined the two routines into one (fwriterW, and SaveTable). but, often after we pull data from web form back to table, we might want to do some additonal process or code on the data BEFORE we save.
So, now the looping code. We have two such routines:
data to web page
web page to data table.
So, this is the looping controls part:
first the loader:
You can over time add a few more control types if you use them. So, note how the above loops the controls, fills them based on "f" attribute setting.
And in my global set of routines, we have also
This is really just the reverse of the first loop.
So, we pass these routines a "div" (with id, and runat server always).
So the only other two helper routines are:
Again, I use the above everyone – in all my code. Again, super easy.
And then the code to write the table back:
So, that’s about it.
The above in fact is a whole re-useable frame work. It allows you now re-use the above code – allows easy edit of any data. And you not re-writing the same code over and over.
Adopting above code quite gives you true RAD development, and great ease of editing records in web forms – in fact it becomes as easy as desktop software.
So, in most cases looping every control on the page is not all so useful. But, placing controls in a "div" (with id, and runat=server) now allows you to pass THAT single div. It also means that you can have multiple parts on a page – maybe a table of billing address and customer record.
and all of the above just simple works – including when you have a master page.