Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 15th, 2005, 03:29 AM
Registered User
 
Join Date: Feb 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Accessing dynamically created controls

Hi,
I am using C# in asp.net to create textbox controls dynamically in a table on a page. There are 7 textboxes across a row (one for each day of the week) and a variable number of rows. As I create the table rows, cells and textboxes I assign the textboxes IDs of "Row"+n+"tbxDay1" = Row1tbxDay1 thru Row1tbxDay7 and then Row 2 thru Row n, each with 7 textboxes.

With javascript I hook the onfocus, onkeypress and onblur events to manipulate and control the input into these boxes. So far, so good.

From here I want to use server side code-behind to update the values in these textboxes to a database. But the problem arises on how I access these controls.

I'd like to use a for next nested loop such as:

for(int i = 1; i<rowcount+1; i++)
{
   for(int j = 1; j<8; j++)
   {
       id = "Row"+i.ToString()+"tbxDay"+j.ToString();
*****************
Here's where it breaks down. I want to access the text property of the textbox controls named with the value of "id" and .Text.

I've done this type of dynamic name assignment in many languages and platforms over the years, but I can't seem to find this available in asp.net c#. Am I going to have to rethink this process?

Bob
 
Old February 15th, 2005, 12:19 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

If you've added them to a page, create the ID, then use Page.FindControl(id), as such:

((TextBox)Page.FindControl(id)).Text = "<value>";

Brian
 
Old February 16th, 2005, 12:10 AM
Registered User
 
Join Date: Feb 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply, Brian. Prior to your post I had tried:
string x = ((TextBox)this.FindControl(id)).Text;
which didn't work. Changing "this" to "Page" didn't help either.

Since these, Rows, Cells and textboxes are being added to an empty table that is defined on the page, I also tried: string x = ((TextBox)table1.FindControl(id)).Text;

The error message generated with these attempts was:
"Object reference is not set to an instance of an object"

This error message can be a bit ambigueous at times because you don't know if you've coded the statement improperly or if the data that you're trying to access is really not there. I suspect that the latter in this case.

I did see a couple of forums explaining that controls that are created dynamically are not automatically added to the viewstate and therefore would not appear in postback. One remedy I saw was to generate the row, cell and textbox add's from the OnInit() function rather than Page_Load().

My most recent attempt was to have my AddRows() function called from OnInit() rather than Page_Load(), but that resulted in the same error. While debugging, I see that the count in the table1.rows property is 0, so I'm pretty sure that the rows aren't being persisted to my code-behind.

If I find the answer I'll post it. Thank you for your help.

Bob

 
Old February 16th, 2005, 02:33 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Actually, I forgot about the table structure; for a table, you have to get down to the row and cell that the control belongs to and call FindControl from the cell, or access the cell's controls collection.

Sorry,

Brian





Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help me about dynamically created controls vingo_mail ASP.NET 2.0 Basics 1 March 24th, 2008 12:54 PM
Problems with Dynamically created controls Preeti_Singh .NET Framework 1.x 1 March 7th, 2007 01:30 PM
Correctly disposing dynamically created controls simarik C# 2 December 20th, 2005 08:49 PM
Events for multiple dynamically created controls Puck312 Pro VB 6 5 August 29th, 2005 03:05 PM
Dynamically created controls katsarosj ASP.NET 1.0 and 1.1 Basics 4 February 4th, 2004 09:13 PM





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