Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 14th, 2006, 10:39 AM
Registered User
 
Join Date: Jan 2006
Location: Grimstad, , Norway.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamically generated textboxes and their values

Hi,

I have some Textbox controls on my page that are generated by a for-loop for each tuple from a database. Their IDs are textboxX where X is the counter from the for-loop.

Now, I activated postback for the textboxes, so that when the user changes the value in one of them, the page does a postback.

To handle the values from the postback, I test Page.IsPostBack. If this test is true, I want to use the new value (from the textbox that did the postback) to ie update the database. So far, I just loop through all the textboxes and update all the values (since I can't find out which control did the postback).

Code:
if (Page.IsPostBack) 
{
    for (int i = 0; i < nAmountOfTextBoxes; i++)
    {
        int value;   // value from textbox
        try
        {
            value = int.Parse(((TextBox)myPanel.FindControl("textbox" + i.ToString())).Text);
        }
        catch
        {
            // Couldn't parse
            value = -1;
        }

        if (value >= 0)
        {
            deportToSiberia(value);// do something with the value
        }
    } // end for
}
This doesn't work. I didn't really expect it to, but I don't know any other way to go about with it. How can I fetch the values from a postback in my code? Any help is greatly appreciated.

Thanks,
Frode
Reply With Quote
  #2 (permalink)  
Old April 17th, 2006, 03:46 PM
Registered User
 
Join Date: Jan 2006
Location: Grimstad, , Norway.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Found a solution. In case anyone stumbles upon this thread wondering the same thing, here it is (in short):

In the Page_Init I added all the textboxes. I checked !Page.IsPostBack each time to see if the values from the database should be loaded.

In the Page_Load I looped through each control in the panel with
Code:
if(Page.IsPostBack) 
{
    foreach( Control c in myPanel.Controls )
    {
        if( c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") )
        {
            TextBox t = (TextBox)c;
            deportToSiberia(t.Text); // Do something with the value from the postback
        }

    } // end for
} // end postback check
This article helped me a lot http://aspnet.4guysfromrolla.com/articles/092904-1.aspx

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically generated dropdownlist csun ASP.NET 1.0 and 1.1 Basics 3 April 8th, 2008 07:38 AM
dynamically generated form SaraJaneQ Javascript How-To 4 November 9th, 2006 11:11 AM
automatically generated textboxes noor ASP.NET 1.0 and 1.1 Basics 6 May 11th, 2005 09:28 AM
dynamically generated submenus elladi Dreamweaver (all versions) 9 December 24th, 2004 08:12 AM



All times are GMT -4. The time now is 12:24 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.