473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

C# Web App: Assigning controls dynamically

55
Hi all,

I am trying to reference these drop down lists I assign in the code behind but I keep getting a 'null reference error. Object is not instantiated'

Here is the code:

Expand|Select|Wrap|Line Numbers
  1. public DropDownList[] ComboBoxes = new DropDownList[4];
  2.  
  3. public void SetComponents()
  4. {
  5.  
  6. ComboBoxes[0] = ddlH1;     //ddlH1 and so forth are controls in the page
  7. ComboBoxes[1] = ddlH2;
  8. ComboBoxes[2] = ddlH3;
  9. ComboBoxes[3] = ddlH4;
  10.  
  11. }

When I run this line of code later on:

Expand|Select|Wrap|Line Numbers
  1. txtUpdateType.Text = ComboBoxes[tracker - 1].SelectedItem.Value;
I get the error :(

Many thanks for any help in advance
Oct 21 '08 #1
8 1271
nateraaaa
663 Expert 512MB
Are your drop down list inside of a container such as a gridview or a panel?

Nathan
Oct 21 '08 #2
MrMancunian
569 Expert 512MB
Did you check wheter the value of "tracker - 1" isn't higher than the number of arrays you created?

Steven
Oct 21 '08 #3
dmj07
55
Drop down lists are inside a tab panel of a tab container. I thought this would be where it is going wrong!! How would I assign them to it if they are containted in there?

And yes tracker value is always above 0 so it does always reference a value within the array.

Many thanks for your replies.
Oct 21 '08 #4
nateraaaa
663 Expert 512MB
You will need to use the FindControl method of the container control that the drop down list reside in.

Expand|Select|Wrap|Line Numbers
  1. array[0] = (DropDownList)container.FindControl("dropdownlist1");
Nathan
Oct 21 '08 #5
dmj07
55
Yes I tried this before similar to what you suggested where if finds the control in the containers:

Expand|Select|Wrap|Line Numbers
  1. ComboBoxes[1] = (DropDownList)Panel2.FindControl("UpdatePanel20").FindControl("ddlH2");

However again when it comes to using the Dropdownlists like here:

Expand|Select|Wrap|Line Numbers
  1. txtUpdateType.Text = ComboBoxes[tracker - 1].SelectedItem.Value;
I still get the not set to instance error :(

Any more help will be greatly appreciated
Oct 21 '08 #6
nateraaaa
663 Expert 512MB
Try finding the updatepanel control 1st then finding the dropdownlist in your updatepanel control.

UpdatePanel updatePanel = (UpdatePanel)Panel2.FindControl("UpdatePanel20");
ComboBoxes[1] = (DropDownList)updatePanel.FindControl("ddlH2");

Also have you run your debugger to verify which object is throwing the null reference exception.
If this does not work you may need to post more of your code so that we can see what is going on in your code behind file.

Nathan
Oct 21 '08 #7
dmj07
55
Hey thanks for all your help on this. I managed to figure it out in the end it was more to do with page load event and how I had not set it correctly in there with the code prior to the stuff I gave here.

Many thanks for your time
Oct 22 '08 #8
nateraaaa
663 Expert 512MB
Hey thanks for all your help on this. I managed to figure it out in the end it was more to do with page load event and how I had not set it correctly in there with the code prior to the stuff I gave here.

Many thanks for your time
Would you mind posting the code that works so that others may be able to see a working example if they run into the same problem? Thanks.
Nathan
Oct 22 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Bil Muh | last post by:
Esteemede Developers, I would like to Thank All of You in advance for your sincere guidances. I am developing a software using Visual C++ .NET Standard Edition with Windows Form (.NET)...
3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
1
by: Robert Howells | last post by:
Perhaps I'm just too new at this to pull it off, or perhaps it's just bad architecture. I'd appreciate some feedback on the the wisdom (or lack thereof) in attempting the following: I'm not new...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web...
1
by: Grega | last post by:
Hi all, I am trying to create a VB smart device application. I create few buttons dynamically on form_load... something like this: Dim WithEvents BtnOperate As New...
3
AHayes
by: AHayes | last post by:
_Background I'm attempting to build a C# Windows application for work where I need to dynamically create and remove menu items (buttons). I've figured out how to dynamically create and (I think)...
4
Frinavale
by: Frinavale | last post by:
Introduction Sometimes, when developing web applications, we need to be able to dynamically load controls based on user selections. The following article describes a simple scenario where TextBox...
0
by: Kabo | last post by:
Hi there, I'm currently working on a C# application that deals with dynamically created TabPages based (each TabPage represents a file that has been opened). On each TabPage there is a...
0
by: lucindaa | last post by:
Hi EveryBody, i have a problem in setting the column type of datagrid dynamically in C# .Net Windows App. i am generating the grid automatically by assigning datasource of a datatable the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.