数据绑定的RadioButtonList绑定,但现在显示在GridView

时间:2016-07-25 17:06:41

标签: asp.net data-binding radiobuttonlist

我有一个GridView控件,其中包含一个包含数据绑定的RadioButtonList的列。 RBL正确绑定到其DataTable,但未在GridView中显示。在标记中添加ListItem确实显示,并且Label控件正在显示 - 我只是将这两个作为测试。有没有人看到我失踪的东西?

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

1 个答案:

答案 0 :(得分:1)

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