Search code examples
asp.netdata-bindingradiobuttonlist

Data-bound RadioButtonList binding but now showing up in GridView


I have a GridView control with a column containing a data-bound RadioButtonList. The RBL is binding to its DataTable properly but is not showing up in the GridView. Adding ListItems in the markup does show, and a Label control is showing - I just did these two as a test. Does anyone see what I'm missing?

TIA for any assistance. Mike

Markup:

<asp:TemplateField HeaderText="Preset Text" HeaderStyle-HorizontalAlign="Center">
     <ItemTemplate>
         <asp:RadioButtonList ID="rblPresetText" runat="server" DataValueField="pKey" DataTextField="Contents" GroupName="PresetText" RepeatDirection="Vertical"></asp:RadioButtonList>
     </ItemTemplate>
</asp:TemplateField>

Codebehind:

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

        GlobalVar.LoadData(Session("UserPKey"))
        Header1.ConnectionStr = GlobalVar.ConnectString
        Header1.HDLawFirm = GlobalVar.LawFirmDir

       If Page.IsPostBack = False Then                             
            FillNotesDataSet()
            BindNotesGrid() 
            BindPresetTextRadioButtonList()
        End If

    End Sub

Protected Sub BindPresetTextRadioButtonList()

        Dim DAL As New DataAccessLayer
        Dim dtPresetText As New DataTable
        Dim rblPresetText As New RadioButtonList

        dtPresetText = DAL.GetTextPickerTextForUser(Session("ClientKey"), Session("UserPKey"))

        rblPresetText.DataSource = dtPresetText
        rblPresetText.DataBind()

    End Sub

Solution

  • You declare the RadioButtonList in a TemplateField but, instead of retrieving that control for each row, you create a new RadioButtonList that you populate. Since that new control is not included in any container or in the GridView, it does not show up on the page.

    You can get the RadioButtonList of your TemplateField in the RowDataBound event handler of the GridView and bind the data to that control:

    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            RadioButtonList rblPresetText = e.Row.FindControl("rblPresetText") as RadioButtonList;
    
            // Bind the data to the RadioButtonList
            ...
        }
    }