<p>Has anyone ever written a function that can convert all of the controls on an aspx page into a read only version?  For example, if UserDetails.aspx is used to edit and save a users information, if someone with inappropriate permissions enter the page, I would like to render it as read-only.  So, most controls would be converted to labels, loaded with the corresponding data from the editable original control.</p>

<p>I think it would likely be a fairly simple routine, ie:  </p>

<pre><code>Dim ctlParent As Control = Me.txtTest.Parent  
Dim ctlOLD As TextBox = Me.txtTest  
Dim ctlNEW As Label = New Label  
ctlNEW.Width = ctlOLD.Width  
ctlNEW.Text = ctlOLD.Text  
ctlParent.Controls.Remove(ctlOLD)  
ctlParent.Controls.Add(ctlNEW)  
</code></pre>

<p>...is really all you need for a textbox --> label conversion, but I was hoping someone might know of an existing function out there as there are likely a few pitfalls here and there with certain controls and situations.</p>

<p>Update:<br>
- Just setting the ReadOnly property to true is not a viable solution, as it looks dumb having things greyed out like that.
- Avoiding manually creating a secondary view is the entire point of this, so using an ingenious way to display a read only version of the user interface that was built by hand using labels is wat I am trying to avoid.</p>

<p>Thanks!!</p>

[{'answer_id': 77938, 'author': 'Quintin Robinson', 'author_id': 12707, 'author_profile': 'https://Stackoverflow.com/users/12707', 'pm_score': 2, 'selected': False, 'text': '<p>You could use a multiview and just have a display view and an edit view.. then do your assignments as:</p>\n\n<pre><code>lblWhatever.Text = txtWhatever.Text = whateverOriginatingSource;\nlblSomethingElse.Text = txtSomethingElse.Text = somethingElseOriginatingSource;\n\nmyViews.SelectedIndex = myConditionOrVariableThatDeterminesEditable ? 0 : 1;\n</code></pre>\n\n<p>then swap the views based on permissions.</p>\n\n<p>not the most elegant but will probably work for your situation.</p>\n\n<p>Maybe I should elaborate a little.. dismiss the psuedo (not sure if I have the selectedindex yada yada right.. but you get the point).</p>\n\n<pre><code>&lt;asp:Multiview ID="myViews" SelectedIndex="1"&gt;\n    &lt;asp:View ID="EditView"&gt;\n        &lt;asp:TextBox ID="txtWhatever" /&gt;&lt;br /&gt;\n        &lt;asp:TextBox ID="txtSomethingElse" /&gt;\n    &lt;/asp:View&gt;\n    &lt;asp:View ID="DisplayView"&gt;\n        &lt;asp:Label ID="lblWhatever" /&gt;&lt;br /&gt;\n        &lt;asp:Label ID="lblSomethingElse" /&gt;\n    &lt;/asp:View&gt;\n&lt;/asp:Multiview&gt;\n</code></pre>\n'}, {'answer_id': 78143, 'author': 'Mark Brackett', 'author_id': 2199, 'author_profile': 'https://Stackoverflow.com/users/2199', 'pm_score': 0, 'selected': False, 'text': '<p>How about creating your own library of controls, that render differently if ReadOnly is true. Something like:</p>\n\n<pre><code>MyTextBox : TextBox {\n   public override void RenderControl(HtmlTextWriter writer) {\n      if (this.ReadOnly) {\n          writer.WriteBeginTag("label");\n          writer.Write(this.Value);\n          writer.WriteEndTag();\n      }\n   }\n}\n</code></pre>\n\n<p>There\'s a way to use web.config to replace all asp:TextBox instances with your own control without having to edit to my:TextBox - but I\'m having trouble finding the reference ATM.</p>\n\n<p>Otherwise, I\'d probably just write a jQuery snippet to do it.</p>\n'}, {'answer_id': 78145, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>I don't know of any existing function, but it's not that hard to process the controls yourself.  The big thing you'll need to worry about is non ASP.NET controls in the control tree. (eg. )</p>\n\n<p>You can case controls to the appropriate type and just check for null, and then deal with each control correctly.  </p>\n"}, {'answer_id': 78197, 'author': 'Tom Kidd', 'author_id': 2577, 'author_profile': 'https://Stackoverflow.com/users/2577', 'pm_score': 0, 'selected': False, 'text': "<p>Short of authoring your own controls, I'm afraid that the route you don't want to go (making a second, label-only page) is probably a pretty good route simply because you don't want someone running Firebug who can edit HTML on the fly to just turn off whatever control you have in place and just use the page as if they had the right to update it.</p>\n"}, {'answer_id': 216684, 'author': 'tvanfosson', 'author_id': 12950, 'author_profile': 'https://Stackoverflow.com/users/12950', 'pm_score': 0, 'selected': False, 'text': '<p>Use a DetailsView.  It does exactly what you want based on the current mode of the page.</p>\n'}, {'answer_id': 217738, 'author': 'Keith Patton', 'author_id': 25255, 'author_profile': 'https://Stackoverflow.com/users/25255', 'pm_score': 2, 'selected': False, 'text': '<p>Scott Mitchell posted a good article on this a while back. </p>\n\n<p><a href="https://web.archive.org/web/20210608183803/http://aspnet.4guysfromrolla.com/articles/012506-1.aspx" rel="nofollow noreferrer">https://web.archive.org/web/20210608183803/http://aspnet.4guysfromrolla.com/articles/012506-1.aspx</a></p>\n\n<p>I\'ve used this approach in the past in conjection with css on the \'read only\' fields to make them look and work exactly like a label, even though they are in fact text boxes.</p>\n'}]
['https://Stackoverflow.com/questions/77900', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8678/']