Link to home
Start Free TrialLog in
Avatar of kahvedzic
kahvedzicFlag for Bosnia and Herzegovina

asked on

Read Only property for RadioButton List in FormView

Hi,
in formview readonly mode I have radiobutton list with two items. Also on same form in read only mode I have several text boxes with read only property set to true (they are visible but not editable). I want to make radiobutton list items look same like textboxes, visible, read only and not editable. Tried with enable= false property but it turns radio buttons and text in grey color and hardly visible. Is there a way to leave Radio Button list items visible but not clickable (editable) on read only formview mode. Thanks for help
Avatar of krunal_shah
krunal_shah

Avatar of kahvedzic

ASKER

Read Only is default mode for my FormView, and on page load event I did this:

Dim radiofv As RadioButtonList

        If FormView1.CurrentMode = FormViewMode.ReadOnly Then
            radiofv = DirectCast(FormView1.FindControl("RadioButtonList2"), RadioButtonList)
            radiofv.Attributes("Onclick") = "return false;"
        End If

Then got error: Object reference not set to an instance of an object.
On this line: radiofv.Attributes("Onclick") = "return false;"
And radiofv is Nothing. How to point to this control.

I also have some code behind for insert and update modes on page load and it is working with direct cast but not with read only.
Is the RadioButtonList2 in the itemtemplate ? only then it will be able to find the control in the READ-ONLY mode ... Also if your RadioButtonList2  is inside itemtemplate and still your code returns the same error write the same code in the Pre_Render event of the oage ..
RadioButtonList2 is in the itemtemplate of FormView1, I move code from page load to Page_PreRender and have same error again (Object reference not set to an instance of an object.). This is code behind

Imports System.Data.SqlClient

