Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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 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 January 4th, 2006, 02:32 AM
Authorized User
 
Join Date: Dec 2005
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
 
Old January 4th, 2006, 06:43 AM
Friend of Wrox
 
Join Date: Jun 2003
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.
 
Old January 4th, 2006, 12:04 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
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
 
Old January 5th, 2006, 02:30 AM
Authorized User
 
Join Date: Dec 2005
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
 
Old January 5th, 2006, 02:47 AM
Friend of Wrox
 
Join Date: Jun 2003
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.
======================================
 
Old January 5th, 2006, 03:31 AM
Authorized User
 
Join Date: Dec 2005
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
 
Old January 5th, 2006, 05:28 AM
Friend of Wrox
 
Join Date: Jun 2003
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.
 
Old January 5th, 2006, 05:43 AM
Friend of Wrox
 
Join Date: Jun 2003
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.
 
Old January 5th, 2006, 05:46 AM
Authorized User
 
Join Date: Dec 2005
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
 
Old January 5th, 2006, 10:03 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
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





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 [email protected] Visual C++ 6 September 3rd, 2003 08:48 AM





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