Looping through Controls in Asp.Net Page

| August 21, 2010 | 0 Comments

Sometimes in programming we need to take control of all Controls in Asp.Net Page. We wish if we can somehow modify their behavior together at runtime. We will learn how to perform looping through controls in asp.net page. Earlier I demonstrated you to Iterate HTMLControls in Page. The logic is same here. But it’s little bit changed.

Requirements

Following are little bit requirement for successful testing of the code:

  1. Visual Studio 2005 or more
  2. .NET Framework v2.0 or more
  3. Little modifications in code according to your need.

 Concepts Covered

  • Iterating controls in Asp.Net Page.
  • Adding controls to ArrayList
  • Looping and Casting.

Code Snippet

There are total six controls in Page (in pairs). They are TextBox, CheckBox and RadioButton. Since at run time there are lot of controls get created for single Asp.Net page. If there is some space on page the Literal Controls are created for the same. And the controls get arrange in their parent controls so in code we have tried to iterate controls in the control till the last control not found. For rest please see the code snippet below:

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>Learn ASP.NET with Vikram</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2 style="background-color:Green; color:White; font-family:Calibri">LEARNING ASP.NET</h2>
    FirstCB:<asp:CheckBox ID="CheckBox1" runat="server" />  SecondCB:<asp:CheckBox ID="CheckBox2" runat="server" /><br /><br />
    Name: <asp:TextBox ID="TextBox1" runat="server" />  Password:<asp:TextBox ID="TextBox2" runat="server" /><br /><br />
    Male: <asp:RadioButton ID="RadioButton1" runat="server" />  Female: <asp:RadioButton ID="RadioButton2" runat="server" /><br />
    <asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" />
    </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    protected void btnClear_Click(object sender, EventArgs e)
    {
        ArrayList list = new ArrayList();

        ArrayList pageControl = new ArrayList();
        pageControl.Add(new TextBox());
        pageControl.Add(new RadioButton());
        pageControl.Add(new CheckBox());

        foreach (Control ctrl in pageControl)
        {
            list.Clear();
            list = GetAllControls(new ArrayList(), ctrl.GetType(), this);

            foreach (Control c in list)
            {
                if (c is TextBox)
                {
                    TextBox txt = (TextBox)c;
                    txt.Text = String.Empty;
                }

                if (c is CheckBox)
                {
                    CheckBox chk = (CheckBox)c;
                    chk.Checked = false;
                }

                if (c is RadioButton)
                {
                    RadioButton rb = (RadioButton)c;
                    rb.Checked = false;
                }
            }
        }
    }

    public static ArrayList GetAllControls(ArrayList list, Type type, Control parent)
    {
        foreach (Control c in parent.Controls)
        {
            if (c.GetType() == type)
            {
                list.Add(c);
            }
            if (c.HasControls())
            {
                list = GetAllControls(list, type, c);
            }
        }
        return list;
    }
}

Category: Asp.Net

Leave a Reply

%d bloggers like this: