Discovering fields in a webform

S

Sanjay Pais

I need to write an application in C# that can accept a WebForm. I am
assuming the WebForm would need to be accessed in design mode. I need to
recursively iterate through the fields in the form, including the fields in
the web usercontrols on the form.



I am using the .NET framework 2.0 and VS 2005Beta2.



This is what I noticed when the form fields were rendered at runtime



On a Form the fields for example

<asp:TextBox ID="WCTextBox1" runat="server"></asp:TextBox>

were rendered as

<input name="TextBox1" type="text" id="TextBox1" />

while form objects contained in web usercontrols (named WebUserControl1 on
the form)

<asp:TextBox ID="WCTextBox1" runat="server"></asp:TextBox>

were rendered as

<input name="WebUserControl1$WCTextBox1" type="text"
id="WebUserControl1_WCTextBox1" />



I need to create a unique list of all the form fields application wide to
create text values for them as well as assign security to the individual
elements.



How would I go about achieving this?



Thanks in advance



Sanjay
 
S

souri challa

Sanjay,

When your asp.net page is loaded all the form fileds and any
contiguous text are parsed and built into a tree of control classes.
You can recurse through the control tree, do some type checking for
what you want to do and get/set values.

e.g private void LoopThroughControls(Control control){

for each (Control ctrl in control.Controls){
if (ctrl is TextBox)
(TextBox)ctrl).Text = "Your text";

if (ctrl is WebControl && nosecurity)
(WebControl)ctrl.disabled = true

if (ctrl.Controls.Count > 0) LoopThroughControls(ctrl);


}
}


HTH,
Souri
 
S

Sanjay Pais

Souri,

But what if you wanted to pass the assembly to another application that
wanted to process the form objects via reflection or something along those
lines?

thanks

Sanjay
 
S

souri challa

Sanjay,

Tell me a little more about this other application and what exactly
you're trying to achieve? Are you trying to parse an ASPX page in a
windows or console app? In this case I would still try to use asp.net
environment/classes to dynamically create a page class and parse it.

-Souri
 
S

Sanjay Pais

Souri,

Yes, that is exactly what I was hoping to do. I was writing a console app
that would load the assembly dll and by reflection traverse though the
classes in the assembly and based on the types, perform a set of tasks.
I was actually traveling down that path when I discovered that in .NET 2.0,
the wizard that publishes the pages creates partial dlls which are not
really assemblies, I am trying to figure out how to create the .net1.x style
bin dlls.

Sanjay
 

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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top