473,480 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Getting value from DropDownList

132 New Member
Hi there.

I have created a DropDownList in asp.net with declaration in the aspx file as follows:

Expand|Select|Wrap|Line Numbers
  1. <asp:DropDownList ID="lbExtractType" runat="server">
  2.   <asp:ListItem Value=" "></asp:ListItem>
  3.   <asp:ListItem Value="apple">apple</asp:ListItem>
  4.   <asp:ListItem Value="banana">banana</asp:ListItem>
  5.   <asp:ListItem Value="orange">orange</asp:ListItem>
  6.   <asp:ListItem Value="peach">peach</asp:ListItem>
  7.   <asp:ListItem Value="pear">pear</asp:ListItem>
  8.   <asp:ListItem Value="grape">grape</asp:ListItem>
  9. </asp:DropDownList>
  10.  
I am then (as a test) trying to display this value as follows in the cs file:

Expand|Select|Wrap|Line Numbers
  1. DropDownList list = (DropDownList)Master.FindControl("ContentPlaceHolder1").FindControl("lbExtractType");
  2.         string extractType = list.SelectedItem.Value;
  3.         MessageBox.Show("Extract Type = " + extractType);
  4.  
When I run this I get the error message:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Server Error in '/WebSite1' Application. 
  3.  
  4. Object reference not set to an instance of an object. 
  5.  
  6. Line 161:
  7. Line 162:        DropDownList list = (DropDownList)Master.FindControl("ContentPlaceHolder1").FindControl("lbExtractType");
  8. Line 163:        string extractType = list.SelectedItem.Value;
  9. Line 164:
  10. Line 165:        MessageBox.Show("Extract Type = " + extractType);
  11.  
  12. Source File: c:\00 CADLine Projects\AJAX\WebSite1\Default.aspx.cs    Line: 163 
  13.  
  14.  
Now I would expect this if I hadn't initialised everything correctly but I "think" I have done this right..!

Any help will be greatly appreciated please?

Thank you.

M :)
Jan 10 '09 #1
3 2297
Bassem
344 Contributor
@E11esar
Hi E11esar,
What is Master? Is it a master page? I don't think you can access master page controls, you have to use content page first. It may be what make the object reference set to null. If I'm wrong, I'm glad to know.
Kind Regards,
Bassem
Jan 10 '09 #2
E11esar
132 New Member
Hi there.

Yes it is within a Master page, hence why I am using the

Expand|Select|Wrap|Line Numbers
  1. DropDownList list = (DropDownList)Master.FindControl("ContentPlaceHolder1").FindControl("lbExtractType"); 
  2.  
to find the control. That part I am happy with as I know that does work but there is something strange going on here.

Ofcourse the issue might be caused by me using an AJAX Accordion control, so the DropDownList is within the Content of the AccordionPane..?

I have also tried a statement such as

Expand|Select|Wrap|Line Numbers
  1. DropDownList list = (DropDownList)Master.FindControl("ContentPlaceHolder1").FindControl("Accordion1").FindControl("AccordionPane1").FindControl("lbExtractType");
  2.  
so this may give an indication of what I am trying to do.

Thank you.

Mark :)
Jan 10 '09 #3
E11esar
132 New Member
Hi there.

I've just solved this one: I hadn't used enough FindControl() commands to get down to the layer that the target control was within.

All sorted now.

Thank you.

M :)
Jan 10 '09 #4

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

Similar topics

1
1744
by: John Smith | last post by:
I have a Repeater that is paginated, and I would like the user to navigate to each page via a DropDownList. I have set the AutPostBack attribute to true and written code that fires on the...
3
2256
by: Earl Teigrob | last post by:
I want to write a function where I pass in a reference to a dropdownlist and a "match value" and have it returns the index of the dropdownlist item that matchs (or -1 if there is no match) ...
4
3097
by: Rob Venable | last post by:
Hi all, Can anyone tell me how to retrieve the ID of a selected item from a DropDownList. My query brings back the ID and value and I cycle through the .Read Method to add items to the...
0
1136
by: Tim Zych | last post by:
I'm having problems with a dropdownlist control. I fill the control with values in the page_load event. But when I click the button the dropdownlist control gets cleared out. How do I use a...
6
1731
by: Teep | last post by:
Below is my code for dropdownlist that is populated from a SQL table. After a selection from the ddl, a datagrid is suppose to come up pertaining to the information selected, but I keep getting a...
5
5922
by: Nathan Sokalski | last post by:
I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an...
1
1736
by: imranabdulaziz | last post by:
Dear All, I am mess with one situation let me explain the scenario. I am making search form where I display 15 field in checkboxlist and user select one or two or three or any no to all field....
0
1057
by: munya | last post by:
I am new in .asp I am trying to use dropdownlist to display values from the database and be able to query the database based on displayed values in a dropdownlist. the prob is when I select any...
2
3302
by: mylog | last post by:
Hi I am having a problem of getting the value from the dynamically generated table and dropdownlist. What I am facing is, I have created a table in the aspx page and now I need to add values to...
0
7049
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7052
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7092
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...
1
6744
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
6981
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
5348
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
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3000
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.