Asp.Net: Clear inputfields after form submit

by Pieter Brinkman 15. April 2009 02:56

Every time a form is successful summited I need to clear all input-fields to the default values and give feedback to the user. I wrote a method ClearControl that can have a control as input parameter. This method will set the control based on his type back to the defaultvalue.

 public static void ClearControl(Control control)
{
 switch (control.GetType().Name)
 {
  case "TextBox":
   TextBox txtBox = (TextBox)control;
   txtBox.Text = "";
   break;
  case "DropDownList":
   DropDownList ddl = (DropDownList)control;
   ddl.SelectedIndex = 0;
   break;
  case "CheckBox":
   CheckBox chk = (CheckBox)control;
   chk.Checked = false;
   break;
  case "CheckBoxList":
   CheckBoxList chkList = (CheckBoxList)control;
   foreach (ListItem li in chkList.Items)
    li.Selected = false;
   break;
  case "Panel":
   ClearFields((Panel)control);
   break;
  case "RadioButtonList":
   RadioButtonList rbl = (RadioButtonList)control;
   rbl.SelectedIndex = -1;
   break;
 }
}

 

To use this method I wrote a method 'ClearFields' that accepts a View or Container control. You can add every type of control that has the .Controls property.

public static void ClearFields(Panel container)
{
 foreach (Control control in container.Controls)
 {
  ClearControl(control);
 }
}

public static void ClearFields(View container)
{
 foreach (Control control in container.Controls)
 {
  ClearControl(control);
 }
}

 

When I have some time left I will try to implement the ClearFields Method as a ExtensionMethod.

Cheers,

Pieter

 

Tags: , , ,

ASP.Net | Controls

Asp.Net: Databinding a array of strings

by Pieter Brinkman 30. January 2009 03:47

You can use a string array as datasource and view the string values by using the Container.DataItem property.

Code example

Codebehind:

string[] testData = {"1","two","3","4"};
rptDemo.DataSource = testData;
rptDemo.DataBind();


And in the .aspx:

<asp:Repeater runat="server" ID="rptDemo">
    <ItemTemplate>
        <%# Container.DataItem %>
    </ItemTemplate>
</asp:Repeater>

Tags: , , , ,

ASP.Net | Controls

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About Me

My name is Pieter Brinkman I am a .NET Software Engineer for Achmea IT in De Meern, The Netherlands. My interests are mainly web applications created with ASP.NET, MSSQL and Silverlight.

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

RecentComments

Comment RSS

Most comments

club penguin cheats club penguin cheats
4 comments
us United States
Web Design Company Web Design Company
2 comments
Web design Web design
2 comments
gb United Kingdom