Thursday, March 22, 2012

Looping on all page server control

In client side, i can loop over all form elements like this:

for(i=0;i<doc.forms[0].elements.length;i++) {...}

how do i do it regarding server controls?

E.g. , suppose i want to initialize all textboxes from the Session object.

How do i loop over all controls in the page?

i have tried to loop over:

Page.Controls, but it seems that this is a different collection as my controls were not present there.

so, how do i do it?

thanks.

Have a look at the following code snippet, which is used to clear all the control elements value...

PrivateSub ClearControls(ByVal parentAs System.Web.UI.Control)

Dim cAs Control

ForEach cIn parent.Controls

If c.GetType()IsGetType(TextBox)Then

Dim tAs TextBox = c

t.Text = ""

ElseIf c.GetType()IsGetType(HtmlControls.HtmlInputHidden)Then

Dim hAs HtmlControls.HtmlInputHidden = c

h.Value = ""

ElseIf c.GetType()IsGetType(DropDownList)Then

Dim dAs DropDownList = c

d.ClearSelection()

ElseIf c.GetType()IsGetType(ListBox)Then

Dim lAs ListBox = c

l.ClearSelection()

ElseIf c.GetType()IsGetType(RadioButtonList)Then

Dim rlAs RadioButtonList = c

rl.ClearSelection()

ElseIf c.GetType()IsGetType(CheckBox)Then

Dim chkAs CheckBox = c

chk.Checked =False

ElseIf c.GetType()IsGetType(CheckBoxList)Then

Dim clAs CheckBoxList = c

cl.ClearSelection()

EndIf

If c.HasControlsThen

ClearControls(c)

EndIf

Next

EndSub

Hope this helps.


Yes,

but what is "Parent"?

(please use C# if possible).

let us watch a code behine a server control button.

the object "Parent" in your code is not "Page" or "this" from within the cope i mentioned.

So what is Parent?

e.g. :

privatevoid Button1_Click(object sender, System.EventArgs e)

{

lbl.Text=this.Controls.Count.ToString();

}

the lbl.text displays the value 3 although i have many controls (mainly textBoxes) on the page.


Haven't you tried using "NameValueCollection"? It gives a lot of info on controls in a web page.
I have actually used it several times on my own web pages.

NameValueCollection coll;

//Load Form variables into NameValueCollection variable.

coll=Request.Form;

// Get names of all forms into a string array.

String[ ] arr1 = coll.AllKeys;


Roinka wrote:

but what is "Parent"?

"Parent" is the parent Control... turn on "tracing" on your app and look at the "Control Tree" in the output to see what is meant by that...


Well,

in the tree i get:

__PAGE

ctl0

Form1

b_display (for a button) .

that is the button b_display belongs to Form1 that belongs to __PAGE.

How do i refer to b_display from within the code?

it is not __PAGE.Form1.b_display.

thanks


it would be what it says in the "Control Unique ID" column.. thats the ID that will work with Page.FindControl

So in my screenshot if i wanted to do something with <asp:Image id="imgProfile">, then i'd have to say:

Dim ThisImage As Image = CType(Page.FindControl("MasterRoot$LoginInfo$imgProfile"), Image)

i could alternatively say

Dim ThisImage As Image = CType(LoginInfo.FindControl("imgProfile"), Image)

since "LoginInfo" is the parent of the imgProfile control

Is that what you are after? or is there more? and i ask this because your subject of "Loop on all page server controls", that's a little different and is only like 10-15 lines of code to load all the controls in a collection of some sort to manipulate


I guess i didnt explain my self properly.

let me present an example.

Suppose i have a page with 3 TextBoxes and 3 Buttons on it.

in thePage_Load event i iterate over the page controls like this:

foreach (Control cinthis.Controls)

TextBox2.Text+=c.GetType()+"\r";

since i have 3 textBoxes and 3 buttons on the form i would expect to see their types in textbox2.

but insted i get:

System.Web.UI.ResourceBasedLiteralControl
System.Web.UI.HtmlControls.HtmlForm
System.Web.UI.LiteralControl

How can i iterate over the Form elements in server side?

you wrote Parent.Controls.

so in the Page_Load event scope what shell i use insted of Parent ?

i assume that System.Web.UI.HtmlControls.HtmlForm is the form (its ID is Form1), so maybe i have to iterate over its elements but it doesnt have a "Controls" property.

so, where do i go from here? assuming a page having 3 TB's and 3 Buttons, from within the Page_Load event scope, how can i iterate over the page controls (that is those TB's and those Buttons) and, say, display their Text?

thanks a lot.




// To iterate through all controls on a page: this.GetControls(this).
protected void FindAllControls(Control parentContainer)
{
foreach ( Control childControl in parentContainer.Controls )
{
// Is the childControl itself a container?
if ( childControl.Controls != null )
{
// Recursive call for any child controls in this child control container...
this.GetControls(childControl);
}

// No...Go by type...
if ( childControl as TextBox != null )
{
// It's a TextBox...
(childControl as TextBox).Text = "some text";
}
if ( childControl as Button != null )
{
// It's a Button...
(childControl as Button).Text = "some text";
}
}
}

NC...


You are a star!

Thanks!


I dunno about that! But thanks.

NC...

0 comments:

Post a Comment