GridView checkbox as ReadOnly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RobertTheProgrammer
    New Member
    • Aug 2007
    • 58

    GridView checkbox as ReadOnly

    Hi folks,

    I'm using ASP.NET with C# code behind. I've got a GridView on my page that has several checkbox fields that are bound to database fields. I want the checkboxes to be visible and editable during normal "EditItemTempla te". However during "ItemTempla te" display, I want it to be ReadOnly. In other words, unless the users are in edit mode, they cannot toggle the checkbox. Apparently checkboxes don't have a ReadOnly option. I've tried setting "Enabled" to false and it works, although it's too grayed-out for my tasted and I'd rather just set it to read only.

    Any idea how to do this?

    Robert
  • RobertTheProgrammer
    New Member
    • Aug 2007
    • 58

    #2
    I've got the workaround working... I use the OnCheckedChange d event to call code behind that resets the checkbox to it's original value. It works, but it's cumbersome because of all the overhead of reloading the page.

    If anyone has any better ideas...

    Robert

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Originally posted by RobertTheProgra mmer
      I've got the workaround working... I use the OnCheckedChange d event to call code behind that resets the checkbox to it's original value. It works, but it's cumbersome because of all the overhead of reloading the page.

      If anyone has any better ideas...

      Robert
      You could set the checkbox's Enabled value to False...
      or you could use JavaScript to do this....

      Again you would do this in your RowDataBound event for your GridView...
      [code=vbnet]
      Private Sub MyGridView_RowD ataBound(ByVal sender As Object, ByVal e As System.Web.UI.W ebControls.Grid ViewRowEventArg s) Handles GV_MyGridView.R owDataBound
      Dim cb As CheckBox
      If e.Row.RowType = DataControlRowT ype.DataRow Then
      cb = CType(e.Row.Fin dControl("MyChe ckboxName"), CheckBox)
      If Not cb Is Nothing Then
      cb.Enabled = False
      'cb.Attributes. Add("onclick"," JavaScript:docu ment.getElement ById('" + cbl.ClientID + "').checked=fal se;")
      End If
      End If
      End Sub[/code]


      Cheers!

      -Frinny

      Comment

      • RobertTheProgrammer
        New Member
        • Aug 2007
        • 58

        #4
        I've tried the Enabled=false attribute and it works well, but the checkbox is too grayed out. My background is gray and it makes the checkbox almost vanish. I'd much rather have a ReadOnly type of option so that clicking on the checkbox would simply do nothing, but it would look relatively normal.

        Robert

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Originally posted by RobertTheProgra mmer
          I've tried the Enabled=false attribute and it works well, but the checkbox is too grayed out. My background is gray and it makes the checkbox almost vanish. I'd much rather have a ReadOnly type of option so that clicking on the checkbox would simply do nothing, but it would look relatively normal.

          Robert
          There is no ReadOnly property on checkboxes...I' d suggest using the JavaScript solution then.

          -Frinny

          Comment

          • DrBunchman
            Recognized Expert Contributor
            • Jan 2008
            • 979

            #6
            Another option would be to use an image that looked like a checkbox which you could display when you wanted it to be 'Read Only' rather than the checkbox itself.

            Dr B

            Comment

            • RobertTheProgrammer
              New Member
              • Aug 2007
              • 58

              #7
              Ah! The checkbox image is brilliant! I'll do that. Thanks!

              Robert

              Comment

              Working...