Groups | Blog | Home
all groups > dotnet faqs > september 2003 >

dotnet faqs : Lock fields


Harshil Patel
9/24/2003 10:14:00 AM
Hi

I am trying to lock fields on my form (VB.NET), so that they cannot be
edited by the user. So far I have accomplished my goal for the text box
by setting its read only property. But what should I do for combo box
and mask edit box (added as an reference). list box etc. I don't want to
use the enabled property, since that makes the controls text to fade or
not seen prominently.

Does anyone know how can we achieve this in VB.NET ??

Thanks




*** Sent via Developersdex http://www.developersdex.com ***
H. Scott Buckwalter
9/25/2003 10:09:28 AM
Here's how I do this:

1) I place two controls next to each other. One is the editing control
(text box, drop list, etc.) and the other is a label.
2) When I load the form, I load to values to both controls.
3) If editing, I hide all label controls. If read only, I hide all editing
controls.

This works for all controls except check boxes. They I leave alone and add
client script to make them read only...
chkbox.Attributes.Add("onClick", "this.checked=true;") (or false if
unchecked.)

I actually take this a step further with naming conventions:
I prefix all controls with 3 characters: txt=text box, cbo=list box,
chk=check box, lbl=label, etc.
Then in the the Page_PreRender() function I loop through all controls as
such:

Dim Ctrl As System.Web.UI.Control
For Each Ctrl In frmEdit.Controls
If Not Ctrl.ID Is Nothing Then
Select Case Ctrl.ID.Substring(0, 3)
Case "txt", "cbo"
Ctrl.Visible = bEditable
Case "lbl"
Ctrl.Visible = Not bEditable
End Select
End If
Next Ctrl

I handle the check boxes elsewhere but adding a Case "chk" would work here
also.
You could also look through checking the type of the object (instead of the
ID) giving you more freedom to name your controls but that would cause
problems if you wanted some controls to remain visible reguardless of the
bEditable setting.

H. Scott Buckwalter

[quoted text, click to view]

AddThis Social Bookmark Button