Looping through controls in VB.Net Web Form

I need to disable every imagebutton. In VB6, I would have done it like this: 

1 Dim iButton As ImageButton
2 For Each iButton In Me.Controls
3 iButton.Enabled = False
4 Next
In .Net, I get a "Specified cast is not valid" errror on line 2. Using Page.controls in place of Me.Controls also produces the same error. So, I tried this (and multiple variations):
1 Dim ibControl As Object
2 For Each ibControl In Me.Controls
3 If TypeOf ibControl Is ImageButton Then
4 ibControl.enabled = False
5 End If
6 Next
That didnt produce any errors, but also didnt produce any results, as ibControl was never recognized as an ImageButton control.
My only solution this far is this:
Imagebutton01.Enabled = False
Imagebutton02.Enabled = False
Imagebutton03.Enabled = False
Etc...
Which, at best, is sloppy coding. Further, there are *dozens* of ImageButton controls.
Any help will be greatly appreciated
0
AARRGGHHH
8/31/2004 9:01:34 AM
asp.net.web-forms 93655 articles. 5 followers. Follow

3 Replies
1617 Views

Similar Articles

[PageSpeed] 7

Hi,

this is place where recursion is your friend!
Take a look at the next example code I cooked up for you (you can copy paste it to test in webmatrix)

<%@ Page Language="VB" %>
<script runat="server">
Sub LinkButton1_Click(sender As Object, e As EventArgs)
Disable(Me.Controls)
End Sub
Private Sub Disable(cs as ControlCollection)
For Each c as Control in cs
If TypeOf c Is ImageButton Then
DirectCast(c, ImageButton).Enabled = False
End If
Disable(c.Controls)
Next
End Sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:ImageButton id="ImageButton1" runat="server"></asp:ImageButton>
<asp:ImageButton id="ImageButton2" runat="server"></asp:ImageButton>
<asp:ImageButton id="ImageButton3" runat="server"></asp:ImageButton>
</p>
<p>
<asp:ImageButton id="ImageButton4" runat="server"></asp:ImageButton>
<asp:ImageButton id="ImageButton5" runat="server"></asp:ImageButton>
<asp:ImageButton id="ImageButton6" runat="server"></asp:ImageButton>
</p>
<p>
<asp:ImageButton id="ImageButton7" runat="server"></asp:ImageButton>
<asp:ImageButton id="ImageButton8" runat="server"></asp:ImageButton>
<asp:ImageButton id="ImageButton9" runat="server"></asp:ImageButton>
</p>
<p>
<asp:ImageButton id="ImageButton10" runat="server"></asp:ImageButton>
<asp:ImageButton id="ImageButton11" runat="server"></asp:ImageButton>
<asp:ImageButton id="ImageButton12" runat="server"></asp:ImageButton>
</p>
<p>
<asp:LinkButton id="LinkButton1" onclick="LinkButton1_Click" runat="server">Disable imagebuttons</asp:LinkButton>
</p>
</form>
</body>
</html>

Grz, Kris.
Read my blog. Handy Firefox plugins for web developers.


Workaround for non working Mark as answer buttons.
0
XIII
8/31/2004 12:52:56 PM
I use this to clear out a pretty lengthy form after a successful database add.

I call the function like so:

ClearTextBoxes(Page)

And here's the function

Private Sub ClearTextBoxes(ByVal ctrl As Control)
Dim c As Control
For Each c In ctrl.Controls
If TypeOf c Is TextBox Then
CType(c, TextBox).Text = String.Empty
End If
If TypeOf c Is CheckBox Then
CType(c, CheckBox).Checked = False
End If
If TypeOf c Is DropDownList Then
CType(c, DropDownList).Items.Item(0).Selected = True
End If
'recursive call
ClearTextBoxes(c)
Next c
End Sub

I'm sure you can add a check for an Image Button using a similar format.
0
JakeJeck
8/31/2004 1:55:28 PM

Thank you very much, XIII and JJ, for the code samples. I'll try these out tonight, and let you know how it goes. I appreciate the help.
AARRGGHHH
0
AARRGGHHH
9/1/2004 6:27:09 AM
Reply:

Similar Artilces:

