Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 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 January 4th, 2006, 02:32 AM
Authorized User
 
Join Date: Dec 2005
Location: Bangalore, , .
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to kapi.goel
Default Access the dynamic control using C#

Hello Experts,

I am creating numerous Server side Text controls using C# as code

Note:We can assume speakerPort as 3

for(int i = 1; i <= speakerPort; i++)
{
  TableRow tr2 = new TableRow();
  TableCell tc3 = new TableCell();

  txtSpeakerName = new TextBox();
  txtSpeakerName.ID = "speakerName_" + i.ToString();
  txtSpeakerName.Text = "";
  tc3.Controls.Add(txtSpeakerName);
  tr2.Cells.Add(tc3);
}

Problem is i am not able to access the txtSpeakerName Text Control.

Error i am facing: "System.NullReferenceException: Object reference not set to an instance of an object"

Guide me how to access all the Text values for "txtSpeakerName" (user has entered) using C# as language.
__________________
KG
Reply With Quote
  #2 (permalink)  
Old January 4th, 2006, 06:43 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The problem seems to be because you are instantiating the same variable for each iteration of the loop; i.e. you are using the same variable for all text boxes.

You could declare an array of text boxes outside the loop and instantiate each entry inside the loop. Then you should be able to get the Text property directly.

BTW... do you add the rows to a table somewhere?

Hope it helps, Jacob.
Reply With Quote
  #3 (permalink)  
Old January 4th, 2006, 12:04 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Instantiation of the textboxes themselves shouldn't be a problem. You can add each to some control collection, then create a new instance as you are doing and it will work.

However, as Jacob pointed out, you are creating new table rows, but aren't putting them anywhere. This might be the start of the problem.

-Peter
Reply With Quote
  #4 (permalink)  
Old January 5th, 2006, 02:30 AM
Authorized User
 
Join Date: Dec 2005
Location: Bangalore, , .
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to kapi.goel
Default

tnx.... Yea i am adding the rows into a table. I can see the dynamic text boxes, this is certainyl not the problem.

Well let me try ur suggestion of arrays... hope it will work.

Kapil

KG
Reply With Quote
  #5 (permalink)  
Old January 5th, 2006, 02:47 AM
Friend of Wrox
Points: 2,376, Level: 20
Points: 2,376, Level: 20 Points: 2,376, Level: 20 Points: 2,376, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Australia.
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

You might like to try finding the control and casting it to a textbox.
I have not tested but I think it would look something like this:
TextBox txtbx = (TextBox) this.FindControl("speakerName_" + x);'Where x is the particular box you are after.
txtbx.Text = "Hello";

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
Reply With Quote
  #6 (permalink)  
Old January 5th, 2006, 03:31 AM
Authorized User
 
Join Date: Dec 2005
Location: Bangalore, , .
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to kapi.goel
Default

planoie, i hv tried using array text boxes too, still not working.

Data member declaration inside the class:-
protected System.Web.UI.WebControls.TextBox [] txtSpeakerName;

Code Inside the button click:-

txtSpeakerName = new TextBox[5];

for(int i = 1; i <= speakerPort; i++)
{
    TableRow tr2 = new TableRow();
    TableCell tc3 = new TableCell();
    TableCell tc4 = new TableCell();

    // Create and Add Text for Speaker Name
    txtSpeakerName[i-1].ID = "speakerName_" + i.ToString();
        txtSpeakerName[i-1].Text = "";
    tc3.Controls.Add(txtSpeakerName[i-1]);
    tr2.Cells.Add(tc3);
}
Still same error is coming
"System.NullReferenceException: Object reference not set to an instance of an object"

For other membes: "I am taking dropdown list value (say 5), af a button click i am getting 5 dynamic text boxes, so i am trying to access the text values of these dynamically created text boxes on form submission".

Pls help me

KG
Reply With Quote
  #7 (permalink)  
Old January 5th, 2006, 05:28 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by planoie
 Instantiation of the textboxes themselves shouldn't be a problem. You can add each to some control collection, then create a new instance as you are doing and it will work.
 Well, if kapi.goel is trying to get the Text property from the txtSpeakerName variable, and not finding it as an entry in the collection I still think it is a problem to it the way that was originally posted.

kapi.goel how about trying something like this (not tested)...
Code:
txtSpeakerName   = new TextBox[5];
for(int i = 0; i < speakerPort; i++)
{
    TableRow  tr2 = new TableRow();
    TableCell tc3 = new TableCell();

    txtSpeakerName[i] = new TextBox();
    txtSpeakerName[i].ID = "speakerName_" + i.ToString();
    txtSpeakerName[i].Text = "";
    tc3.Controls.Add(txtSpeakerName[i]);
    tr2.Cells.Add(tc3);
}
txtSpeakerName[0].Text = "the first speaker";
(I changed the indexing which was starting from 1 also; be aware of the name change).

If the above doesn't work you should try and identify more specifically which object is null; that is, comment out all code, which is potentially the problem and notice when the null reference disappear.

Hope it helps, Jacob.
Reply With Quote
  #8 (permalink)  
Old January 5th, 2006, 05:43 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen, , Denmark.
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I realize now that I may have misunderstod the problem. When some button is clicked, the text boxes are made, and on the post back of the form, you are trying to access the values in the boxes, right?

Jacob.
Reply With Quote
  #9 (permalink)  
Old January 5th, 2006, 05:46 AM
Authorized User
 
Join Date: Dec 2005
Location: Bangalore, , .
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to kapi.goel
Default

yeah true. on clicking button a new page is opening and hence is displaying dynamic text boxes. i just want to access those text boxes

KG
Reply With Quote
  #10 (permalink)  
Old January 5th, 2006, 10:03 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

kapi,

Can you explain in detail where in the execution of your page code the call is made to construct the textboxes? The problem might be that you aren't actually creating the textboxes before the code that's trying to access them. Perhaps you could post the relevant parts of your page code so we can see the bigger picture of what's going on.

-Peter
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
dynamic button control SKhna ASP.NET 2.0 Basics 1 February 1st, 2008 10:36 AM
Dynamic control events??? feffe Pro VB 6 21 October 21st, 2007 05:47 PM
Dynamic control moving Ramacharyulu C# 2005 3 March 9th, 2007 12:11 AM
Dynamic placing of a control Warbird General .NET 1 July 9th, 2004 02:19 PM
Dynamic Control array carl.dufresne@sita.aero Visual C++ 6 September 3rd, 2003 08:48 AM



All times are GMT -4. The time now is 06:05 AM.


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