Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

This example shows on how to get all server controls ID in the page using recursive FindControl.

Here are the code blocks below:

    protected void Page_Load(object sender, EventArgs e)

    {

        GetPageControlsID(Page);

    }

 

    public void GetPageControlsID(Control Parent)

    {

        string txBoxID = string.Empty;

        string ddListID = string.Empty;

        string lblID = string.Empty;

 

        System.Collections.Specialized.StringCollection strColTxtBoxID = new System.Collections.Specialized.StringCollection();

        System.Collections.Specialized.StringCollection strColDDListID = new System.Collections.Specialized.StringCollection();

        System.Collections.Specialized.StringCollection strCollblID = new System.Collections.Specialized.StringCollection();

 

        if (Parent is TextBox)

        {

            //Get the TextBox Control ID

            txBoxID = ((TextBox)Parent).ID;

            //then store the Control ID in a Collections

            strColTxtBoxID.Add(txBoxID);

        }

        else if (Parent is DropDownList)

        {

            //Get the DropDownLists Control ID

            ddListID = ((DropDownList)Parent).ID;

            strColDDListID.Add(ddListID);

        }

        else if (Parent is Label)

        {

            //Get the Labels Control ID

            lblID = ((Label)Parent).ID;

            strCollblID.Add(lblID);

        }

        else

        {

            foreach (Control c in Parent.Controls)

                GetPageControlsID(c);

        }

        //Just add some conditions here if you wan't to check for all other control types like

        //Button,ListBox, RadioButton, etc...

 

        //PRINT the Control ID's Collections that was found

 

        //Iterating through the TextBox Control ID's collections

        foreach (string id in strColTxtBoxID)

        {

            Response.Write(id + "<BR/>");

        }

 

        //Iterating through the DropDownList Control ID's collections

        foreach (string id in strColDDListID)

        {

            Response.Write(id + "<BR/>");

        }

 

        //Iterating through the Label Control ID's collections

        foreach (string id in strCollblID)

        {

            Response.Write(id + "<BR/>");

        }

 

    }

 

As you can see the code was pretty straight forward and self explanatory.

That’s it!

 

Technorati Tags: ,,
Posted on Tuesday, June 23, 2009 4:51 AM ASP.NET , C# , Tips&Tricks | Back to top


Comments on this post: Get All Server Control ID’s in the Page

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net