BrijBhasin
asked on
clearing form fields after postback
Is there a way to clear all the the form fields i.e textbox, dropdownlist after a postback event like button click
ASKER
I know about the IsPostBack Property but how do I clear the form fields?
1. turn off the viewstate, or
2. Response.Redirect(Request. RawURL) in your event handler
2. Response.Redirect(Request.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I get an error "Value of an integral type expected" here -- switch (ctrl.GetType())
adding a ToString() to the GetType() should do the trick, i reckon...
ASKER
that removed the error but the form fields don't get cleared. BTW this is for a web form so I changed the code as following and that didn't help either
foreach (object ctrl in Page.Controls)
{
switch (ctrl.GetType().ToString() )
{
case "System.Web.UI.WebControls .TextBox":
((TextBox)ctrl).Text = "";
break;
// case "System.Windows.Forms.Comb oBox":
// ((ComboBox)ctrl).Items.Cle ar();
// break;
default:
break;
// etcetera
}
}
foreach (object ctrl in Page.Controls)
{
switch (ctrl.GetType().ToString()
{
case "System.Web.UI.WebControls
((TextBox)ctrl).Text = "";
break;
// case "System.Windows.Forms.Comb
// ((ComboBox)ctrl).Items.Cle
// break;
default:
break;
// etcetera
}
}
did you try just redirecting yourself back to the same page?
ASKER
No I haven't tried that as my page has several panels where the visibilty is controlled by user selections .. if I do a redirect I will lose the panel the user was working on.
can you reload just the panel with the input fields in it then?
ASKER
ya I could load the panel and clear the fields individually but I thought I could have a more universal solution which I could use accross my application.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Following is a modified working version of a code snippet I found online .. similar to eliytres suggest .. I pass a Panel to this function
public static void ClearFormFields(System.Web .UI.Contro l parent)
{
//where parent is a panel;
foreach (Control c in parent.Controls)
{
if ((c.GetType() == typeof(TextBox)))
{
// is it a textbox?
TextBox t = (TextBox)c;
t.Text = "";
}
else if ((c.GetType() == typeof(DropDownList)))
{
// is it a dropdown list?
DropDownList d = (DropDownList)c;
d.ClearSelection();
}
else if ((c.GetType() == typeof(ListBox)))
{
// is it a listbox?
ListBox l = (ListBox)c;
l.ClearSelection();
}
else if ((c.GetType() == typeof(RadioButtonList)))
{
// is it a radiobutton list?
RadioButtonList rl = (RadioButtonList)c;
rl.ClearSelection();
}
else if ((c.GetType() == typeof(CheckBox)))
{
// is it a checkbox?
CheckBox chk = (CheckBox)c;
chk.Checked = false;
}
else if ((c.GetType() == typeof(CheckBoxList)))
{
// is it a radiobutton list?
CheckBoxList cl = (CheckBoxList)c;
cl.ClearSelection();
}
// if (c.HasControls())
// {
// ClearFields(c);
// }
}
}
public static void ClearFormFields(System.Web
{
//where parent is a panel;
foreach (Control c in parent.Controls)
{
if ((c.GetType() == typeof(TextBox)))
{
// is it a textbox?
TextBox t = (TextBox)c;
t.Text = "";
}
else if ((c.GetType() == typeof(DropDownList)))
{
// is it a dropdown list?
DropDownList d = (DropDownList)c;
d.ClearSelection();
}
else if ((c.GetType() == typeof(ListBox)))
{
// is it a listbox?
ListBox l = (ListBox)c;
l.ClearSelection();
}
else if ((c.GetType() == typeof(RadioButtonList)))
{
// is it a radiobutton list?
RadioButtonList rl = (RadioButtonList)c;
rl.ClearSelection();
}
else if ((c.GetType() == typeof(CheckBox)))
{
// is it a checkbox?
CheckBox chk = (CheckBox)c;
chk.Checked = false;
}
else if ((c.GetType() == typeof(CheckBoxList)))
{
// is it a radiobutton list?
CheckBoxList cl = (CheckBoxList)c;
cl.ClearSelection();
}
// if (c.HasControls())
// {
// ClearFields(c);
// }
}
}
ASKER
sorry I meant to say jaime_olivares suggestion
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuipageclassispostbacktopic.asp