469,651 Members | 1,643 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,651 developers. It's quick & easy.

Dynamically creating array of controls

Anybody quickly replies it, would be a great help!

I try to create dynamic array of textboxes control in ASP.NET(C#). I got the
following error while I tried to assign value to ID property of dynamically
created textbox. (look at my code please )

Error: “ Object reference is not set to an instance of Object”

totalRows – int variable which varies with number of records in SQL table

Textbox[]txbox = new Textbox [totalRows];

For (int i=0; i < totalRows; i++)
{

txbox[i].ID = “myTextbox”; //Getting error here

}

Thanks,
Nov 22 '05 #1
6 2672
an id is a unique identifier, it seems you are passing the same value to all
of the controls.. try this..
For (int i=0; i < totalRows; i++)
{

txbox[i].ID = “myTextbox” + i;

}

"Bhavin" wrote:
Anybody quickly replies it, would be a great help!

I try to create dynamic array of textboxes control in ASP.NET(C#). I got the
following error while I tried to assign value to ID property of dynamically
created textbox. (look at my code please )

Error: “ Object reference is not set to an instance of Object”

totalRows – int variable which varies with number of records in SQL table

Textbox[]txbox = new Textbox [totalRows];

For (int i=0; i < totalRows; i++)
{

txbox[i].ID = “myTextbox”; //Getting error here

}

Thanks,

Nov 22 '05 #2
Do you know how can I capture values from dynamically created controls?

"[Alan Flores]" wrote:
an id is a unique identifier, it seems you are passing the same value to all
of the controls.. try this..
For (int i=0; i < totalRows; i++)
{

txbox[i].ID = “myTextbox” + i;

}

"Bhavin" wrote:
Anybody quickly replies it, would be a great help!

I try to create dynamic array of textboxes control in ASP.NET(C#). I got the
following error while I tried to assign value to ID property of dynamically
created textbox. (look at my code please )

Error: “ Object reference is not set to an instance of Object”

totalRows – int variable which varies with number of records in SQL table

Textbox[]txbox = new Textbox [totalRows];

For (int i=0; i < totalRows; i++)
{

txbox[i].ID = “myTextbox”; //Getting error here

}

Thanks,

Nov 22 '05 #3
When you create an array of objects, the objects inside it are NOT
initialized ! In other owrd, it's an array of null values mate ... change
your code to this:

Textbox[] txbox = new Textbox [totalRows];

for(int i=0; i < totalRows; i++){
txbox[i] = new Textbox();
txbox[i].ID = "myTextbox"+i;
}

Angel
O:]


"Bhavin" <Bh****@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
Anybody quickly replies it, would be a great help!

I try to create dynamic array of textboxes control in ASP.NET(C#). I got the following error while I tried to assign value to ID property of dynamically created textbox. (look at my code please )

Error: " Object reference is not set to an instance of Object"

totalRows - int variable which varies with number of records in SQL table

Textbox[]txbox = new Textbox [totalRows];

For (int i=0; i < totalRows; i++)
{

txbox[i].ID = "myTextbox"; //Getting error here

}

Thanks,

Nov 22 '05 #4

Thanks a lot Angel. It worked(But next problem)

Textbox[] txbox = new Textbox [totalRows];

for(int i=0; i < totalRows; i++){
txbox[i] = new Textbox();
txbox[i].ID = "myTextbox"+i;
}

I used above code in my page_load event. It worked. But once all dynamic
empty textboxes got created I entered values in them. Now I want to capture
those values using dynamic IDs of textboxes but those IDs no longer available
after page_load event. I would like to capture all values from textboxes to
INSERT data in SQL table.

"Angelos Karantzalis" wrote:
When you create an array of objects, the objects inside it are NOT
initialized ! In other owrd, it's an array of null values mate ... change
your code to this:

Textbox[] txbox = new Textbox [totalRows];

for(int i=0; i < totalRows; i++){
txbox[i] = new Textbox();
txbox[i].ID = "myTextbox"+i;
}

Angel
O:]


"Bhavin" <Bh****@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
Anybody quickly replies it, would be a great help!

I try to create dynamic array of textboxes control in ASP.NET(C#). I got

the
following error while I tried to assign value to ID property of

dynamically
created textbox. (look at my code please )

Error: " Object reference is not set to an instance of Object"

totalRows - int variable which varies with number of records in SQL table

Textbox[]txbox = new Textbox [totalRows];

For (int i=0; i < totalRows; i++)
{

txbox[i].ID = "myTextbox"; //Getting error here

}

Thanks,


Nov 22 '05 #5
You could just store those controls in a member variable in your page class,
and check on them, getting their .Text property.

.... or, when you create them, you could add an event handler for their
OnChange event, adn get the values that way

.... or, you could even loop inside the page's controls collection and find
them through there !

... or I could just keep writing. I think the first or second suggestion
will work fine :]

Angel
O:]
"Bhavin" <Bh****@discussions.microsoft.com> wrote in message
news:22**********************************@microsof t.com...

Thanks a lot Angel. It worked(But next problem)

Textbox[] txbox = new Textbox [totalRows];

for(int i=0; i < totalRows; i++){
txbox[i] = new Textbox();
txbox[i].ID = "myTextbox"+i;
}

I used above code in my page_load event. It worked. But once all dynamic
empty textboxes got created I entered values in them. Now I want to capture those values using dynamic IDs of textboxes but those IDs no longer available after page_load event. I would like to capture all values from textboxes to INSERT data in SQL table.

"Angelos Karantzalis" wrote:
When you create an array of objects, the objects inside it are NOT
initialized ! In other owrd, it's an array of null values mate ... change your code to this:

Textbox[] txbox = new Textbox [totalRows];

for(int i=0; i < totalRows; i++){
txbox[i] = new Textbox();
txbox[i].ID = "myTextbox"+i;
}

Angel
O:]


"Bhavin" <Bh****@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
Anybody quickly replies it, would be a great help!

I try to create dynamic array of textboxes control in ASP.NET(C#). I got
the
following error while I tried to assign value to ID property of

dynamically
created textbox. (look at my code please )

Error: " Object reference is not set to an instance of Object"

totalRows - int variable which varies with number of records in SQL

table
Textbox[]txbox = new Textbox [totalRows];

For (int i=0; i < totalRows; i++)
{

txbox[i].ID = "myTextbox"; //Getting error here

}

Thanks,


Nov 22 '05 #6
Dan
Angelos - Can you translate into vb.net? - Can you direct me to a newsgroup
that might if not?

Thanks

Dan

"Angelos Karantzalis" wrote:
When you create an array of objects, the objects inside it are NOT
initialized ! In other owrd, it's an array of null values mate ... change
your code to this:

Textbox[] txbox = new Textbox [totalRows];

for(int i=0; i < totalRows; i++){
txbox[i] = new Textbox();
txbox[i].ID = "myTextbox"+i;
}

Angel
O:]


"Bhavin" <Bh****@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
Anybody quickly replies it, would be a great help!

I try to create dynamic array of textboxes control in ASP.NET(C#). I got

the
following error while I tried to assign value to ID property of

dynamically
created textbox. (look at my code please )

Error: " Object reference is not set to an instance of Object"

totalRows - int variable which varies with number of records in SQL table

Textbox[]txbox = new Textbox [totalRows];

For (int i=0; i < totalRows; i++)
{

txbox[i].ID = "myTextbox"; //Getting error here

}

Thanks,


Nov 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Bhavin | last post: by
9 posts views Thread by netasp | last post: by
5 posts views Thread by Nathan Sokalski | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.