Simon Simon - 1 month ago 10
ASP.NET (C#) Question

asp.net dropdown list always select 0 index with postback

working on a simple project after click a button i'm clear all textbox text and I want dropdown list select it's 0 index value..........I'm trying to write some jquery code to complete this work

$(document).ready(function () {
$("#btnAdd").bind("click", function () {
$("#productNameDDL")[0].selectedIndex = 0;
});
});


but don't work this code what should have I do now.Thanks in advanced

Answer

Since you tagged your question with asp.net, the problem is most likely the id of the DropDownList. Use:

$("#<%= productNameDDL.ClientID %>").prop('selectedIndex', 0);

Asp.net renames the ID propery of a Control on the client side to ensure unqiue id's. See the Microsoft Site for more info.

UPDATE

To change the SelectedIndex of a DropDownList programatically you can use this.

protected void Button1_Click(object sender, EventArgs e)
{
    productNameDDL.SelectedIndex = 0;
    //or
    productNameDDL.SelectedValue = "value";
}