Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Professionals
|
ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Professionals 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 June 27th, 2009, 09:27 AM
Authorized User
 
Join Date: Apr 2007
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default Get value from javascript function into Dropdown and Display selected Value

Hello Friends,

I've two dropdownlist(ddlCountry,ddlState). Onchange event of CountryDropdown(ddlCountry) StateNames are displayed into StateName dropdown using javascript. And it's working fine but prooblem is if i print the value of selected StateName on a button click at code-behind i got an error Object reference set to an instance of an object. So can anybody let me know what should i do know

Here is my code

This is my javascript function
function myFunc()
{
if(document.getElementById("ddlCountry").value=="I ndia")
{
//Remove Existing Values
var e=document.getElementById("ddlState");
while(e.firstChild)
{
e.removeChild(e.firstChild);
}
var myArray=new Array(2);
myArray[0]="Delhi";
myArray[1]="Mumbai";


for(var i=0;i<myArray.length;i++)
{
//Option(Text,Value)
e.options[i]=new Option(myArray[i]);
}

}
else if(document.getElementById("ddlCountry").value=="A merica")
{
//Remove Existing Values
var e=document.getElementById("ddlState");
while(e.firstChild)
{
e.removeChild(e.firstChild);
}

var myArray=new Array(2);
myArray[0]="Washington";
myArray[1]="LA";


for(var i=0;i<myArray.length;i++)
{
//Option(Text,Value)
e.options[i]=new Option(myArray[i]);
}
}

}

This is my .aspx code
<asp:DropDownList ID="ddlCountry" runat="server" onchange="myFunc(this)">
<asp:ListItem Text="Select Country" Value="Select Country"></asp:ListItem>
<asp:ListItem Text="India" Value="India"></asp:ListItem>
<asp:ListItem Text="America" Value="America"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlState" runat="server">
</asp:DropDownList>

This is my Code-Behind(on button click event) code
string name = ddlCountry.SelectedItem.Text+","+ddlState.Selected Item.Text;
Label1.Text = name.ToString();

And ddlState.SelectedItem.Text getting null that's why that's throwing error
 
Old June 27th, 2009, 11:12 AM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

You are populating the ddlState control, but that doesn't mean there is a selected item there. There is no "default" selected item when a dropdownlist is first populated. An item must be explicitly selected (either on the client or programmatically) for SelectedItem to have a non-null value.

You probably should consider using validation to force the user to make a selection in ddlState before the postback.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to highlight selected dropdown values. sheetm J2EE 0 June 5th, 2007 05:04 AM
Selected value from dropdown menu wont display. mizjinx Classic ASP Databases 0 May 20th, 2007 08:24 PM
selected item in dropdown not saved on postback rathbird ADO.NET 3 March 6th, 2006 08:44 AM
Getting selected item in dropdown to match XML juliaPGATOUR XSLT 1 December 11th, 2003 05:56 AM
Title: dropdown list as selected according to the san_only ASP.NET 1.x and 2.0 Application Design 1 June 4th, 2003 01:55 AM





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