Composite Custom Control - Textbox Readonly Bug

N

Nick Stansbury

Hi,

ASP.Net 2 - Strange bug / problem observed with the use of the readonly
property in composite custom controls. Using the class shell defined below,
the issue is with the readonly property. If you set the inner text box's
"ReadOnly" property to be true, and then use javascript (in this case a
pop-up window) to pass back a value into the text box, then the values in
the text box is not persisted accross postbacks. Very bizarre. Not the case
under the old version of Asp.net. However replace Readonly = true with
_textbox.Attributes.Add("ReadOnly", "ReadOnly") then it works fine. Anyone
got any ideas why?

Nick

Here's the class shell (I wrote this off-the-cuff here so I hope all the
code is right - the important thing is the principle!) :

Public Class MyCustomControl : inherits WebControl : implements
iNamingContainer
Private _Textbox as textBox
private Image as Image

Protected Overrides Sub CreateChildControls()
_TextBox = new TextBox
_TextBox.ID = "txtInnerTextBox"
Me.Controls.add(_TextBox)
_Image = new Image
_Image.ID = "imgInnerImage"
Me.Controls.Add(_Image)

_TextBox.Readonly = true
_Image.Attributes.Add("onclick", "Javascript:document.getElementById('"
& Me._TextBox.ClientID & "').value='Hullo');")
End Sub
End Class
 
T

Teemu Keiski

TextBox won't load postback data if its server-side ReadOnly property is
marked "true". Basically it means reads-only from server-side stand point
e.g client browser cannot change the value. If you switch to use client-side
readonly-attribute that won't effect server-side functionality.
 
T

Teemu Keiski

To add to my previous post, this behavior is described in MSDN docs:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.readonly.aspx

Important:
The Text value of a TextBox control with the ReadOnly property set to
true is sent to the server when a postback occurs, but the server does no
processing for a read-only text box. This prevents a malicious user from
changing a Text value that is read-only. The value of the Text property is
preserved in the view state between postbacks unless modified by server-side
code.
 
N

Nick Stansbury

So basically the answer to my question is
"read the damn documentation you f****ing idiot"

Thanks for the patient answer anyway...

Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top