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
|