Ask a Question related to ASP.NET, Design and Development.

  1. #1

    Default Readonly Checkbox?

    Well, I'm sure you guys know that the ASP.NET Checkbox
    control doesn't have a Readonly property. I'm writing an
    application where I need it to have, that is, I want to
    be able to set the Checked property in code, but if the
    user clicks on it, the checkbox won't change.

    Now, I'll admit I truly don't know how to do this. I can
    easily write a control where I can't set the Checked
    property in code, as follows:

    '--------- Begin Code --------------
    <DefaultProperty("Checked"), ToolboxData("<{0}:CB
    runat=server></{0}:CB>")> Public Class CB
    Inherits System.Web.UI.WebControls.CheckBox

    Dim _readonly As Boolean

    <Bindable(True), Category("Appearance"), DefaultValue
    (False)> Overrides Property [Checked]() As Boolean
    Get
    Return MyBase.Checked
    End Get

    Set(ByVal Value As Boolean)
    If Not _readonly Then
    MyBase.Checked = Value
    End If
    End Set
    End Property

    <Bindable(True), Category("Appearance"), DefaultValue
    (False)> Property [Readonly]() As Boolean
    Get
    Return _readonly
    End Get

    Set(ByVal Value As Boolean)
    _readonly = Value
    End Set
    End Property
    End Class
    '--------- End Code --------------

    With that control, I can't set the Checked property in
    code, but if I view a form with that control in my
    browser and click on the checkbox, it checks or unchecks
    as usual, which is exactly the opposite of the behavior
    I'd like it to have.

    Now I can't figure out how to get that functionality, or
    indeed how to handle the event of the user clicking on
    the control at all without posting the form back to the
    server. I played with overriding the various Render
    methods, but that got me nowhere.

    The basic problem is that I have no clue how the control
    is drawn as checked or unchecked when the user clicks it
    on his/her browser. Apparently there's no postback (as I
    would assume in any case), so there must be some code
    that runs on the client side to handle it. Can I alter
    that code? How do I get to it? Keep in mind that I want
    this to be a server control, not a client control. I
    really don't even need to mess with the Checked property
    of the control -- if I could just prevent it from drawing
    the checked/unchecked state when it's clicked, I will
    have accomplished what I want, since my goal is to have a
    control in which I can display boolean data without the
    user having the ability to alter the visual state of the
    control.

    Anyone have any ideas? Thanks in advance.
    John Kievlan Guest

  2. Similar Questions and Discussions

    1. Readonly Field
      Dreamwever places this code in a readonly field readonly="readonly" but then The readonly attribute of the INPUT tag is not supported. ...
    2. Readonly listbox
      I'm developing a set of WebControls that can be toggled between Readwrite and Readonly modes. They are each inherited from their standard webcontrol...
    3. Collection Readonly
      I have a Web service with a serializable collection class. My test app can read the collection without any problems. The problem I'm having is...
    4. Howto bind CheckBox to the datagrid/ Then update the database field when the checkbox is clicked.
      I am trying to update the database field when the checkbox is clicked. I am trying to modified the following solution but.. got stuck on the...
    5. ReadOnly checkbox in datagrid
      Hi Group, I have a checkbox column in a web datagrid which is related to a "Product in stock" property. The grid is in view mode, so the user...
  3. #2

    Default Re: Readonly Checkbox?

    Either set Enabled=False (it'll be dim) or... in the render...

    Attributes.Add("onClick", "this.checked=" + lcase(checked.tostring) + ";")

    So that if it's enabled or disabled and you don't want it dim, it'll alsways
    be the value that it started with.


    That should do,
    Thanks,
    Shawn


    "John Kievlan" <jodakim@comcast.com> wrote in message
    news:03ff01c35617$b527d930$a501280a@phx.gbl...
    > Well, I'm sure you guys know that the ASP.NET Checkbox
    > control doesn't have a Readonly property. I'm writing an
    > application where I need it to have, that is, I want to
    > be able to set the Checked property in code, but if the
    > user clicks on it, the checkbox won't change.
    >
    > Now, I'll admit I truly don't know how to do this. I can
    > easily write a control where I can't set the Checked
    > property in code, as follows:
    >
    > '--------- Begin Code --------------
    > <DefaultProperty("Checked"), ToolboxData("<{0}:CB
    > runat=server></{0}:CB>")> Public Class CB
    > Inherits System.Web.UI.WebControls.CheckBox
    >
    > Dim _readonly As Boolean
    >
    > <Bindable(True), Category("Appearance"), DefaultValue
    > (False)> Overrides Property [Checked]() As Boolean
    > Get
    > Return MyBase.Checked
    > End Get
    >
    > Set(ByVal Value As Boolean)
    > If Not _readonly Then
    > MyBase.Checked = Value
    > End If
    > End Set
    > End Property
    >
    > <Bindable(True), Category("Appearance"), DefaultValue
    > (False)> Property [Readonly]() As Boolean
    > Get
    > Return _readonly
    > End Get
    >
    > Set(ByVal Value As Boolean)
    > _readonly = Value
    > End Set
    > End Property
    > End Class
    > '--------- End Code --------------
    >
    > With that control, I can't set the Checked property in
    > code, but if I view a form with that control in my
    > browser and click on the checkbox, it checks or unchecks
    > as usual, which is exactly the opposite of the behavior
    > I'd like it to have.
    >
    > Now I can't figure out how to get that functionality, or
    > indeed how to handle the event of the user clicking on
    > the control at all without posting the form back to the
    > server. I played with overriding the various Render
    > methods, but that got me nowhere.
    >
    > The basic problem is that I have no clue how the control
    > is drawn as checked or unchecked when the user clicks it
    > on his/her browser. Apparently there's no postback (as I
    > would assume in any case), so there must be some code
    > that runs on the client side to handle it. Can I alter
    > that code? How do I get to it? Keep in mind that I want
    > this to be a server control, not a client control. I
    > really don't even need to mess with the Checked property
    > of the control -- if I could just prevent it from drawing
    > the checked/unchecked state when it's clicked, I will
    > have accomplished what I want, since my goal is to have a
    > control in which I can display boolean data without the
    > user having the ability to alter the visual state of the
    > control.
    >
    > Anyone have any ideas? Thanks in advance.

    Shawn B. Guest

  4. #3

    Default Re: Readonly Checkbox?

    >-----Original Message-----
    >Either set Enabled=False (it'll be dim) or... in the
    render...

    Yes, I know about that, that's what I've been doing, but
    I don't like having it grayed out... it doesn't look
    quite so neat... and organized... as I'd like it to :P
    >Attributes.Add("onClick", "this.checked=" + lcase
    (checked.tostring) + ";")
    >
    >So that if it's enabled or disabled and you don't want
    it dim, it'll alsways
    >be the value that it started with.
    Thanks, friend! That did it! It works perfectly. My
    code is now as follows:

    Protected Overrides Sub Render(ByVal writer As
    System.Web.UI.HtmlTextWriter)
    If _readonly Then
    MyBase.Attributes.Add
    ("onClick", "javascript:" & Me.ID & ".checked=" + LCase
    (Checked.ToString) + ";")
    End If
    MyBase.Render(writer)
    End Sub

    That adds a SPAN element around the checkbox that
    contains the onClick event. I added the "javascript:"
    because I use javascript and vbscript both on this
    particular form, and the default page language is
    vbscript. "this.checked" doesn't work since it thinks
    you're talking about the SPAN element, so I changed it to
    use the ID of the control -- and it works great. A
    readonly control can only be changed from code, a non-
    readonly control can be changed both in code and by being
    clicked.

    Again, thanks.

    --John
    John Kievlan Guest

  5. #4

    Default Re: Readonly Checkbox?

    You might also try

    addattribute("onClick", "return false")


    Thanks,
    Shawn

    "John Kievlan" <jodakim@comcast.com> wrote in message
    news:0bd301c356a8$20002240$a601280a@phx.gbl...
    >
    > >-----Original Message-----
    > >Either set Enabled=False (it'll be dim) or... in the
    > render...
    >
    > Yes, I know about that, that's what I've been doing, but
    > I don't like having it grayed out... it doesn't look
    > quite so neat... and organized... as I'd like it to :P
    >
    > >Attributes.Add("onClick", "this.checked=" + lcase
    > (checked.tostring) + ";")
    > >
    > >So that if it's enabled or disabled and you don't want
    > it dim, it'll alsways
    > >be the value that it started with.
    >
    > Thanks, friend! That did it! It works perfectly. My
    > code is now as follows:
    >
    > Protected Overrides Sub Render(ByVal writer As
    > System.Web.UI.HtmlTextWriter)
    > If _readonly Then
    > MyBase.Attributes.Add
    > ("onClick", "javascript:" & Me.ID & ".checked=" + LCase
    > (Checked.ToString) + ";")
    > End If
    > MyBase.Render(writer)
    > End Sub
    >
    > That adds a SPAN element around the checkbox that
    > contains the onClick event. I added the "javascript:"
    > because I use javascript and vbscript both on this
    > particular form, and the default page language is
    > vbscript. "this.checked" doesn't work since it thinks
    > you're talking about the SPAN element, so I changed it to
    > use the ID of the control -- and it works great. A
    > readonly control can only be changed from code, a non-
    > readonly control can be changed both in code and by being
    > clicked.
    >
    > Again, thanks.
    >
    > --John

    Shawn B. Guest

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139