how to clear dropdown boxes when i click clear button .

Posted by Chanti444 under ASP.NET on 10/28/2011 | Points: 10 | Views : 37287 | Status : [Member] | Replies : 6
hi,
my web application have five dropdown boxes ans one submit button and clear button .
if i choose each dropdown box have some selected value then how to clear all dropdownboxes when i click clear button ..............can u help me pls ....


thanq for advance

satya


Responses

Posted by: Abdulhaknet on: 10/29/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Friend ,

There are two things

dropdown list should be runat='server'

1. you want reset the selectedindex =0.
2. Clear the collection items from dropdownlist
then use
dropdownlist.items.Clear();


Chanti444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sathya4260 on: 10/29/2011 [Member] Starter | Points: 25

Up
0
Down
Just clear the drop down listed items in clear button click event
{

dropdownlist1.items.Clear();
dropdownlist2.items.Clear();
dropdownlist3.items.Clear();
dropdownlist4.items.Clear();
dropdownlist5.items.Clear();
}


Sathish Kumar S

Chanti444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: RohiK on: 11/2/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

another easy solution for clear all the values of form is- 'use javascript'

place javascript code in your <script> tag

<script language="javascript">


function clearForm(oForm) {

var elements = oForm.elements;

oForm.reset();

for (i = 0; i < elements.length; i++) {

field_type = elements[i].type.toLowerCase();

switch (field_type) {

case "text":
case "password":
case "textarea":


elements[i].value = "";
break;

case "radio":
case "checkbox":
if (elements[i].checked) {
elements[i].checked = false;
}
break;

case "select-one":
case "select-multi":
elements[i].selectedIndex = 0;
break;

default:
break;
}
}
}
</script>


your clear button is as follows
   <input type="button" name="clear" value="Reset" onclick="clearForm(this.form);">


simple!!
plz mark as answer if you got the solution.


Happy Programming!!
Rohi

Chanti444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: perfectchourasia-9163 on: 11/15/2011 [Member] Starter | Points: 25

Up
0
Down
the best way is to use select on zero index

public void ddlbinduser()

{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["linkbuilding"].ToString().Trim());
SqlDataAdapter da = new SqlDataAdapter("select * from user_detail where role_id=3", con);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
ddluser.DataSource = ds.Tables[0];
ddluser.DataTextField = "name";
ddluser.DataValueField = "id";
ddluser.DataBind();
ddluser.Items.Insert(0, new ListItem("-Select-"));
}
}


ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/

Chanti444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayeshl on: 11/17/2011 [Member] Starter | Points: 25

Up
0
Down
hi friend,
thanks to post this question...

here is answer for it..


clear selection of that dropdown by below code of line.

dropdown1.ClearSelection();


than clear all ItemList

dropdown1.Item.Clear()


happy coding
jayesh l

Chanti444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anandkumarviky on: 9/22/2016 [Member] Starter | Points: 25

Up
0
Down
Dropdown item can be clear by removing items from dropdown or clear innerHTML of dropdown.

1-
 Dropdownlist.Items.Clear()

Using JQuery
$("#drpdownid").innerHTML('')
;
for more details http://www.csharpmagic.com/clear-dropdown-values-using-javascript/

Chanti444, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response