Click here to Skip to main content
11,796,393 members (78,134 online)
Click here to Skip to main content

Enable ReadOnly Mode for your WebForms!

, 24 Feb 2004 55.9K 676 41
Rate this:
Please Sign up or sign in to vote.
This article will show you, how to implement a readonly mode for webforms.


A common webform should be able to display the content in:

  • Edit mode (to change data)

    Edit Mode

  • Read only mode (to display data)

    Read Mode

Let's say, you have to program a webform with several controls (textboxes, dropdowns, checkboxes....) to add, edit and display detail information (e. g. customers details).

Wouldn't it be nice, if you could just switch between edit mode (with all different input controls) and display mode (labels instead of the input controls) by calling a method?


To implement this behavior, I wrote a method in a base class (derived from System.Web.UI.Page), which is accessible from any webform deriving from this base class.

First of all, I collect all (self defined) input-controls in a DataTable (the DataTable has three columns: the ControlCollection [in which the controls reside], the control and index of the control):

/// Check all controls inside Collection and put them into a ControlContainer
/// <span class="code-SummaryComment"><PARAM name="cc">ControlCollection</PARAM>

Second, I replace the input-controls with Labels...

/// Replace writable controls with read-only controls
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 = 
        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 should 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);


The base class does support GridLayout and FlowLayout (for both, examples are included).

This code should give you an idea of how to implement the intention and could be enhanced and optimized.

Feel free to add comments.



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...

Comments and Discussions

GeneralGreat Code! Pin
eltule12-Jan-07 6:45
membereltule12-Jan-07 6:45 
Generalcustom control version Pin
DanInManchester31-Jan-06 11:40
memberDanInManchester31-Jan-06 11:40 
QuestionAny solution for ViewState? Pin
Bruno Raymond10-Feb-05 9:36
memberBruno Raymond10-Feb-05 9:36 
GeneralGreat, but... Pin
rvern31-Jan-05 11:16
memberrvern31-Jan-05 11:16 
QuestionRe: Great, but... Pin
AdnanDev5-May-06 13:46
memberAdnanDev5-May-06 13:46 
Hi Randy:

I really appriciate if u can email me the VB code.

GeneralNICE Pin
Eric Newton6-Nov-04 10:13
memberEric Newton6-Nov-04 10:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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