vb.net class help

P

paulbearden

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!
 
G

Guest

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);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top