Archana
Archana

Reputation: 243

cannot select the item in the dropdownlist, if the list has only one item

I have 2 dropdownlists, the 1st dropdownlist is bind to the second dropdownlist. If the second dropdownlist has only one item, this item cannot be selected and hence any function cannot be triggered. I went to any related answers that have already been uploaded...but it didn't help... It's important. Can someone help me out in this issue?

Upvotes: 0

Views: 2240

Answers (2)

Kapil Khandelwal
Kapil Khandelwal

Reputation: 16144

Just add a default ListItem to your second dropdown.

Eg.

<asp:ListItem Text="--Select--" Value="-1"></asp:ListItem>

You can add from c# also: Write this after you have populated dropdownlist from your datasource.

DropDownList1.Items.Insert(0, new ListItem("--Select--", "-1"));

Upvotes: 0

Curtis
Curtis

Reputation: 103348

this item cannot be selected and hence any function cannot be triggered

This item can't be selected, as its already selected. Therefore your onchange event etc, won't be triggered as its not being changed.

You have 2 options:

  1. Use @Kapil's suggestion of adding a "default" item, so that the 2nd item can be selected.
  2. Call the same function onload as well as onchange. Therefore when the page loads the cascaded dropdown will be populated as well:

.

protected void Page_Load(){
   BindCascadeDropDown();
}

protected void ddl_onchange() {
   BindCascadeDropDown();
}

protected void BindCascadeDropDown(){
   //Bind here
}

Upvotes: 2

Related Questions