dynamically created radiobuttonlist

Janet picture Janet · Jan 4, 2010 · Viewed 7.7k times · Source

Have a master page. The content page has a list with hyperlinks containing request variables. You click on one of the links to go to the page containing the radiobuttonlist (maybe).

First problem: When I get to the new page, I use one of the variables to determine whether to add a radiobuttonlist to a placeholder on the page. I tried to do it in page)_load but then couldn't get the values selected. When I played around doing it in preInit, the first time the page is there, I can't get to the page's controls. (Object reference not set to an instance of an object.) I think it has something to do with the MasterPage and page content? The controls aren't instantiated until later? (using vb by the way)

Second problem: Say I get that to work, once I hit a button, can I still access the passed request variable to determine the selected item in the radiobuttonlist?

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
    'get sessions for concurrent 

    Dim Master As New MasterPage
    Master = Me.Master

    Dim myContent As ContentPlaceHolder = CType(Page.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)
    If Request("str") = "1" Then
        Dim myList As dsSql = New dsSql()   ''''instantiate the function to get dataset
        Dim ds As New Data.DataSet

        ds = myList.dsConSessionTimes(Request("eid"))
        If ds.Tables("conSessionTimes").Rows.Count > 0 Then
            Dim conY As Integer = 1

            CType(myContent.FindControl("lblSidCount"), Label).Text = ds.Tables("conSessionTimes").Rows.Count.ToString

Sorry to be so needy - but maybe someone could direct me to a page with examples? Maybe seeing it would help it make sense?

Thanks....JB

Answer

Americus picture Americus · Jan 5, 2010

If you have a content placeholder, could you just add the radio button list control to there?

On Master Page:

<asp:ContentPlaceHolder id="ContentPlaceHolderForRadioButtonList" runat="server">       
</asp:ContentPlaceHolder>

Some links, containing request variables used on the next page.

<a href="RadioButtonList.aspx?ref=first" >Link 1</a>
<a href="RadioButtonList.aspx?ref=second" >Link 2</a><br />
<a href="RadioButtonList.aspx?ref=third" >Link 3</a><br />
<a href="RadioButtonList.aspx?ref=forth" >Link 4</a><br />
<a href="RadioButtonList.aspx?ref=fifth" >Link 5</a><br />
<a href="RadioButtonList.aspx?ref=sixth" >Link 6</a>

Now on the page with the radio button list, add it into the content placeholder.

<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolderForRadioButtonList" Runat="Server">
<!-- radio button list to be dynamically populated-->
    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
    </asp:RadioButtonList>
</asp:Content>

RadioButtonList.aspx: Code to dynamically populate your radio button list based on the information passed in.

 Partial Class RadioButtonList
    Inherits System.Web.UI.Page
    Private selection As String = ""

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        selection = IIf(Request.QueryString("ref") IsNot Nothing, Request.QueryString("ref"), "")
        If selection = "first" Then
            RadioButtonList1.Items.Add(New ListItem("first", "1"))
            RadioButtonList1.Items.Add(New ListItem("third", "3"))
            RadioButtonList1.Items.Add(New ListItem("fifth", "5"))
        ElseIf selection = "second" Then
            RadioButtonList1.Items.Add(New ListItem("second", "2"))
            RadioButtonList1.Items.Add(New ListItem("forth", "4"))
            RadioButtonList1.Items.Add(New ListItem("sixth", "6"))
        Else
            RadioButtonList1.Items.Add(New ListItem("first", "1"))
            RadioButtonList1.Items.Add(New ListItem("second", "2"))
            RadioButtonList1.Items.Add(New ListItem("third", "3"))
            RadioButtonList1.Items.Add(New ListItem("forth", "4"))
            RadioButtonList1.Items.Add(New ListItem("fifth", "5"))
            RadioButtonList1.Items.Add(New ListItem("sixth", "6"))
        End If

        'set the selected radio button
        For i As Integer = 0 To RadioButtonList1.Items.Count - 1
            If RadioButtonList1.Items(i).Text = selection Then
                RadioButtonList1.Items(i).Selected = True
                Exit For
            End If
        Next

    End Sub

End Class

Hopefully you'll find something helpful in here.