Partial Class Default2
    Inherits System.Web.UI.Page

    Dim uslov As String
    Dim kb As New kBazica

    Private Function CreateDataTable() As Data.DataTable
        Try
            Dim myDataTable As Data.DataTable = New Data.DataTable()
            Dim myDataColumn As Data.DataColumn

            myDataColumn = New Data.DataColumn()
            myDataColumn.DataType = Type.GetType("System.String")
            myDataColumn.ColumnName = "Name"
            myDataTable.Columns.Add(myDataColumn)


            Return myDataTable
        Catch ex As Exception

        End Try

    End Function

    Private Function AddDataToTable(ByVal Name As String, ByVal myTable As Data.DataTable)

        Try
            Dim row As Data.DataRow = myTable.NewRow()
            row("Name") = Name

            myTable.Rows.Add(row)
            myTable.AcceptChanges()
        Catch ex As Exception

        End Try
    End Function

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        FormView1.ChangeMode(FormViewMode.Insert)
        GridView1.SelectedIndex = -1

        Name_SearchBox.Text = ""
        Asset_No_SearchBox.Text = ""
        Location_SearchBox.Text = ""
        OS_SearchBox.Text = ""
        Serial_SearchBox.Text = ""
        Label1.Text = ""
        BrojLabel.Text = GridView1.Rows.Count
    End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
        FormView1.ChangeMode(FormViewMode.ReadOnly)
        GridView1.SelectedRow.Focus()

        
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.GridView1.Attributes.Add("style", "table-layout:dynamic")

        Dim fizvi As RadioButtonList
        Dim ilotext As TextBox
        Dim asset As TextBox
        Dim rack As TextBox
        Dim model As TextBox
        Dim ctl As TextBox
        Dim lvl As TextBox
        Dim type As TextBox
        Dim storage As TextBox
        Dim raidless As TextBox
        Dim serial As TextBox
        Dim part As TextBox
        Dim garancija As TextBox
        Dim contact As TextBox

        If FormView1.CurrentMode = FormViewMode.Insert Then
            fizvi = DirectCast(FormView1.FindControl("RadioButtonList1"), RadioButtonList)
            ilotext = DirectCast(FormView1.FindControl("ILO_AddressTextBox"), TextBox)
            asset = DirectCast(FormView1.FindControl("Asset_NumberTextBox"), TextBox)
            rack = DirectCast(FormView1.FindControl("Rack_NumberTextBox"), TextBox)
            model = DirectCast(FormView1.FindControl("HW_ModelTextBox"), TextBox)
            ctl = DirectCast(FormView1.FindControl("HW_Array_ctlTextBox"), TextBox)
            lvl = DirectCast(FormView1.FindControl("HW_RAID_LevelTextBox"), TextBox)
            type = DirectCast(FormView1.FindControl("HW_HDD_typeTextBox"), TextBox)
            storage = DirectCast(FormView1.FindControl("HW_StorageTextBox"), TextBox)
            raidless = DirectCast(FormView1.FindControl("HW_RAID_LESTextBox"), TextBox)
            serial = DirectCast(FormView1.FindControl("TextBox4"), TextBox)
            part = DirectCast(FormView1.FindControl("Part_NumberTextBox"), TextBox)
            garancija = DirectCast(FormView1.FindControl("Warranty_DateTextBox"), TextBox)
            contact = DirectCast(FormView1.FindControl("Support_Contact_NameTextBox"), TextBox)

            If fizvi.SelectedValue = "V" Then
                ilotext.Enabled = False
                ilotext.BackColor = System.Drawing.SystemColors.InactiveBorder
                asset.Enabled = False
                asset.BackColor = System.Drawing.SystemColors.InactiveBorder
                rack.Enabled = False
                rack.BackColor = System.Drawing.SystemColors.InactiveBorder
                model.Enabled = False
                model.BackColor = System.Drawing.SystemColors.InactiveBorder
                ctl.Enabled = False
                ctl.BackColor = System.Drawing.SystemColors.InactiveBorder
                lvl.Enabled = False
                lvl.BackColor = System.Drawing.SystemColors.InactiveBorder
                type.Enabled = False
                type.BackColor = System.Drawing.SystemColors.InactiveBorder
                storage.Enabled = False
                storage.BackColor = System.Drawing.SystemColors.InactiveBorder
                raidless.Enabled = False
                raidless.BackColor = System.Drawing.SystemColors.InactiveBorder
                serial.Enabled = False
                serial.BackColor = System.Drawing.SystemColors.InactiveBorder
                part.Enabled = False
                part.BackColor = System.Drawing.SystemColors.InactiveBorder
                garancija.Enabled = False
                garancija.BackColor = System.Drawing.SystemColors.InactiveBorder
                contact.Enabled = False
                contact.BackColor = System.Drawing.SystemColors.InactiveBorder
            Else
                ilotext.Enabled = True
                ilotext.BackColor = Drawing.Color.White
                asset.Enabled = True
                asset.BackColor = Drawing.Color.White
                rack.Enabled = True
                rack.BackColor = Drawing.Color.White
                model.Enabled = True
                model.BackColor = Drawing.Color.White
                ctl.Enabled = True
                ctl.BackColor = Drawing.Color.White
                lvl.Enabled = True
                lvl.BackColor = Drawing.Color.White
                type.Enabled = True
                type.BackColor = Drawing.Color.White
                storage.Enabled = True
                storage.BackColor = Drawing.Color.White
                raidless.Enabled = True
                raidless.BackColor = Drawing.Color.White
                serial.Enabled = True
                serial.BackColor = Drawing.Color.White
                part.Enabled = True
                part.BackColor = Drawing.Color.White
                garancija.Enabled = True
                garancija.BackColor = Drawing.Color.White
                contact.Enabled = True
                contact.BackColor = Drawing.Color.White
            End If

        End If

        If FormView1.CurrentMode = FormViewMode.Edit Then
            fizvi = DirectCast(FormView1.FindControl("RadioButtonList3"), RadioButtonList)
            ilotext = DirectCast(FormView1.FindControl("ILO_AddressTextBox"), TextBox)
            asset = DirectCast(FormView1.FindControl("Asset_NumberTextBox"), TextBox)
            rack = DirectCast(FormView1.FindControl("Rack_NumberTextBox"), TextBox)
            model = DirectCast(FormView1.FindControl("HW_ModelTextBox"), TextBox)
            ctl = DirectCast(FormView1.FindControl("HW_Array_ctlTextBox"), TextBox)
            lvl = DirectCast(FormView1.FindControl("HW_RAID_LevelTextBox"), TextBox)
            type = DirectCast(FormView1.FindControl("HW_HDD_typeTextBox"), TextBox)
            storage = DirectCast(FormView1.FindControl("HW_StorageTextBox"), TextBox)
            raidless = DirectCast(FormView1.FindControl("HW_RAID_LESTextBox"), TextBox)
            serial = DirectCast(FormView1.FindControl("TextBox3"), TextBox)
            part = DirectCast(FormView1.FindControl("Part_NumberTextBox"), TextBox)
            garancija = DirectCast(FormView1.FindControl("Warranty_DateTextBox"), TextBox)
            contact = DirectCast(FormView1.FindControl("Support_Contact_NameTextBox"), TextBox)

            If fizvi.SelectedValue = "V" Then
                ilotext.Enabled = False
                ilotext.BackColor = System.Drawing.SystemColors.InactiveBorder
                asset.Enabled = False
                asset.BackColor = System.Drawing.SystemColors.InactiveBorder
                rack.Enabled = False
                rack.BackColor = System.Drawing.SystemColors.InactiveBorder
                model.Enabled = False
                model.BackColor = System.Drawing.SystemColors.InactiveBorder
                ctl.Enabled = False
                ctl.BackColor = System.Drawing.SystemColors.InactiveBorder
                lvl.Enabled = False
                lvl.BackColor = System.Drawing.SystemColors.InactiveBorder
                type.Enabled = False
                type.BackColor = System.Drawing.SystemColors.InactiveBorder
                storage.Enabled = False
                storage.BackColor = System.Drawing.SystemColors.InactiveBorder
                raidless.Enabled = False
                raidless.BackColor = System.Drawing.SystemColors.InactiveBorder
                serial.Enabled = False
                serial.BackColor = System.Drawing.SystemColors.InactiveBorder
                part.Enabled = False
                part.BackColor = System.Drawing.SystemColors.InactiveBorder
                garancija.Enabled = False
                garancija.BackColor = System.Drawing.SystemColors.InactiveBorder
                contact.Enabled = False
                contact.BackColor = System.Drawing.SystemColors.InactiveBorder
            Else
                ilotext.Enabled = True
                ilotext.BackColor = Drawing.Color.White
                asset.Enabled = True
                asset.BackColor = Drawing.Color.White
                rack.Enabled = True
                rack.BackColor = Drawing.Color.White
                model.Enabled = True
                model.BackColor = Drawing.Color.White
                ctl.Enabled = True
                ctl.BackColor = Drawing.Color.White
                lvl.Enabled = True
                lvl.BackColor = Drawing.Color.White
                type.Enabled = True
                type.BackColor = Drawing.Color.White
                storage.Enabled = True
                storage.BackColor = Drawing.Color.White
                raidless.Enabled = True
                raidless.BackColor = Drawing.Color.White
                serial.Enabled = True
                serial.BackColor = Drawing.Color.White
                part.Enabled = True
                part.BackColor = Drawing.Color.White
                garancija.Enabled = True
                garancija.BackColor = Drawing.Color.White
                contact.Enabled = True
                contact.BackColor = Drawing.Color.White
            End If

        End If

        BrojLabel.Text = GridView1.Rows.Count

    End Sub

    Protected Sub TraziButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TraziButton.Click
        Dim Name As String
        Dim ds As New Data.DataSet
        GridView1.Dispose()
        ds.Dispose()
        Formiraj_Uslov()
        If uslov <> "" Then
            Label1.Text = ""
            ds = kb.SQLpuni("SELECT Name from Servers with (nolock) where (" + uslov + ") order by Name;", ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

            If ds.Tables(0).Rows.Count > 0 Then
                For Each dr As Data.DataRow In ds.Tables(0).Rows
                    Name = dr.Item("Name")
                Next
            Else
                Label1.Text = "Nisu pronadjeni rezultati koji zadovoljavaju zadane kriterije."
                Label1.Visible = True
            End If

            Me.GridView1.DataSource = ds
            Me.GridView1.AutoGenerateColumns = True
            Me.GridView1.DataBind()
        Else
            Label1.Text = "Unesite kriterije za pretragu."
            Label1.Visible = True
        End If
        GridView1.SelectedIndex = -1
        BrojLabel.Text = GridView1.Rows.Count
    End Sub

    Private Sub Formiraj_Uslov()
        If Name_SearchBox.Text <> "" Then
            uslov = "Name like '%'+ '" + Name_SearchBox.Text + "'+'%'"
        Else
            uslov = ""
        End If

        If Asset_No_SearchBox.Text <> "" Then
            If uslov = "" Then
                uslov = "Asset_Number like '%'+ '" + Asset_No_SearchBox.Text + "'+'%'"
            Else
                uslov = uslov + " and Asset_Number like '%'+ '" + Asset_No_SearchBox.Text + "'+'%'"
            End If
        End If

        If Location_SearchBox.Text <> "" Then
            If uslov = "" Then
                uslov = "Location like '%'+ '" + Location_SearchBox.Text + "'+'%'"
            Else
                uslov = uslov + " and Location like '%'+ '" + Location_SearchBox.Text + "'+'%'"
            End If
        End If

        If OS_SearchBox.Text <> "" Then
            If uslov = "" Then
                uslov = "OS like '%'+ '" + OS_SearchBox.Text + "'+'%'"
            Else
                uslov = uslov + " and OS like '%'+ '" + OS_SearchBox.Text + "'+'%'"
            End If
        End If

        If Serial_SearchBox.Text <> "" Then
            If uslov = "" Then
                uslov = "Serial_Number like '%'+ '" + Serial_SearchBox.Text + "'+'%'"
            Else
                uslov = uslov + " and Serial_Number like '%'+ '" + Serial_SearchBox.Text + "'+'%'"
            End If
        End If
    End Sub

    Protected Sub AdvancedButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AdvancedButton.Click
        If Asset_No_Label.Visible = False And Location_Label.Visible = False And OS_Label.Visible = False And Serial_Label.Visible = False And Asset_No_SearchBox.Visible = False And Location_SearchBox.Visible = False And OS_SearchBox.Visible = False And Serial_SearchBox.Visible = False Then
            Asset_No_Label.Visible = True
            Location_Label.Visible = True
            OS_Label.Visible = True
            Serial_Label.Visible = True
            Asset_No_SearchBox.Visible = True
            Location_SearchBox.Visible = True
            OS_SearchBox.Visible = True
            Serial_SearchBox.Visible = True
        Else
            Asset_No_Label.Visible = False
            Location_Label.Visible = False
            OS_Label.Visible = False
            Serial_Label.Visible = False
            Asset_No_SearchBox.Visible = False
            Location_SearchBox.Visible = False
            OS_SearchBox.Visible = False
            Serial_SearchBox.Visible = False
        End If

        If Asset_No_Label.Visible = True Then
            AdvancedButton.Text = "Skloni"
            Panel2.Width = 1135
        Else
            AdvancedButton.Text = "Prosirena pretraga"
            Panel2.Width = 350
        End If
        BrojLabel.Text = GridView1.Rows.Count
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Name_SearchBox.Text = ""
        Asset_No_SearchBox.Text = ""
        Location_SearchBox.Text = ""
        OS_SearchBox.Text = ""
        Serial_SearchBox.Text = ""
        Label1.Text = ""
        BrojLabel.Text = GridView1.Rows.Count
    End Sub

    Protected Sub FormView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs) Handles FormView1.ItemInserted
        'GridView1.DataBind()
        'Response.Redirect(Request.RawUrl)
        Dim Name As String
        Dim ds As New Data.DataSet


        ds = kb.SQLpuni("SELECT Name from Servers ORDER BY Name;", ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)


        If ds.Tables(0).Rows.Count > 0 Then
            For Each dr As Data.DataRow In ds.Tables(0).Rows
                Name = dr.Item("Name")
            Next
        End If

        Me.GridView1.DataSource = ds
        Me.GridView1.AutoGenerateColumns = True
        Me.GridView1.DataBind()

        GridView1.SelectedIndex = -1
    End Sub

    Protected Sub FormView1_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdatedEventArgs) Handles FormView1.ItemUpdated
        'GridView1.DataBind()
        'Response.Redirect(Request.RawUrl)
        Dim Name As String
        Dim ds As New Data.DataSet


        ds = kb.SQLpuni("SELECT Name from Servers ORDER BY Name;", ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)


        If ds.Tables(0).Rows.Count > 0 Then
            For Each dr As Data.DataRow In ds.Tables(0).Rows
                Name = dr.Item("Name")
            Next
        End If

        Me.GridView1.DataSource = ds
        Me.GridView1.AutoGenerateColumns = True
        Me.GridView1.DataBind()

        GridView1.SelectedRow.Focus()
    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Name As String
        Dim ds As New Data.DataSet


        ds = kb.SQLpuni("SELECT Name from Servers ORDER BY Name;", ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)


        If ds.Tables(0).Rows.Count > 0 Then
            For Each dr As Data.DataRow In ds.Tables(0).Rows
                Name = dr.Item("Name")
            Next
        End If

        Me.GridView1.DataSource = ds
        Me.GridView1.AutoGenerateColumns = True
        Me.GridView1.DataBind()

        GridView1.SelectedIndex = -1

        Label1.Text = ""
        BrojLabel.Text = GridView1.Rows.Count
    End Sub

    Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        BrojLabel.Text = GridView1.Rows.Count

    End Sub

    Protected Sub InsertButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        BrojLabel.Text = GridView1.Rows.Count
    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Dim radiofv As RadioButtonList

        If FormView1.CurrentMode = FormViewMode.ReadOnly Then
            radiofv = DirectCast(FormView1.FindControl("RadioButtonList2"), RadioButtonList)
            radiofv.Attributes("Onclick") = "return false;"
        End If
    End Sub
