Reset Text Boxes in ASP.Net - Part 2

In my last Article "Reset Text Boxes in ASP.Net" I suggested a way to clear all text boxes in ASP.NET form(C#). But in real time situation life is not that easy and we are faced with more than just text boxes. So this time I'll show how to clear some of the very comman controls on a ASP.NET form.

C#

private void ClearForm(Control parent)
{
foreach (Control ctl in parent.Controls)
{
if (ctl.Controls.Count > 0)
{
ResetFormControlValues(ctl);
}
else
{
switch(ctl.GetType().ToString())
{
case "System.Web.UI.WebControls.TextBox":
((TextBox)ctl).Text = "";
break;
case "System.Web.UI.WebControls.CheckBox":
((CheckBox)ctl).Checked = false;
break;
case "System.Web.UI.WebControls.RadioButton":
((RadioButton)ctl).Checked = false;
break;

}
}
}
}

VB.NET

PrivateSub ClearForm(ByVal parent As Control)
ForEach ctl As Control In parent.Controls
If ctl.Controls.Count > 0 Then
ResetFormControlValues(ctl)
Else
SelectCase ctl.GetType().ToString()
Case"System.Web.UI.WebControls.TextBox"
CType(ctl, TextBox).Text = ""
Case"System.Web.UI.WebControls.CheckBox"
CType(ctl, CheckBox).Checked = False
Case"System.Web.UI.WebControls.RadioButton"
CType(ctl, RadioButton).Checked = False
EndSelect
EndIf
Next ctl
End Sub

Hey!! this can also be done through JavaScript.... but that in the next post.. so plz join again.
Hope that helps you all, please comment if any more to add, improve or anything.

Comments

Popular posts from this blog

How does dual SIM mobile work

My First Post

Nokia 7705 Twist