windows forms in vb .net into web forms
we are creating a database driven website as part of our project. currently we have forms connecting to the database created in visual basic. these windows forms we need to convert into web forms which are asp .net compatible. can u give me an answer or show me a way. Hi, because of the difference in nature between web and win applications you'll need to reimplement at least the UI part. If you have a multitiered application in which the busines logic and data access are in different parts/assemblies you can reuse them. Grz, Kris.Read my blog. Handy Firefox plugins for web developers.Wor...

Implementing .Net Form into an APS.net web form, can this be done
I am new here, but have been searching for a while, and may not have the correct lingo to find what I am looking for.  I am tasked with implementing a .exe application that was writen vb6 then converted to .net into a new website my team is developing. The idea is to put each of the 3 different forms in this .exe application of 3 different .aspx pages. I have attempted multiple things to get this into the page and even started to just rewrite it as an ASP.net web form, but even then I can't reuse any of the code since the System.Web.UI.Page doesn't inherit the sa...

VB.NET web form Image control SizeMode
The "PictureBox" control in VB.NET has a property called "SizeMode". This allows you to choose how the image will display in the picture box if it is not the same size as the picturebox. However the "image" control for a VB.NET web form (in an ASP.NET application) does not have such a property, instead all images seem to be set to "StretchImage". I want them to be set to "normal", how can I do this? I thought of building my own user control but this would be silly because there must be a simpler way....

How to access a control on a web form from anoter web form
hi all I have this scenarios i have two web forms, one called form1 and the other called form2 form1 has a label with id "lbl1" is it possible to access lbl1 from form2, change its text permenantly? thanks.   refer: http://dotnetslackers.com/Community/blogs/haissam/archive/2007/11/26/ways-to-pass-data-between-webforms.aspxSSNPlease remember to click "Mark as Answer" on the post that helps you. MinaSamy: hi all I have this scenarios i have two web forms, one called form1 and the other called form2 form1 has a label with id "lbl1" is it p...

