Click here to Skip to main content
12,817,318 members (33,046 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » Howto » Downloads


41 bookmarked
Posted 24 Feb 2004

Enable ReadOnly Mode for your WebForms!

, 24 Feb 2004
This article will show you, how to implement a readonly mode for webforms.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Trivadis.Spieler.ReadOnly {
	/// <summary>
	/// Summary description for BasePage.
	/// </summary>
	public class BasePage: System.Web.UI.Page {

		private DataTable _ControlContainer = null;
		private string _MultiDelimiter = ", ";

		public BasePage():base() {}

		/// <summary>
		/// Set the passed Controls in a read-only mode
		/// </summary>
		/// <param name="cc"></param>
		public void SetReadOnly(ControlCollection cc) {
		/// <summary>
		/// Create ControlContainer on the fly
		/// (better should be a typed Table / typed object)
		/// </summary>
		/// <returns></returns>
		private DataTable CreateDataTable() {
			_ControlContainer = new DataTable("TextBoxes");
			DataColumn dcControlColl = new DataColumn("ControlCollection", typeof(System.Object));
			DataColumn dcControl = new DataColumn("Control", typeof(System.Object));
			DataColumn dcIndex = new DataColumn("Index", typeof(System.Int32));

			return _ControlContainer;

		/// <summary>
		/// Replace writable controls with read-only controls
		/// </summary>
		private void ReplaceControls() {

			//Change sort-order, because removing should start from the highest index
			DataRow[] drs = _ControlContainer.Select("", "Index desc");

			foreach(DataRow dr in drs) {

				ControlCollection cc = (ControlCollection)dr["ControlCollection"];
				Control c = (Control)dr["Control"];
				int index = cc.IndexOf(c);

				//Remove existing Control

				Label l = new Label();
				l.CssClass = "lbl";

				//WebControl and HtmlControl don't have an equal Base Class (!)
				if (c is WebControl)
					l.Style.Add("LEFT", ((WebControl)c).Style["LEFT"]);
					l.Style.Add("TOP", ((WebControl)c).Style["TOP"]);
					l.Style.Add("POSITION", ((WebControl)c).Style["POSITION"]);
					l.Style.Add("Z-INDEX", ((WebControl)c).Style["Z-INDEX"]);
				else if (c is HtmlControl)
					l.Style.Add("LEFT", ((HtmlControl)c).Style["LEFT"]);
					l.Style.Add("TOP", ((HtmlControl)c).Style["TOP"]);
					l.Style.Add("POSITION", ((HtmlControl)c).Style["POSITION"]);
					l.Style.Add("Z-INDEX", ((HtmlControl)c).Style["Z-INDEX"]);

				if (c is TextBox) {
					l.Text = ((TextBox)c).Text;
				else if (c is DropDownList || c is RadioButtonList) {
					if (((ListControl)c).SelectedValue != string.Empty)
						l.Text = ((ListControl)c).Items.FindByValue(((ListControl)c).SelectedValue).Text;
				else if (c is CheckBoxList || c is ListBox) {
					if (((ListBox)c).SelectionMode == ListSelectionMode.Multiple) {
						foreach(ListItem li in ((ListBox)c).Items) {
							if (li.Selected) l.Text += li.Text + _MultiDelimiter;
						if (l.Text.Length > 0)
							l.Text = l.Text.Substring(0, l.Text.LastIndexOf(_MultiDelimiter));
					else {
						l.Text = ((ListBox)c).SelectedValue;
				else if (c is RadioButton || c is CheckBox) {
					l.Text = ((CheckBox)c).Checked.ToString();				
				else {
					//Validators and Buttons does not exist in readonly mode
					if (!(c is BaseValidator) && !(c is Button))
						//unknown type
						throw new ArgumentOutOfRangeException("Controltype not supported at the moment", c.ToString());

				//Add new read-only Control
				cc.AddAt(index, l);



		/// <summary>
		/// Check all controls inside Collection and put them into a ControlContainer
		/// </summary>
		/// <param name="cc">ControlCollection</param>
		private void CollectControls(ControlCollection cc) {
			foreach(Control c in cc) {
				//Which Controls do we support?
				if (c is TextBox || 
					c is ListBox || 
					c is DropDownList || 
					c is RadioButton ||
					c is RadioButtonList ||
					c is CheckBox ||
					c is CheckBoxList ||
					c is BaseValidator ||
					c is Button)
					_ControlContainer.Rows.Add(new object[]{cc, c, cc.IndexOf(c)});
				//Are there further Controls inside current Control?
				if (c.HasControls())	


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Patrick Spieler
Web Developer
Switzerland Switzerland
Consultant for Trivadis AG in Switzerland.
Interested in .NET, Architectures, Patterns and Web 2.0.
Blog and articles at

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 25 Feb 2004
Article Copyright 2004 by Patrick Spieler
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid