Create Dynamic Radio Button List Using XML

Posted on 2006-04-21
Last Modified: 2010-04-23
I'm writing a web user control that contains 3 RadioButtonList web controls in addition to other controls.  

I would like to make this control reusable for different applications so instead of hard coding the radio buttons, I would like to dynamically add them when the form is loaded.  In addition to setting up the radio button item text, I would also like to associate a stored proc name that would be executed when the radio button is selected.  I was thinking that having this data in an external xml file would be the way to go since the xml file could be easily replaced/modified for other uses, and the user control would not need to be recompiled or redeployed.  

I am *really* new at using xml, so I am not sure of the best way to form it for this use.  It seems that each example I have found in my searches are all so different, that I am more confused than when I first started exploring this.  I would like to be able to use the xmlReader to create the radio buttons and gather the proc name.

Please let me know if you can add radio buttons to a RadioButtonList in this fashion, and how to get started on forming the XML.

I'm using Visual Studio 2002 - with VB code behind for the .ascx file.

Example Result:

RadioButtonList1 (can have up to 12 items)
  Map       (calls sp_Map)
  Class     (calls sp_class)
  Owner   (calls sp_owner)
  Street    (calls sp_street)
  Location (calls sp_Location)

RadioButtonList2 (can have up to 8 items)
  Vacant   (calls sp_vacant)


Question by:janmarini
    LVL 7

    Author Comment


    Figured this out ....

    XML looks something like this:

    <?xml version="1.0" encoding="utf-8" ?>
          <!-- START OF: RadioButtonList1 name rbl1 max 12 values -->
                <desc>Land Use Code</desc>
          <!-- END OF: RadioButtonList1 name rbl1 -->
          <!-- START OF: RadioButtonList2 name rbl2 max 8 values -->
                <desc>Vacant Properties</desc>
                <desc>Open Permits</desc>
          <!-- END OF: RadioButtonList2 name rbl2 -->
          <!-- START OF: RadioButtonList3 name rbl3 max 4 values-->
          <!-- END OF: RadioButtonList3 name rbl3 -->

    And Code looks like this:


    'Populate dynamic RadioButtonLists using psRadioButtonsList.xml
            If Not Page.IsPostBack Then
            End If

    Private Sub PopulateLists()
            'Create dynamic RadioButtonLists from XML file
            'Schema column (0)=Value (1)=Desc (2)=Storedd Proc Name
            'Assumes DataSet.Table is created for each ListBox (3)
            'If Table is empty set RadioButtonList.visible = False
            'Dim ds As DataSet = New DataSet()
            Dim t As DataTable
            Dim r As DataRow
            Dim c As DataColumn
            Dim a
            For Each t In ds.Tables
                'a = "TableName: " + t.TableName
                Select Case t.TableName
                    Case "rbl1"
                        For Each r In t.Rows
                            If r(0).ToString() <> "" Then
                                rbl1.Visible = False
                                Exit For
                            End If
                        Next r
                    Case "rbl2"
                        For Each r In t.Rows
                            If r(0).ToString() <> "" Then
                                rbl2.Visible = False
                                Exit For
                            End If
                        Next r
                    Case "rbl3"
                        For Each r In t.Rows
                            If r(0).ToString() <> "" Then
                                rbl3.Visible = False
                                Exit For
                            End If
                        Next r
                End Select
            Next t
        End Sub

    Example to obtain Vlaue and Stored Proc Name when item is selected in RadioButtonList

    Private Sub rbl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbl1.SelectedIndexChanged
            If rbl1.SelectedIndex = -1 Then
                col1 = rbl1.SelectedItem.Value
                Dim strProcName As String = ""

                Dim drv1 As DataRowView
                Dim dataview1 As New DataView()
                dataview1.Table = ds.Tables("rbl1")
                dataview1.RowFilter = "[desc] = '" & Trim(col1) & "'"

                For Each drv1 In dataview1
                    'Save value as session variable needed for later processing in session
                    Session("col1") = drv1("value")
                    strProcName = drv1("proc")
          End If
    End Sub

    Accepted Solution

    Closed, 500 points refunded.
    The Experts Exchange
    Community Support Moderator of all Ages

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video discusses moving either the default database or any database to a new volume.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now