数据绑定 RadioButtonList 绑定但现在显示在 GridView 中

Data-bound RadioButtonList binding but now showing up in GridView

我有一个 GridView 控件,其列包含数据绑定的 RadioButtonList。 RBL 正确地绑定到它的 DataTable,但没有显示在 GridView 中。在标记中添加 ListItems 确实显示了,并且显示了标签控件 - 我只是将这两个作为测试。有人看到我遗漏了什么吗?

TIA 寻求任何帮助。 麦克

标记:

<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>

代码隐藏:

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

您在 TemplateField 中声明了 RadioButtonList,但您没有为每一行检索该控件,而是创建了一个新的 RadioButtonList 并进行了填充。由于该新控件未包含在任何容器或 GridView 中,因此它不会显示在页面上。

您可以在 GridView 的 RowDataBound 事件处理程序中获取 TemplateField 的 RadioButtonList,并将数据绑定到该控件:

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
        ...
    }
}