Can you return .net web controls in a .net page from a web service?
I've got a problem with a project. I've got to compile an entire .net page and return it as part of a Web Service for display. The problem I have is that I need to add WebControls and functionality to the page so that not only the html content and images are displayed (which I've managed) but that textboxes, labels, buttons and datasets can be displayed also. Essentially I need to make a call from one site for a page, populate the page and return it in a state as though it resided on the same server/webspace as the calling page. Code is currently built as follows into a string: strOpenHT...

.Net Web Form & Web Service Deployment
Hi all I have written some test web services and some web form applications and deployed them to my IIS. That's fine but I now want to deploy them to my real IIS server. I have created an msi for each but the problem is I seem to only be able to deploy to the default web site. Our default web site is turned off and instead we use several other sites. Does anybody happen to if Powerbuilder 11 supports non default web sites? If not do I need to hack the msi and if so can anybody suggest a suitable tool. I have Orca but it's not very friendly. Unless of course I knew exactly w...

.Net Windows Control in Web Form
Hi all,How can I use a .Net windows control in a web form? I use object tag but it doesn't work and displays empty.Thanks in advance cheers Hi, you can check this article out: Hosting a Windows Control in a Web Form. This shows only how to host the control. If you want interaction with the client you can do this by setting the correct CAS (Code Access Security) settings on the client pc. Grz, Kris.Read my blog. Handy Firefox plugins for web developers.Workaround for non working Mark as answer buttons. Thanks guys for your replycheers...

Web forms and mobile web forms
Hey everyone, I am looking to develope a web site that depending on the device connecting to it, it will display a certain page. In this case im looking to create a web and wap version of the same project. I have the Web forms version finished using ASP.NET and C# and I am wondering how to create the mobile web form and incorporate it in the project. In the project folder, can I just add a mobile web??? But what about displaying device specific pages?? Cheers -Dudach Yes, you can add mobile web forms and controls to your Web Application project. No problem. For device/br...

How to Make Web Forms from other Web Forms
In putting together an ASP.NET application for a biz application, we have come to the point where there are three different types of basic forms which have, for the most part, been perfected. Now its is a matter of cloning those forms for various uses and modifying or adding code to suit the particular need. Copy and paste works OK, but there is a lot of changing of certain words, etc. Is there a way in ASP.NET to take these basic forms, turn them into classes or templates, create forms from them, then add or overwrite code? ? (Anybody who is a Vis Fox Pro programmer would know imme...

Controlling web control instances on Web Form
I have created a composite control, extending functionality of the Hidden Control. However, I would like to prevent the user from adding this control to the page multiple times. I only want one instance of the control on the Web Form. What setting in my project to I adjust to enable this functionality? I can not find it in any of the books that I have referenced. Is this a setting or is this code that has to be added to the inheriting class. Looks like there is no standard apporach. But I'd consider using static field to count number of created objects and throw exception if second object ...

Deploying Vb.Net Web Forms on the server
I created a setup file for my first test Web Application. It has a few forms that I like to test on the server. What do I have to do on the server for it to work properly. I have it working on my dev machine. What do I have to install and is there any changes that I have to make to machine.config or any other files. I am using the default settings in Global and web.config files. These files were generated by the Vb.Net 2003. I have been reading on this for awhile and like to make sure of the steps I have to follow. Any help will be greatly appreciated....

Creating a web form in VB.NET 2003
I apologise if this has been asked a thousand times.  Firstly, I am using VB.NET 2003 (v7.1.3088) and .NET Framework 1.1.4322  SP1 on WinXP Professional SP2 platform with all security and critical updates in place.My background: good old FoxPro, VB6 and VBA under Access (yuk!)I am playing about with a test project before hopefully upgrading to VB.NET 2005 and i am having a fundamental problem with web forms for a ASP.NET web application.  Whenever I copy and paste an object (text box, label, etc) VB.NET pastes the object back onto the form apparently correctly.  After up...

login form for web services vb.net
Hi, I'm trying to create a windows form with  web services that check if the user and password are the same within the SQL SERVER DATABASE, if its the same then your a valid user if not your an invalid user. That is working ok, but I want to implement a try statement which only allows the user to enter in the details 3 times then the windows form shuts down. also if they do have it correct then another window opens up. ANY HELP PLEASE OR IF SOME 1 KNOWS HOW TO DO IT IN  ASP.NET :>  HERE THE CODE: <WebMethod()> _     &nbs...

how to call controls of web control form in coding of other forms?
hello everyone i have one problem i have created one webcontrol shoppingcart in which there are two labels one of qty and other of amt which are initially enable =falsenow on other page(aspx page) i have one txtbox of qty as txtqty i want tat as soon as user enters qty txtqty and clicks on add cart button it shd be display d value in lblqty of web control shopping cart make sure i m using datalist on aspx page so give me suggestions on tat basis and my coding is in vb. plz give ur suggestions as fast as possible. Neeti what do you mean by webcontrol.... it this user control or normal ...

Web resources about - Looping through controls in VB.Net Web Form - asp.net.web-forms

Control - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Home lost after out-of-control grassfire at Edgecombe in central Victoria
One home is lost after a fast-moving grassfire sweeps through Edgecombe in central Victoria..

Huemote for Apple Watch controls your Philips Hue lights
If you use a Philips Hue lighting system and own an Apple Watch, then Huemote is a must-have app. What easier way is there to control the brightness, ...

Contextual and Granular Notification Controls
... written a lot about notifications, at one point calling the notifications screen “my home screen.” But I don’t like how we are forced to control ...

ESI cost control and networks
There is an interesting discussion thread on Twitter about how the way Medicare for All saves money is by significantly reducing provider, device, ...

US Democratic debate: Candidates spar on gun control
Candidates in the Democratic race for the White House hold their liveliest TV debate yet, clashing over gun control and healthcare among other ...

Doro PDF printer adds color conversion, page rotation controls
Lightweight virtual PDF printer Doro has been updated with new color conversion and page rotation settings. If the default "UseDeviceIndependentColor" ...

While No One Was Watching, Obama Took Control of the Courts
... and President Obama’s legacy.” It didn’t favor conservatives but there was a dramatic shift to the far left. The appellate is under the control ...

Sanders Backs Gun Control Legislation Ahead of Democratic Debate
Hillary Clinton's campaign calls her rival's move a "debate-eve conversion."

Flair’s Smart Vent System Controls the Temperature in Multiple Rooms
This time last year, we took a look at how Keen Home was changing up smart home temperature control with their KeenVent , a smart vent that could ...

Resources last updated: 1/20/2016 7:02:23 PM