Posted by Suthahar on Tuesday, September 13, 2011
No comments
Labels
Web
Reset Controls Value using Asp.net / Javascript / Jquery
Asp.net:
Dynamicaly you have to add controls means you don't know controls id so we can find control type in particular page after we can reset the page if
your using C#.net coding mean page will
refersh ,or if you are using javascript /j query means page not refresh
C#.net Coding:
protected void Button1_Click
(object sender, EventArgs e)
{
ResetControl(this);
}
public void ResetControl(Control Parent)
{
foreach (Control c in Parent.Controls)
{
switch (c.GetType().ToString())
{
case "System.Web.UI.WebControls.TextBox":
((TextBox)c).Text = "";
break;
case "System.Web.UI.WebControls.CheckBox":
((CheckBox)c).Checked = false;
break;
case "System.Web.UI.WebControls.RadioButton":
((RadioButton)c).Checked = false;
break;
}
}
}
Javascript:
You can add client event in button .
<script language="javascript" type='text/javascript'>
function CLSEvent()
{
for (i=0; i<document.forms[0].length; i++)
{
doc = document.forms[0].elements[i];
switch (doc.type)
{
case "text" :
doc.value = "";
break;
case "checkbox" :
doc.checked = false;
break;
case "radio" :
doc.checked = false;
break;
case "select-one" :
doc.options[doc.selectedIndex].selected = false;
break;case "select-multiple" :
while (doc.selectedIndex != -1)
{
indx = doc.selectedIndex;
doc.options[indx].selected = false;
}
doc.selected = false;
break;
default :
break;
}
}
}
</script>
JQuery:
function clear_form_elements(ele) {
$(ele).find(':input').each(function() {
switch(this.type) {
case 'password':
case 'select-multiple':
case 'select-one':
case 'text':
case 'textarea':
$(this).val('');
break;
case 'checkbox':
case 'radio':
this.checked = false;
}
});
}
0 Comments