End Class

Open in new window

try this ,

radiofv = DirectCast(FormView1.Row.FindControl("RadioButtonList2"), RadioButtonList)
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Dim radiofv As RadioButtonList

        If FormView1.CurrentMode = FormViewMode.ReadOnly Then
            radiofv = DirectCast(FormView1.Row.FindControl("RadioButtonList2"), RadioButtonList)
            radiofv.Attributes("Onclick") = "return false;"
        End If
    End Sub

returns error Object reference not set to an instance of an object. on this line
radiofv =  DirectCast(FormView1.Row.FindControl("RadioButtonList2"),  RadioButtonList)
I was just reviewing your code and did not see any datasource set to the formview and no databind associated with the formview . For finding controls in the formview you will have to set a datasource to the formview and then databind it ...
Just for test I create new formview, bind it to data from sql table and use this code

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim fiz As RadioButtonList

        fiz = DirectCast(FormView1.FindControl("RadioButtonList1"), RadioButtonList)
        fiz.Attributes("Onclick") = "return false;"


    End Sub

Problem is that one radio button is selected and if I click on other none of them is and cant be selected. On page refresh data is bound again and displayed correctly.

This is not what I need. I need to create like read only radio buttons that cant be clicked or changed (like text boxes), but not disabled and grey.
If you are not going to use this radiobuttonlist for entering any data, then

<asp:RadioButtonList ID="someid" runat="server" onclick="javascript:return false;">
--
--
</asp:RadioButtonList>
there is no onclick property for radiobutton list
Visual studio will not show you the onclick property but its a javascript propery which is a cross-browser supported solution ... so just put the onclick and see if its working for your scenario ...
have a warning, but it is working. Only problem is that it is not working as I want to. As I said in earlier post I am able to click on other radiobutton list item and deselect all items of rb list. What I want is to disable clicking on radiobutton list completely, just to have one item selected and cant change it. Is this possible with javascript?
Is it possible  this way to disable only one radio button list item that is not selected (something other than return false)?
ASKER CERTIFIED SOLUTION
Avatar of masterpass
masterpass
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok, I understand what I have to do but I dont know how. Little more help please.

Thanks