pauloh
pauloh

Reputation: 1002

ASP.Net - Loop all HTML components in the current page

I wish I could go all the elements of an ASP.Net page (HTML elements) and manipulate its text property / value. How could I do that?

I've been checking, and the property this.Page.Form.Controls apparently gets all the elements, but I would only page elements that caught the event. Does anyone have any idea.

I'll put an example of code that I'm currently studying and trying adpater for my needs.

Thanks

 string x = String.Empty;
 string y = String.Empty;

 foreach (Control ctrl in this.Page.Form.Controls){
     if (ctrl is TextBox){
        x += ((TextBox)(ctrl)).ID + ((TextBox)(ctrl)).Parent + "\n";
     } else {
        y += ctrl.GetType().Name + ctrl.GetType().Namespace + "\n";
     }
 }

Obs.: I am using some components of the Telerik components.

Upvotes: 1

Views: 3676

Answers (3)

matt-dot-net
matt-dot-net

Reputation: 4244

The controls on a page are stored in a tree data structure. You could use a recursive method to do this:

    private void SetText(ControlCollection controls, String textToSet)
    {
        foreach(Control c in controls)
        {
            if (c is ITextControl)
                ((ITextControl)c).Text = textToSet;

            if (c.HasControls())
                SetText(c.Controls, textToSet);
        }
    }

and you would call this method somewhere like let's say, in the OnPreRender Event (to make sure that you get all controls that have been added to the page) and pass the Page.Controls ControlCollection:

    protected override void OnPreRenderComplete(EventArgs e)
    {
        base.OnPreRenderComplete(e);
        SetText(Page.Controls, "new text");
    }

Upvotes: 1

David Hedlund
David Hedlund

Reputation: 129802

I'll have a stab at an answer, although I'm not sure it's what you're after (see my comments to the question).

If what you want to do is to quickly handle all the TextBoxes of a form (say) in one manner, and all of its DropDownLists in another manner, then what you're looking for may be OfType

DoSomethingWithAllTextBoxes( Page.Form.Controls.OfType<TextBox>() );
DoSomethingWithAllDropDownLists( Page.Form.Controls.OfType<DropDownList>() );

private void DoSomethingWithAllTextBoxes( IEnumerable<TextBox> textboxes) {
    foreach(TextBox txt in textboxes) {
       txt.Text = "Modified";
    }
}

If you're looking for event binding, you could run code like this one in OnInit

protected override void OnInit(EventArgs e) {

    foreach(TextBox txt in Page.Form.Controls.OfType<TextBox>() ) {
       txt.OnTextChanged += TextChangedEventListener;
    }        

    base.OnInit(e);
}

Upvotes: 1

rick schott
rick schott

Reputation: 21127

Attach the elements you want to an event and then act of them when its fired. This example is for demonstration but you could define a custom event.

ASPX:

   <asp:TextBox ID="TextBox1" AutoPostBack="true" runat="server"
        ontextchanged="TextBox1_TextChanged"></asp:TextBox>
   <asp:TextBox ID="TextBox2" AutoPostBack="true" runat="server"
       ontextchanged="TextBox1_TextChanged"></asp:TextBox>

Code-Behind:

 protected void TextBox1_TextChanged(object sender, EventArgs e)
 {
     TextBox tb = sender as TextBox;
     //do something
 }

Upvotes: 0

Related Questions