Reset Function in Javascript
Hi
I am developing a user control with dropdown controls in asp.net. The requirement is that I display the data from the database int开发者_如何学Goo the dropdown such that when selected a value, it should display corresponding records (to that particular value) into the gridview. Also, there is an ‘ALL’ option in the dropdown which will get all the records regardless of the match.
the dropdown controls are with default as ‘Select One’ (added using the edit item functionality of the dropdown using the value =””
property),
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="">--select one--</asp:ListItem>
</asp:DropDownList>
and “ALL” option is add to them from the code behind with index 0.
Eg: DropDownList1.Items.Insert(0, "All");
So I need to write a javascript function for reset which, when clicked displays the ‘select One’ option in the dropdown instead of “ALL” (which is what happening now). Any help is really appreciated.
use like this
document.getElementById("<%#dropdownlist1.ClientID%>").value = 0;
or
document.getElementById("<%#dropdownlist1.ClientID%>").selectedIndex = 0;
If you have a drop-down option of both <option value="">
and <option value="0">
you can just use document.getElementById('dropdown_ID').value = 0
to select the option with zero value and document.getElementById('dropdown_ID').value = ""
to select the option with empty value. This way they can easily be determine as 2 different option values as sometimes ""
may be 0
as well when evaluated depending how some function handles it.
Why can't you make the zeroth item in the dropdown the "--select one--" entry?
You can use JavaScript onchange
event to check which option is set.
http://www.javascriptkit.com/jsref/select.shtml#aform
精彩评论