Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Professionals
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 27th, 2009, 09:27 AM
Authorized User
 
Join Date: Apr 2007
Location: Delhi, Delhi, India.
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
  #2 (permalink)  
Old June 27th, 2009, 11:12 AM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
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(); 
}
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:07 PM.


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