• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 797
  • Last Modified:

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 ?
0
JeePeeTee
Asked:
JeePeeTee
2 Solutions
 
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
 
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now