【问题标题】:Data-bound RadioButtonList binding but now showing up in GridView数据绑定 RadioButtonList 绑定,但现在显示在 GridView 中
【发布时间】:2016-07-26 03:41:28
【问题描述】:

我有一个 GridView 控件,其中的一列包含一个数据绑定 RadioButtonList。 RBL 已正确绑定到其 DataTable,但未显示在 GridView 中。在标记中添加 ListItems 确实显示,并且显示了一个 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

【问题讨论】:

  • 你检查过dtPresetText不为空吗?
  • 是的,它有 13 行,我什至可以在绑定后的即时窗口中从 DDL 中获取值。

标签: asp.net data-binding radiobuttonlist


【解决方案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
        ...
    }
}

【讨论】:

  • 啊啊啊,效果很好。非常感谢@ConnorsFan!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多