Array & Control Problem

  • 13 years ago

    hi all,

    I am working on a form with 10 controls(textbox, dropdownlist, button) on it. What I am trying to do is:

    - I want to count no. of controls on my web form(ignoring controls like Button, Div, Panel).

    - After counting the same I want to generate a loop which will fill an Array/ ArrayList with the values from various controls(mainly textbox and dropdownlist).

    - and in a class I want to extract the values of the same.

    Regards,

    Royal

  • 13 years ago

    Royal,

    Enabling the search of controls ain't too bad:

    main page:
                if (!IsPostBack)
                {

                    DoControls(this);


    sub-routine:
            // recursive routine for finding all controls underneath a parent (page)
            private void DoControls(Control c)
            {
                if (c.HasControls())
                {
                    foreach (Control o in c.Controls)
                    {
                        DoControls(o);
                    }
                }
                else
                    System.Diagnostics.Debug.WriteLine(c.GetType().ToString());
            }


    output:
    System.Web.UI.LiteralControl
    System.Web.UI.HtmlControls.HtmlTitle
    System.Web.UI.LiteralControl
    System.Web.UI.LiteralControl
    System.Web.UI.WebControls.GridView
    System.Web.UI.LiteralControl
    System.Web.UI.LiteralControl

     

    However, in order to gain the values etc from the controls, you'll have to "assess" their type (typeof), and unless you absolutely know that the control you want the values from has the same attribute that is accessible (ie ".Text" for  instance) - you'll still have to do the appropriate cast I think.

    The class could contain a simple hashtable / dictionary - where you could store the control name against its value.

    Dunno if this is the kind of thing you're thinking of, but don't see why it shouldn't work.

    Joe 

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“C++ : Where friends have access to your private members.” - Gavin Russell Baker