Setting a label value to a dropdownlist selected value

I am trying to set the value of a label to the selected value from a hidden dropdown list. This code is not working and the label value remians at its default.

Any ideas? Thanks very much.

Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)

Dim mylabel As Label

Dim myddl As DropDownList

If (DetailsView1.CurrentMode = DetailsViewMode.ReadOnly) Then

mylabel = DetailsView1.FindControl(


myddl = DetailsView1.FindControl(


mylabel.Text = myddl.SelectedValue.ToString

End If
7/31/2006 6:36:03 AM 72751 articles. 3 followers. Follow

5 Replies

Similar Articles

[PageSpeed] 28

I have now put this code in the Page_Load sub as it wasn't being called before.

The code is:

Dim mylabel As New Label

Dim myddl As New DropDownList

If Page.IsPostBack = True Then

If (DetailsView1.CurrentMode = DetailsViewMode.ReadOnly) Then

mylabel =

CType(DetailsView1.FindControl("Label1"), Label)

myddl =

CType(DetailsView1.FindControl("DropDownListViewOffice"), DropDownList) 

mylabel.Text = myddl.SelectedValue.ToString

End If

End If


And the error I am getting is on the line:  mylabel.Text = myddl.SelectedValue.ToString

and the error is: System.NullReferenceException: Object reference not set to an instance of an object.

The code for the dropdown and the label from the item template is:


<asp:DropDownList Enabled="false" ID="DropDownListViewOffice" runat="server" SelectedValue='<%# Bind("Office") %>'

DataSourceID="SqlDataSource3" DataTextField="OfficeName" DataValueField="OfficeID">


<asp:Label ID="Label1" runat="server" Text="Label" ></asp:Label>


I would be really happy to hear from anyone with some advise!!



7/31/2006 11:47:23 AM


Guess one or both controls are missing on Page_Load.

If I understand the goal you are trying to achieve perhaps this link helps:


7/31/2006 5:04:29 PM


Thanks for the help. That other link is what I am trying to do but can I not use the way I have been trying already? What do you mean by the controls are missing on Page_load?

Thanks again.


8/2/2006 4:09:43 AM


I dont use VB so I could be wrong: "Dim x as New Label (New DropDownlist)". Is the "New" needed? 

Anyway, I tried to simulate your code in a working example (be it in C#). Everything works without errors but..

When I put the code in Page_Load I noticed that at the end of Page_Load the debugger shows Label1.Text having the right value, but on rendering it keeps showing "Label". Enabling or disabling ViewState and with or without checking IsPostBack made no difference. When I put the code in Page_Databound everything works as expected.

So in the code you posted I see no reason for the null-reference error (except for perhaps the "New").



8/2/2006 11:26:03 AM

Hi Sjonnie,

Thanks very much for helping with this. I have tried without 'new' and it makes no difference. When debugging the lines:

mylabel =

CType(DetailsView1.FindControl("Label1"), Label)

myddl =

CType(DetailsView1.FindControl("DropDownListViewOffice"), DropDownList)

dont seem to be getting a value, and so I am thinking maybe the problem is with the FindControl?

Does this hint at any other problem?

Thanks again for your help, really appreciate it.


8/2/2006 11:59:37 AM

Similar Artilces:

new to .net, need help setting db values from checkbox's then retrieving value to a label control
Hi, Being new to i know this possible i just can't quite get it right!  I need a page with 5 checkbox's that when submitted sets the bit values in DB fields to checked. Then i need another page that retrieves the records of the checkboxes, but instead of having the db pre-set checkboxes on the second page i want it to turn on label web controls depending on if the check box is set.. I hope that makes sense?!! Can anyone point me in the right direction or to a suitable tutorial? Thanks in advance Ben Excellent article here.

Dropdownlist selected value passes first value in list no matter what value is selected. why ?
 HI . I have a dropdownlist. Databinds ok but when I must pass a value as a Selected value it always passes the first value in the list even if I select another value. I tried to set the ddl to enableviewstate false but it doesn't change anything. Any ideas ? Code below, thanksLoading the ddl:       productCategories = CatalogAdmin.Admin_Product_Categories_NotIn(productId);        for (int i = 0; i < productCategories.Rows.Count; i++)        {       &...

how to set the cascading dropdownextendar value to the value of dropdownlist control
i have a cascading drop down extendar inside gridview , a dropdownlist control , a link button. i want to set the  value of dropdownextendar to the value of dropdown when i click the link button . <EditItemTemplate> <asp:DropDownList ID="ProvinceDDL" runat="server" /><act:CascadingDropDown ID="ProvinceCDD" runat="server" TargetControlID="ProvinceDDL" Category="Province" PromptText="- Please select a Province -" LoadingText="[Loading Province...]" ServicePath="LocactionService....

How Select one dropdownlist value using down arrows corresponding other dropdownlist value are select
I am using two dropdownlist box.One is used for country selection. other one is used for state selection.I am using dropdown selectedindexchanged event. when i click country dropdownlist corresponding state is selected in state dropdownlist box. How select the country in country dropdownlist using down arrow keys at the same time corresponding state is select in state dropdown list.R.Karthikeyan   kartmca: How select the country in country dropdownlist using down arrow keys at the same time corresponding state is select in state dropdown list.Generally, we don't implmenet...

DropDownList selected value determines value of second DropDownList
I have a quick question that I'm sure has been addressed before, but I can't seem to find a solution. I have two drop down list fields: startTime and endTime coded as such: <asp:DropDownList id="startTime" runat="server"> <asp:ListItem Value="00:00">12:00 A.M.</asp:ListItem> <asp:ListItem Value="00:30">12:30 A.M.</asp:ListItem> <asp:ListItem Value="01:00">1:00 A.M.</asp:ListItem> <asp:ListItem Value="...

Setting selected value of DropDownList inside a repeater control
Hi everyone, I am binding a repeater control to a datatable. I bind 3 columns from the datatable to repeater columns. With an extra column being a DropDownList.I like on the ItemDataBound event to read a datavalue from the current record that is not displayed/bound to the repeater control (call it column4) and once I read that value I like to set the selected value of the dropDownList Control Many thanks in advance    Hey, Well what you should do is find your item every time you want to read from the drop down list. So what basically happens is : yo...

How do I set the selected value of child DropDownList control cascading
 Hello i use ajax with  CascadingDropDown for 2 dropdownlist, one parent and the second chield  for save this informatio in data base is ok, no problem, the question is when i load this information from database to my aspx, i can put the selected value in the dropdownlist parent was very good, but when y tried to put the select value to the chield i have the problem, the razon is that the chield dropdownlist is no load ... how i can load the chiel dropdown for clien side,,  any examples please for example in the proyect

when loading a previously created record on to form, some controls are missing values(controls are depended on values selected in other controls)
Hello - I have created a page for Editing records.  this page will open up a chosen record and allow user to edit the data.On the form, I have 3 drop down lists.   The second dropdown list should fill values depending on value chosen in first dropdown list.  The third dropdown list should show/fill values depending on value chosen in second dropdown list.So taking this into count, i have several records with various data.  When I open-up/load a record in Edit form, only the first dropdown list has a value selected that was the pre-existing va...

dropdownlist's selected index value is not printing the selected value
Here iS my code .Aspx Page <asp:DropDownList ID="ddlkeyword" runat="server" Width="204px" DataTextField="Form" DataValueField="Form" OnSelectedIndexChanged="ddlkeyword_SelectedIndexChanged" >   </asp:DropDownList> <asp:Button ID="Button1" runat="server" Text="Submit" Width="110px" OnClick="Button1_Click" /> my aspx.cs protected void Page_Load(object sender, EventArgs e) { fillddlkeyword(); }protected void Page_Init(object sender, EventArgs e) { ...

DropDownList Selected value to set gridview selected Index and page
I have a dropdownlist and a gridview that is bound to the same objectdatasource. In the gridview I am allowing paging.  When user select an item in the dropdownlist list I want to position the gridview selected value to that page and that record. Is there an easy way to do this. I tried this code Protected Sub GrdMyIssues_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GrdMyIssues.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then 'find the position data selected in the If e.Row.Cells(1).Text = Me.ViewSt...

Hello, I'd like to set the value of a DropDownList based on a value of another field in the DataGrid.These values are from different <columns>.The first value shows up just fine in the "BoundColumn".But, the one I want to set in the TemplateColumn isn't working.Can someone take a look at my code snippet and see what I'm doing wrong? The Control I'm trying to set is called "ddlBenchStart".I'm trying to get the value from the "BenchStart" column.The values in "BenchStart" would be "Bench" or "Start&...

Setting a dropdownlist value based on a value in a Database
I have a page that allows users to create a user account. The aspx has various dropdowns that I populate by binding the dropdownlist to a dataset. OK so far? However, I also want a page where the user can edit their I need to be able to set the selected value based on what is saved in my user table and I also need to add the extra entries. Anyone know how I can do this easily. Do I need to join on my user table and my table that has all the various categories in order to get the selected value and add my other values?? Thanks, COG. For selecting the correct...

Handling DBNull Values in DropDownList Selected Value
I have a dropdown list inside a formview that is used to update a business opportunities table. Each opportunity may or may not have a salesperson assigned. On the update page I want to set the current value in the database as the selected value in the dropdown list. But if there is no salesperson assigned i need to handle the dbnull value and not try to set the selected value. Can someone give me some pointers? Should i handle this in the Formview_databinding event or in the Datasource for the opportunity Databinding event? How do i get at the cell of the datasource that i need to test for ...

DropDownList value selected base on database value?
Hi, is there anyway how I can allow my dropdownlist (on pageload event) to select the value base on the value found in the database, in addition I also want the dropdownlist to contain other values too however the selectedValue must be the one found in the database. Sorry if it sounds confusing. I'm using VB.NET 2003 with ASP.NET Thanks. Hi,Whatever you want to get selected for your dropdown use following code.if(dropdownid.items.findbyvalue("value") != null){    dropdownid.items.selecteditem = false;    dropdownid.items.findbyvalue("value").selected = t...

Web resources about - Setting a label value to a dropdownlist selected value -

Tag Synonyms - Stack Overflow
Q&A for professional and enthusiast programmers

Accidental entities - what about the UI?
This post is a follow-up to my previous blog post " Accidental entities - you don't need that identity ". In that post, we followed a consultant ...

Flash Builder Plug-in for SAP Netweaver Gateway - Adobe Labs
Search Navigation Labs Home Wiki Home Technologies Alchemy Blueprint Camera Profiles and DNG Profile Editor Cirrus ColdFusion 10 DNG Codec Fireworks ...

Air Astana - Join Now
Thank you for considering membership of Nomad Club – Kazakhstan’s first Frequent Flyer Program. You could soon be on your way to earning free ...

Languages -> C# Articles, Tutorials, Examples: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Blog - Avisra
Avisra Web Development Home Blog Services Contact The Blog Latest Module Builder: How can we make it better? Sitefinity by Chris on July 2, 2013 ...

Online Booking Tool Support
This Online Booking Tool Support page might help you as you navigate through the process of booking travel through your OBT provided by CWT. ...

@Html and beyond
... ); } return string .Format( “{0}{1}” , me.Member.Name, id); } The Html building methods are quite simple. Building a dropdownlist for a ...

Top Ranked Articles
Top Ranked Articles - Free source code and tutorials for Software developers and Architects.; Updated: 23 Feb 2013

Unit Testing DateTime.Now Without Isolation
public virtual void SeedWithYearsSince(DropDownList list, int year){ for (int index = year; index

Resources last updated: 1/7/2016 10:52:08 PM