Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 753
  • Last Modified:

ASP.net hide RadioButtonList items

Hi

I am using the following code to disable RadioButtonList items. Is it possible to hide them instead?


  Private Sub GridView3_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView3.RowDataBound
        Try
            ' Required to ignore the header and footer rows
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim oNumberAnswers As String = CStr(e.Row.Cells(4).Text)

                Dim rdb As RadioButtonList = DirectCast(e.Row.FindControl("RadioButtonList1"), RadioButtonList)

                If oNumberAnswers = "2" Then
                    rdb.Items(2).Enabled = False
                    rdb.Items(3).Enabled = False
                    rdb.Items(4).Enabled = False
                ElseIf oNumberAnswers = "3" Then
                    rdb.Items(3).Enabled = False
                    rdb.Items(4).Enabled = False
                ElseIf oNumberAnswers = "4" Then
                    rdb.Items(4).Enabled = False

                End If
            End If
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub
0
Murray Brown
Asked:
Murray Brown
  • 2
  • 2
1 Solution
 
Jesus RodriguezIT ManagerCommented:
Under the hood, you can access the attributes of the item and assign it a CSS style.
So you should be able to then programmatically assign it by specifying:

RadioButtonList.Items(1).CssClass.Add("visibility", "hidden")
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi thanks Jesus. Where would I put this code? Thanks. Murray
0
 
Jesus RodriguezIT ManagerCommented:
In your RowDatabound event you can do something like this

Private Sub GridView3_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView3.RowDataBound
         Try
             ' Required to ignore the header and footer rows
             If e.Row.RowType = DataControlRowType.DataRow Then
                 Dim oNumberAnswers As String = CStr(e.Row.Cells(4).Text)

                 Dim rdb As RadioButtonList = DirectCast(e.Row.FindControl("RadioButtonList1"), RadioButtonList)
  
                 If oNumberAnswers = "2" Then
                     rdb.Items.Remove(rdb.Items.FindByValue(rdb.items(2).value))
                     rdb.Items.Remove(rdb.Items.FindByValue(rdb.items(3).value))
                     rdb.Items.Remove(rdb.Items.FindByValue(rdb.items(4).value))
                 ElseIf oNumberAnswers = "3" Then
                     rdb.Items.Remove(rdb.Items.FindByValue(rdb.items(3).value))
                     rdb.Items.Remove(rdb.Items.FindByValue(rdb.items(4).value))
                 ElseIf oNumberAnswers = "4" Then
                     rdb.Items.Remove(rdb.Items.FindByValue(rdb.items(4).value))
                 End If
             End If
         Catch ex As Exception
             Response.Write(ex.Message)
         End Try
     End Sub 

Open in new window

               
        if you use  RadioButtonList1.Items(Element_Index).Attributes.Add("style", "visibility:hidden") then you will have an space between each element that will be hidden
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now