Iterate Html Controls in Asp.Net Page

| August 5, 2010 | 1 Comment

Generally websites are designed in Html . And so controls are named accordingly. For example, for creating Checkbox control in Html you would need to write following line:

<input id="FirstChkBox" type="checkbox" value="C#" />CSharp

And if you want implemet server side validation for your controls you would change the Html controls by adding attribute run at=”server” as (for Asp.Net)

<input id="FirstChkBox" type="checkbox" value="C#" />CSharp

Now consider the scenario where you are having four or more Checkbox Controls in your Web Application and all are of Html types. You need to iterate all Checkbox controls and determine their properties such as Name, Checked, Value and much. So we will learn how to interate html controls in asp.net page.

Iterate Html Controls in Asp.Net

Iterate Html Controls in Asp.Net

Code Snippet:

So check out the code here.

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!--<span class="hiddenSpellError" pre=""-->DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<span class="hiddenSpellError" pre="">CheckBox</span> UserId(s)
</head>
<body>
    <form id="form1" runat="server" defaultbutton="btnGet">
    <div>
    <h2><asp:Label ID="lblHead" ForeColor="Orange" runat="server">Iterate HTML Control's in Asp.Net Page
    </asp:Label></h2><hr />
    Select Language you like more:<br /><br />
    <input id='FirstChkBox' runat='server' type='checkbox' value='C#'/>CSharp

    <input id='SecondChkBox' runat='server' type='checkbox' value='ASP.NET'/>Asp.Net

    <input id='ThirdChkBox' runat='server' type='checkbox' value='SQL'/>Sql

    <input id='FourthChkBox' runat='server' type='checkbox' value='JAVA'/>Java



    <asp:Button ID="btnGet" runat="Server" Text="Get Values" OnClick="btnGet_Click" /><br /><br />
    <asp:<span class="hiddenSpellError">TextBox ID="txtAllControls" ForeColor="DarkBlue" runat="server" TextMode="MultiLine"
    Width="200" Height="100"></asp:TextBox>
    </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;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
protected void btnGet_Click(object sender, EventArgs e)
{
// Create object for the control you want to check
HtmlInputCheckBox chk = new HtmlInputCheckBox();
ArrayList list = GetAllControls(new ArrayList(), chk.GetType(), this);

foreach (Control c in list)
{
if (c is HtmlInputCheckBox)
{
string name = ((HtmlInputCheckBox)c).Name;
bool check = ((HtmlInputCheckBox)c).Checked;
string value = ((HtmlInputCheckBox)c).Value;
txtAllControls.Text += "Name: " + name + "\nStatus: " + check.ToString()
+ "\nValue: " + value + "\n******************\n";
}
}
}

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: .NET, Asp.Net

Leave a Reply

%d bloggers like this: