Retrieve all input values from all controls at once

Experts,

I am currently working on a project to process webforms the very easy way. I use DevExpress Textboxes and memo's etc to build my webforms. Ive created a process.aspx and form1.aspx. Form1.aspx submits its values (via session) to process.aspx where it will be posted into the database.

Now i need to be able to loop throught my controls on my form1.aspx so i dont need to write all the code for every control listed in the page. This also makes it easier for me to add new forms.

Bassicly what i want is:
-Loop through all the controls on the form page and paste it into an session where the key is numeric and the value is the input from the control. [0]['text0'], [1]['text1'], [2]['text2']
-Loop through the session and store it in the database (database part is already build!).

I have never worked with sessions before and i dont know how to loop through the controls on my webform, could someone please help me out ?
LVL 4
JeePeeTeeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

masterpassCommented:
have a look at the code below
private void LoopThrough()
{
     foreach (Control ctrl in Page.Controls)
     {
      // do what ever you want with each control here     
     }
}

Open in new window

0
Máté FarkasDatabase Developer and AdministratorCommented:
Yes, you can use a code like this

protected void Page_Load(object sender, EventArgs e)
        {
            string Value = "";
            foreach (Control Ctrl in this.form1.Controls)
            {
                switch (Ctrl.GetType().Name)
                {
                    case "TextBox": Value = ((TextBox)Ctrl).Text; break;
                    case "DropDownList": Value = ((DropDownList)Ctrl).Text; break;
                    case "RadioButton": Value = ((RadioButton)Ctrl).Text; break;
                    case "CheckBox": Value = ((CheckBox)Ctrl).Text; break;
                }
                Session[Ctrl.ID] = Value;
            }
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LopsyCommented:
This is rather sketchy but i hope u get the idea
use javascript gets all the element names in a form and their values

Step 1: declare an array to hold d form values

String  array[] = new  String[count];  --where count is d number of elements in d form

Step 2: loop thru d form elelments and get d name /value pairs
for(i=0; i<document.FormName.elements.length; i++)
{
   var fieldName = document.FormName.elements[i].name;
   var fieldValue = document.FormName.elements[i].value;
step 3: Add d name value pairs to array
}

Finally
Step 4:
Add d array to a seesion variable
session.add('myformvalues',strarry )

hope i helped a little
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

JeePeeTeeAuthor Commented:
Hi experts,

Thanks for all your fast responses. Ive been using the code from agux3e cause he mentioned an switch/case scenario which is very usefull, didn thought of that.

Also, i dont want to use javascript (althought il keep it in mind for my php projects!!!).

Ive transformed the giving code to use with devexpress web controls. I also added a auto increment integer to use as key but that throws an exception:

        protected void submitButton_Click(object sender, EventArgs e)
        {
            string Value = "";
            int i = 0;
            foreach (Control Ctrl in this.Form.Controls)
            {
                switch (Ctrl.GetType().Name)
                {
                    case "ASPxTextBox": Value = ((DevExpress.Web.ASPxEditors.ASPxTextBox)Ctrl).Text; break;
                    case "ASPxComboBox": Value = ((DevExpress.Web.ASPxEditors.ASPxComboBox)Ctrl).Text; break;
                    case "ASPxRadioButton": Value = ((DevExpress.Web.ASPxEditors.ASPxRadioButton)Ctrl).Text; break;
                    case "ASPxCheckBox": Value = ((DevExpress.Web.ASPxEditors.ASPxCheckBox)Ctrl).Text; break;
                    case "ASPxMemo": Value = ((DevExpress.Web.ASPxEditors.ASPxMemo)Ctrl).Text; break;
                }
                Session[i] = Value;
                i++;
            }
            Server.Transfer("process.aspx");
        }

Will throw :
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Anyone know what ive done wrong?

0
Carl TawnSystems and Integration DeveloperCommented:
If you're doing a Server.Transfer then storing everything in the Session is a bit overkill. You can simply access the original context page from the page you transferred to.
0
JeePeeTeeAuthor Commented:
Will i still be able to retrieve the values ? The whole idea is to post the values from any page to process.aspx where it will be stored in my database.

If so, how to achieve this kind of behavior ? (thats also why i wanted the keys from the session to be numeric).
0
Carl TawnSystems and Integration DeveloperCommented:
You can still use a loop to cycle over all of the controls in the page, but you don't need to write them to the Session first.

Something that nobody has mentioned yet is that your loop will need to be recursive due to the nested nature of WebForm layout structures.

From Process.aspx you just need to do something like:
// retrieve the page we transferred from
Page caller = Page.PreviousPage;

// now we can loop through the control as before

Open in new window

0
JeePeeTeeAuthor Commented:
Hi,

sorry for my slow responses, kinda busy with 1337 stuff atm. Anyway, i did not know of that functionality. I will try to implement that in my project, thank you very much!

Ill keep you guys posted!
0
JeePeeTeeAuthor Commented:
Hi guys,

Tried to follow your suggestions but i cant seem to acces the controls when using the previouspage method. I have posted this issue in devexpress cause i think thats where the problem lies. I do think you guys helped me out a lot and i think its time to give away some points ;).
0
JeePeeTeeAuthor Commented:
Real fast answers! Also, gave me more inside on how i should achieve my goal in a better way.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.