Composite Custom Control - Textbox Readonly Bug

Discussion in 'ASP .Net Building Controls' started by Nick Stansbury, Mar 20, 2006.

  1. 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
     
    Nick Stansbury, Mar 20, 2006
    #1
    1. Advertising

  2. Nick Stansbury

    Teemu Keiski Guest

    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.

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke


    "Nick Stansbury" <> wrote in message
    news:...
    > 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
    >
     
    Teemu Keiski, Mar 20, 2006
    #2
    1. Advertising

  3. Nick Stansbury

    Teemu Keiski Guest

    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.



    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke

    "Nick Stansbury" <> wrote in message
    news:...
    > 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
    >
     
    Teemu Keiski, Mar 20, 2006
    #3
  4. So basically the answer to my question is
    "read the damn documentation you f****ing idiot"

    Thanks for the patient answer anyway...

    Nick

    "Teemu Keiski" <> wrote in message
    news:...
    > 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.
    >
    >
    >
    > --
    > Teemu Keiski
    > ASP.NET MVP, AspInsider
    > Finland, EU
    > http://blogs.aspadvice.com/joteke
    >
    > "Nick Stansbury" <> wrote in message
    > news:...
    >> 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
    >>

    >
    >
     
    Nick Stansbury, Mar 20, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. S. Justin Gengo
    Replies:
    0
    Views:
    564
    S. Justin Gengo
    Jul 16, 2003
  2. Chad
    Replies:
    0
    Views:
    243
  3. Custom Composite Control inherited from TextBox

    , Oct 24, 2006, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    140
    John Saunders
    Oct 24, 2006
  4. Jonathan Hyatt
    Replies:
    1
    Views:
    2,092
    Jonathan Hyatt
    Jun 8, 2004
  5. ErwinP
    Replies:
    1
    Views:
    789
    ErwinP
    Aug 19, 2005
Loading...

Share This Page