Best way to Catch all the values in a form

Last post 05-12-2008 6:01 AM by Abhishek khanna. 11 replies.

Sort Posts:

  • Best way to Catch all the values in a form

    05-11-2008, 6:18 AM
    • Loading...
    • afrika
    • Joined on 05-21-2007, 5:15 AM
    • Posts 212

    Hello,

    I have a form, which has 20 input fields for name and phone numbers.

    ie

    "Name1" runat="server">
    
    "Phone1" runat="server">44

     This goes from name1 and phone1 to 20. In my protected void Button1_Click(object sender, EventArgs e) Whats the best way to catch all this values. I have tried using the

    1. foreach (string element in Request.Form)

    But cant get my way around getting all the names and value pairs

    2. Manually catching all the values using

    string name1 = Name1.Text.ToString();

    string name2 = Name2.Text.ToString();

    etc

     

    Whats the most efficient way around this ?

    Thanks

    Ehi

  • Re: Best way to Catch all the values in a form

    05-11-2008, 6:46 AM
    • Loading...
    • anzer
    • Joined on 10-19-2004, 12:00 AM
    • UAE
    • Posts 566

    This code may help you... 

            for(int i=1; i<=20; i++)
            {
                Request.Form["Name" + i.ToString()];
                Request.Form["Phone" + i.ToString()];
            }

     

    But if the text boxes are in a user control or a page using master form you need to use the client id part also in the Request.Form

    If any issues please let me know with details.

     
     

    If this post was useful to you, please mark it as answer. Thanks

    My Blog
  • Re: Best way to Catch all the values in a form

    05-11-2008, 6:52 AM

    Hi

    If you want this to be name/values pairs then this is best approach

    Hashtable ht = new Hashtable();foreach (Control ctr in Page.Controls)

    {

    if (ctr is TextBox)

    {

    TextBox txt = (TextBox)ctr;

    ht.Add(txt.ID, txt.Text);

    }

    }

     

  • Re: Best way to Catch all the values in a form

    05-11-2008, 6:55 AM

    Better formatting:

     Hashtable ht = new Hashtable();

    foreach (Control ctr in Page.Controls)

    {

    if (ctr is TextBox)

    {

    TextBox txt = (TextBox)ctr;

    ht.Add(txt.ID, txt.Text);

    }

    }

     

  • Re: Best way to Catch all the values in a form

    05-11-2008, 6:55 AM
    • Loading...
    • afrika
    • Joined on 05-21-2007, 5:15 AM
    • Posts 212

    Here is the error message

     

    Server Error in '/cellulantsms' Application.

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

    Source Error:

    Line 34:         for (int i = 1; i <= 20; i++)
    Line 35:         {
    Line 36:             Request.Form["name" + i.ToString()];
    Line 37:             Request.Form["phone" + i.ToString()];
    Line 38:         } 

    Source File: c:\inetpub\wwwroot\cellulantsms\welcome\delete.aspx.cs    Line: 36

  • Re: Best way to Catch all the values in a form

    05-11-2008, 6:56 AM
    • Loading...
    • afrika
    • Joined on 05-21-2007, 5:15 AM
    • Posts 212

    Opps Sorry, that was the reply to anzer's message above. Didnt know there would be so many replies in such a such time :-)

  • Re: Best way to Catch all the values in a form

    05-11-2008, 7:01 AM
    • Loading...
    • afrika
    • Joined on 05-21-2007, 5:15 AM
    • Posts 212

    So how do i get the values out ?

    I tried using

     Response.Write(txt); 

    and

     Response.Write(ht); 

    But not getting any values out :(

    Abhishek khanna:

    Better formatting:

     Hashtable ht = new Hashtable();

    foreach (Control ctr in Page.Controls)

    {

    if (ctr is TextBox)

    {

    TextBox txt = (TextBox)ctr;

    ht.Add(txt.ID, txt.Text);

    }

    }

     

  • Re: Best way to Catch all the values in a form

    05-11-2008, 7:13 AM
    Answer
    • Loading...
    • anzer
    • Joined on 10-19-2004, 12:00 AM
    • UAE
    • Posts 566

    afrika:
    Compiler Error Message: CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
     

     Thats just a sample code you should use the values we are getting from Form variables, for example assign the values to some variable.

    for(int i=1; i<=20; i++)
            {
                string name = Request.Form["Name" + i.ToString()];
                string phone = Request.Form["Phone" + i.ToString()];
            }
     

    If this post was useful to you, please mark it as answer. Thanks

    My Blog
  • Re: Best way to Catch all the values in a form

    05-11-2008, 7:14 AM
    Answer
    • Loading...
    • anzer
    • Joined on 10-19-2004, 12:00 AM
    • UAE
    • Posts 566

    or try

    for(int i=1; i<=20; i++)
            {
                Response.Write( Request.Form["Name" + i.ToString()].ToString());
                Response.Write( Request.Form["Phone" + i.ToString()].ToString());
            }

    If this post was useful to you, please mark it as answer. Thanks

    My Blog
  • Re: Best way to Catch all the values in a form

    05-11-2008, 7:57 AM
    • Loading...
    • Rizwan328
    • Joined on 05-25-2006, 10:26 AM
    • Dubai, UAE
    • Posts 118

    afrika. are you still facing issue.

    If this post helps you, please mark it as Answer.



    Cheers,
    Muhammad Rizwan Javed.
    Software Engineer
    Aim168 Digital,Dubai, UAE.



  • Re: Best way to Catch all the values in a form

    05-12-2008, 3:46 AM
    • Loading...
    • afrika
    • Joined on 05-21-2007, 5:15 AM
    • Posts 212

    Sorted.

     Thanks. I just didnt get a reply from Abhishek Khanna

  • Re: Best way to Catch all the values in a form

    05-12-2008, 6:01 AM

    Hey afrika

    Was away. But you can try my version too, just for fun & learn.

    Here is what you can do:

    foreach (Control ctr in Page.Controls)
    {

    if (ctr is TextBox)
    {

    TextBox txt = (TextBox)ctr;
    Response.Write("ID: " + txt.ID + " value: " + txt.Text);
    }

    }

    Thanks

    Abhishek

Page 1 of 1 (12 items)