How to search for any control on a web page and get value out of it

by Jagadish Pulakhandam on 10/18/2011 9:57:46 AM
Rated 0 from 0 votes
Brief: Demonstrates on parsing through all controls on a given page and tries to get a value of a control provided.
Posted to: ASP.NET Basic Controls
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Following is the screenshot:


Following is the markup:

01.<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
02. 
03.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
05.<head runat="server">
06.    <title></title>
07.</head>
08.<body>
09.    <form id="form1" runat="server">
10.    <div>
11.        <table>
12.            <tr>
13.                <td>
14.                    <asp:Label ID="Label1" runat="server" Text="Enter Value:"></asp:Label>
15.                </td>
16.                <td>
17.                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
18.                </td>
19.            </tr>
20.            <tr>
21.                <td>
22.                    <asp:Label ID="Label2" runat="server" Text="Select Value:"></asp:Label>
23.                </td>
24.                <td>
25.                    <asp:DropDownList ID="DropDownList1" runat="server">
26.                        <asp:ListItem Value="01">first</asp:ListItem>
27.                        <asp:ListItem Value="02">second</asp:ListItem>
28.                        <asp:ListItem Value="03">third</asp:ListItem>
29.                    </asp:DropDownList>
30.                </td>
31.            </tr>
32.            <tr>
33.                <td valign="top">
34.                    <asp:Label ID="Label3" runat="server" Text="Select Value:"></asp:Label>
35.                </td>
36.                <td>
37.                    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
38.                        <asp:ListItem Value="1">Sunday</asp:ListItem>
39.                        <asp:ListItem Value="2">Monday</asp:ListItem>
40.                        <asp:ListItem Value="3">Tuesday</asp:ListItem>
41.                    </asp:RadioButtonList>
42.                </td>
43.            </tr>
44.        </table>
45.        <hr />
46.        <table>
47.            <tr>
48.                <td>
49.                    <asp:Label ID="Label4" runat="server" Text="Enter control name to get value :"></asp:Label>
50.                </td>
51.                <td>
52.                    <asp:TextBox ID="txtControlName" runat="server"></asp:TextBox>
53.                </td>
54.                <td >
55.                    <asp:Button ID="btnShow" runat="server" Text="Show Value"
56.                        onclick="btnShow_Click" />
57.                </td>
58.            </tr>
59.            <tr>
60.                <td colspan="3">
61.                    <asp:Label ID="lblMsg" runat="server"></asp:Label>
62.                </td>
63.            </tr>
64.        </table>
65.    </div>
66.    </form>
67.</body>
68.</html>

Following is the code behind:

01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Web;
05.using System.Web.UI;
06.using System.Web.UI.WebControls;
07. 
08.namespace WebApplication1
09.{
10.    public partial class WebForm1 : System.Web.UI.Page
11.    {
12.        protected void Page_Load(object sender, EventArgs e)
13.        {
14. 
15.        }
16. 
17.        protected void btnShow_Click(object sender, EventArgs e)
18.        {
19.            Control ctl = FindControl(this, txtControlName.Text);
20.            if (ctl == null)
21.            {
22.                lblMsg.Text = "Not found";
23.            }
24.            else
25.            {
26.                lblMsg.Text = GetValueFromControl(ctl);
27.            }
28.        }
29. 
30.        private string GetValueFromControl(Control ctl)
31.        {
32. 
33.            switch (ctl.GetType().ToString())
34.            {
35.                case "System.Web.UI.WebControls.TextBox":
36.                    return ((TextBox)ctl).Text;
37.                case "System.Web.UI.WebControls.DropDownList":
38.                    DropDownList ddl = ((DropDownList)ctl);
39.                    if (ddl.SelectedItem != null)
40.                    {
41.                        return ddl.SelectedItem.Value;
42.                    }
43.                    break;
44.                case "System.Web.UI.WebControls.RadioButtonList":
45.                    RadioButtonList rbl = ((RadioButtonList)ctl);
46.                    if (rbl.SelectedItem != null)
47.                    {
48.                        return rbl.SelectedItem.Value;
49.                    }
50.                    break;
51.            }
52. 
53.            return string.Empty;
54. 
55.        }
56. 
57.        private Control FindControl(Control parent, string sCtlToFind)
58.        {
59.            //search for control in either upper/lower case
60.            if (string.Compare(parent.ID, sCtlToFind,true) == 0) return parent;
61. 
62.            foreach (Control ctl in parent.Controls)
63.            {
64.                Control ctlToReturn = FindControl(ctl, sCtlToFind);
65.                if (ctlToReturn != null) return ctlToReturn;
66.            }
67.            return null;
68.        }
69.    }
70.}


Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following


Discussion
No comments yet!