How to clear or reset all Asp.net Controls…………………….
In this topic we will discuss How to clear or reset all the
asp.net controls/fields Like All TextBox, All Label, All CheckBox, All
CheckBoxList, All RadioButton, All RadioButtonList, All DropDownList
etc that are present on the web page/form
Source Code with Default.aspx...............
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
height: 23px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td colspan="2" align="center">
<b><i>How to clear or reset all Asp.net Controls </i></b> </td>
</tr>
<tr>
<td>TextBox Control</td>
<td>TextBox Control</td>
<td>
<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
TextBox
Control</td>
<td class="style2">
<asp:TextBox ID="TextBox2"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
RadioButton</td>
<td class="style2">
<asp:RadioButton ID="RadioButton1"
runat="server"
Text="Male"
/>
<asp:RadioButton ID="RadioButton2"
runat="server"
Text="FeMale"
/>
</td>
</tr>
<tr>
<td>
DropDownList</td>
<td>
<asp:DropDownList ID="DropDownList1"
runat="server">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Noida</asp:ListItem>
<asp:ListItem>Gr. Noida</asp:ListItem>
<asp:ListItem>New Delhi</asp:ListItem>
<asp:ListItem>Ghaziabad</asp:ListItem>
<asp:ListItem>Varanasi</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
RadioButtonList</td>
<td>
<asp:RadioButtonList ID="RadioButtonList1"
runat="server"
RepeatDirection="Horizontal">
<asp:ListItem>First Div</asp:ListItem>
<asp:ListItem>Second Div</asp:ListItem>
<asp:ListItem>Third Div</asp:ListItem>
<asp:ListItem>Fail</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td>
CheckBox</td>
<td>
<asp:CheckBox ID="CheckBox1"
runat="server"
Text="High
School" />
<asp:CheckBox ID="CheckBox2"
runat="server"
Text="Intermidiate"
/>
<asp:CheckBox ID="CheckBox3"
runat="server"
Text="Graduation"
/>
<asp:CheckBox ID="CheckBox4"
runat="server"
Text="Post
Graduation" />
</td>
</tr>
<tr>
<td>
CheckBoxList</td>
<td>
<asp:CheckBoxList ID="CheckBoxList1"
runat="server">
<asp:ListItem>Pan Card</asp:ListItem>
<asp:ListItem>Voter Id</asp:ListItem>
<asp:ListItem>Adhar Card</asp:ListItem>
<asp:ListItem>Driving Licence</asp:ListItem>
<asp:ListItem>None of Above</asp:ListItem>
</asp:CheckBoxList>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="ResetAllControl"
runat="server"
onclick="ResetAllControl_Click"
Text="Reset All Control" />
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Source Code with Default.aspx.cs…………………………….
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default :
System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs
e)
{
}
protected void
ResetAllControl_Click(object sender, EventArgs e)
{
foreach (Control
control in form1.Controls)
{
//clear all the TextBox controls on the page ....
if (control is TextBox)
{
TextBox tt = (TextBox)control;
tt.Text
= "";
}
//clear all the Label controls on the page ....
else if
(control.GetType() == typeof(Label))
{
((Label)(control)).Text = string.Empty;
}
//this code for DropDownList it to the very first item Like
"Select"
else if
(control.GetType() == typeof(DropDownList))
{
((DropDownList)(control)).SelectedIndex = 0;
}
//unchecked for all the CheckBox controls
else if
(control.GetType() == typeof(CheckBox))
{
((CheckBox)(control)).Checked = false;
}
//unchecked for all the CheckBoxList controls on the page
else if
(control.GetType() == typeof(CheckBoxList))
{
((CheckBoxList)(control)).ClearSelection();
}
//unchecked for all the RadioButton controls on the page
else if
(control.GetType() == typeof(RadioButton))
{
((RadioButton)(control)).Checked = false;
}
//unchecked for all the RadioButtonList controls on the page
else if
(control.GetType() == typeof(RadioButtonList))
{
((RadioButtonList)(control)).ClearSelection();
}
}
}
}
Result
0 comments:
Post a Comment