Reset All Asp.net control or Clear All Page Control
---------------------------
use this
-----------------
protected void btnReset_Click(object sender, EventArgs e)
{
Control[] controls = { btnSubmit,txtName,ddlCountry,hfCountryID,lblCityName,chkYes,chkNo };
ClearAllFormControl(controls);
}
void ClearAllFormControl(Control[] controls)
{
foreach (Control c in controls)
{
switch (c.GetType().ToString())
{
case "System.Web.UI.WebControls.Button":
{
//((Button)c).Text = "Submit";
} break;
case "System.Web.UI.WebControls.TextBox":
{
((TextBox)c).Text = string.Empty;
} break;
case "System.Web.UI.WebControls.DropDownList":
{
if (((DropDownList)c).SelectedIndex > 0) ((DropDownList)c).SelectedIndex = 0;
} break;
case "System.Web.UI.WebControls.HiddenField":
{
((HiddenField)c).Value = string.Empty;
} break;
case "System.Web.UI.WebControls.Label":
{
((Label)c).Text = string.Empty;
} break;
case "System.Web.UI.WebControls.CheckBox":
{
((CheckBox)c).Checked = false;
} break;
}
}
}
OR
___________
// use this namespace : using System.Web.UI.WebControls;
void ClearAllPageControl()
{
Control[] controllist = { ddlTypeofUpdating, txtOrderNo, txtOrderDate, txtDescription, fuOrderCopy, hfOrderCopy, hfOrderId };
foreach (Control control in controllist)
{
if (control is TextBox)
{
TextBox textBox = (TextBox)control;
textBox.Text = string.Empty;
}
else if (control is DropDownList)
{
DropDownList ddl = (DropDownList)control;
if (ddl.SelectedIndex > 0)
ddl.SelectedIndex = 0;
}
else if (control is FileUpload)
{
FileUpload fu = (FileUpload)control;
fu = null;
}
else if (control is HiddenField)
{
HiddenField hf = (HiddenField)control;
hf.Value = "0";
}
}
}
---------------------------
use this
-----------------
protected void btnReset_Click(object sender, EventArgs e)
{
Control[] controls = { btnSubmit,txtName,ddlCountry,hfCountryID,lblCityName,chkYes,chkNo };
ClearAllFormControl(controls);
}
void ClearAllFormControl(Control[] controls)
{
foreach (Control c in controls)
{
switch (c.GetType().ToString())
{
case "System.Web.UI.WebControls.Button":
{
//((Button)c).Text = "Submit";
} break;
case "System.Web.UI.WebControls.TextBox":
{
((TextBox)c).Text = string.Empty;
} break;
case "System.Web.UI.WebControls.DropDownList":
{
if (((DropDownList)c).SelectedIndex > 0) ((DropDownList)c).SelectedIndex = 0;
} break;
case "System.Web.UI.WebControls.HiddenField":
{
((HiddenField)c).Value = string.Empty;
} break;
case "System.Web.UI.WebControls.Label":
{
((Label)c).Text = string.Empty;
} break;
case "System.Web.UI.WebControls.CheckBox":
{
((CheckBox)c).Checked = false;
} break;
}
}
}
OR
___________
// use this namespace : using System.Web.UI.WebControls;
void ClearAllPageControl()
{
Control[] controllist = { ddlTypeofUpdating, txtOrderNo, txtOrderDate, txtDescription, fuOrderCopy, hfOrderCopy, hfOrderId };
foreach (Control control in controllist)
{
if (control is TextBox)
{
TextBox textBox = (TextBox)control;
textBox.Text = string.Empty;
}
else if (control is DropDownList)
{
DropDownList ddl = (DropDownList)control;
if (ddl.SelectedIndex > 0)
ddl.SelectedIndex = 0;
}
else if (control is FileUpload)
{
FileUpload fu = (FileUpload)control;
fu = null;
}
else if (control is HiddenField)
{
HiddenField hf = (HiddenField)control;
hf.Value = "0";
}
}
}
No comments:
Post a Comment