Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2007

Tip: Looking for answers? Try searching our database.

vb.net class help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
paulbearden - 27 Jan 2007 03:54 GMT
Greetings,

I'm fairly new to .net (especially 2.0). I using vb.net, 2.0 framework and
Visual Studio 2005.

I'm trying to create a class with a member that will iterate through a web
page and discover all the controls on the page, list their types, names and
what value they hold. What I want is to be able to save all the fields on
any page without having to know what fields there are to begin with. I'll
then save the fields and values to a collection for later retrieval.

Ultimately I want to create a class that can iterate through all the pages
on the site calling the page class for each page so all the controls on the
entire site can be saved and retrieved.

This will allow me to, for instance, to set the value of a contol three
pages back without having to pass variables or get/set a bunch of sessions.
I will also allow me to enable/disable controls site wide or page wide from
anywhere on the site.

When I attempt to 'discover' what controls are on a given page I have
managed to determine the control types but not their names.

Can anyone provide an example or point me to where I might find one?
Thanks!
Jason Vermillion - 27 Jan 2007 23:35 GMT
Paul,

The name of the control as it is seen in the designer is Control.ID.  Other
usefull id/name properties on a control object are:

* Control.ClientID - used to identify each control in the html when using
client side Javascript.
* Control.UniqueName - uniquely identifies the control on the server side.  
Used internally to identify nested controls.  Don't use the UniqueName in
clientside Javascripts.

Here is some sample code that recursively iterates through the Controls on a
web page.  The code is in C#, though.  I just relealized that you needed VB.  
Let me know if you would like the code in VB.

Hope this helps,
Jason Vermillion
......
Some controls and nested controls on a panel.
......
   <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
   <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
   <asp:Button ID="Button1" runat="server" Text="Button" />
   <asp:LinkButton ID="LinkButton1"
runat="server">LinkButton</asp:LinkButton>
   <asp:CheckBox ID="CheckBox1" runat="server" />
   <asp:Panel ID="Panel1" runat="server" Height="50px" Width="125px">
       <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
       <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></asp:Panel>
   <br />
   <asp:ListBox ID="lstControlsOnPage" runat="server">
   </asp:ListBox>
........

   protected void Page_Load(object sender, EventArgs e)
   {
       System.Collections.Generic.List<ControlInfo> controlList = new
System.Collections.Generic.List<ControlInfo>();

       lstControlsOnPage.Items.Clear();
       GetChildControls(this, controlList);

       foreach (ControlInfo ctl in controlList)
       {
           lstControlsOnPage.Items.Add(ctl.ToString());
       }

   }

   public struct ControlInfo
   {
       public string ID;
       public string Type;
       public string ClientID;
       public string UniqueID;
       public string Value;

       public override string ToString()
       {
           return "ID: " + ID + " Type: " + Type + " ClientID: " + ClientID
+ " UniqueID: " + UniqueID + " Value: " + Value;
       }
   }

   /// <summary>
   /// Get the Value of the control if it has one (TextBox.Text,
Label.Text, CheckBox.Checked, etc.).
   /// </summary>
   /// <param name="ctl">Control to test.</param>
   /// <returns>The Control's value.</returns>
   private string GetValue(Control ctl)
   {
       string value = "";
       /*
       I think you'll need to test what type of control it is and
conditionally determine what the "value"
       is based on the type.  Try to cast each control as the most general
Interface that the control
       implements that has a value.  For example, ITextControl covers
TextBox and Label.
        */

       /* "as" statement is similiar to the VB TryCast operator */
       ITextControl itext = ctl as ITextControl;
       if (itext != null)
       {
           value = itext.Text;
       }

       CheckBox ichk = ctl as CheckBox;
       if (ichk != null)
       {
           value = ichk.Checked.ToString();
       }

       /* Check for other control types here...
        */
       return value;
   }

   /// <summary>
   /// Recursively search through the Controls collection and append the
control information to a generic list.
   /// </summary>
   /// <param name="ctl">The parent control to iterate over.</param>
   /// <param name="ctlList">List of control informations.</param>
   private void GetChildControls(Control ctl,
System.Collections.Generic.List<ControlInfo> ctlList)
   {
       ControlInfo ctlInfo;
       ctlInfo.ClientID = ctl.ClientID;
       ctlInfo.Type = ctl.GetType().Name;
       ctlInfo.UniqueID = ctl.UniqueID;
       ctlInfo.ID = ctl.ID;
       ctlInfo.Value = GetValue(ctl);

       ctlList.Add(ctlInfo);

       foreach (Control child in ctl.Controls)
       {
           GetChildControls(child, ctlList);
       }
   }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.