473,606 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ 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 2311
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
1756
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 OnSelectedIndexChange event. However, I am unable to retrieve the value of the DropDownList after the PostBack. I have tried dropdown.SelectedValue, dropdown.SelectedItem.Value, and dropdown.SelectedIndex. None of these returns the page that was...
3
2267
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) private int GetMatchingIndexOfDropDown(ref DropDownList d, string MatchValue) { return }
4
3108
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 DropDownList. e.g. While dr.Read DDL.Items.Add(dr("value")) End While I can do it if I use this method:
0
1149
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 value from the dropdownlist? I've simplified what's going on. This example is supposed to add to a textbox the value in the dropdownlist.
6
1740
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 DNS error. I developed this "simple" page but so far it is not simple! argh! <%@ Page Language="VB" Debug="true" Explicit="true" %> <%@ import Namespace="System.Data" %>
5
5947
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 event is fired by one of the controls contained in the User Control, these variable are reset. Here is my current code (I have a little more to add later, right now I am just concerned about the variables getting reset): Public Class DatePicker2...
1
1748
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. Depending on his selection (he tick the field and click filter button ) Now in filter button click I create label and dropdownlist control(populated from database) and add it to placeholder control. Now I select value and click the search button. Now...
0
1069
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 value in the dropdownlist and click a button to query a database the value in the dropdownlist changes back to the first value in the dropdownlist. Here is the code: for the dropdownlist private void DropView_SelectedIndexChanged(object sender,...
2
3316
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 the the cells of the table respectively which I have done easily but the problem is when I add the dynamically generated dropdownlist to the cell I cannot read the value from the dropdownlist. For adding the dropdownlist in the cell I have done the...
0
8009
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8432
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8078
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8299
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5962
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3919
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3964
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2442
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1548
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.