You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在后台代码中填充Telerik RadGrid的TemplateColumn内标签

解决Telerik RadGrid模板列Label无法填充数据的问题

嘿,我太懂这个问题了——RadGrid的模板列控件不会自动帮你绑定数据,直接设DataSource然后DataBind()只会生成空的Label,得手动在ItemDataBound事件里处理才行。

问题根源

你现在的代码只是给网格挂了数据源,但模板列里的<asp:Label>并没有和DataTable的字段建立绑定关系,所以网格只会生成行结构,不会自动把数据塞进这些Label里。要访问模板列里的控件,必须借助RadGrid的ItemDataBound事件,在每行数据绑定的时候手动赋值。

具体解决步骤

1. 给ASPX里的RadGrid添加ItemDataBound事件

先修改你的RadGrid标签,加上OnItemDataBound属性,指定后台的事件处理方法:

<telerik:RadGrid ID="grd_transactions" runat="server" GroupPanelPosition="Top" AutoGenerateColumns="False" OnItemDataBound="grd_transactions_ItemDataBound">
 <!-- 原来的GroupingSettings、MasterTableView及列定义都保持不变 -->
</telerik:RadGrid>

2. 后台实现事件处理方法

在后台代码里写这个事件方法,当每行数据绑定到网格项时,找到对应的Label并把DataTable里的值赋进去:

protected void grd_transactions_ItemDataBound(object sender, GridItemEventArgs e)
{
    // 只处理数据行,跳过表头、分页栏这些非数据项
    if (e.Item is GridDataItem dataItem)
    {
        // 获取当前行的数据源(DataTable作为数据源时会包装成DataRowView)
        DataRowView rowView = dataItem.DataItem as DataRowView;
        DataRow row = rowView.Row;

        // 逐个找到Label并赋值
        Label lbl_Type = dataItem.FindControl("lbl_Type") as Label;
        if (lbl_Type != null)
        {
            lbl_Type.Text = row["Type"].ToString();
        }

        Label lbl_Date = dataItem.FindControl("lbl_Date_Time_UTC_GMT") as Label;
        if (lbl_Date != null)
        {
            // 注意后台DataTable的列名是"Date & Time (UTC/GMT)",不是ASPX里转义后的&amp;
            lbl_Date.Text = row["Date & Time (UTC/GMT)"].ToString();
        }

        Label lbl_BTC = dataItem.FindControl("lbl_BTC_Investment_Address") as Label;
        if (lbl_BTC != null)
        {
            lbl_BTC.Text = row["BTC Investment Address"].ToString();
        }

        Label lbl_Deposit = dataItem.FindControl("lbl_Deposit_Amount") as Label;
        if (lbl_Deposit != null)
        {
            lbl_Deposit.Text = row["Deposit Amount"].ToString();
        }

        Label lbl_Payout = dataItem.FindControl("lbl_Payout_Amount") as Label;
        if (lbl_Payout != null)
        {
            lbl_Payout.Text = row["Payout Amount"].ToString();
        }

        Label lbl_Status = dataItem.FindControl("lbl_Status") as Label;
        if (lbl_Status != null)
        {
            lbl_Status.Text = row["Status"].ToString();
        }
    }
}

3. 可选优化:简化DataTable列名

列名里带特殊字符(比如&、空格)容易在后台代码里出错,建议给DataTable用更简洁的列名,然后在模板列的HeaderText里保留原来的显示文本:

// 修改DataTable列定义
DataTable table = new DataTable();
table.Columns.Add("Type", typeof(string));
table.Columns.Add("DateTimeUTC", typeof(string)); // 替换原来的带特殊字符的列名
table.Columns.Add("BTCAddress", typeof(string));
table.Columns.Add("DepositAmount", typeof(string));
table.Columns.Add("PayoutAmount", typeof(string));
table.Columns.Add("Status", typeof(string));

// 赋值时用新列名
table.Rows.Add("1", "2024-05-20 12:00:00", "bc1qxxxx", "0.5 BTC" ,"1.2 BTC", "Completed");
table.Rows.Add("2", "2024-05-21 14:30:00", "bc1qyyyy", "0.3 BTC" ,"0.7 BTC", "Pending");
// 其他行同理

ASPX里的模板列HeaderText保持不变,比如日期列还是:

<telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn_Date_Time_UTC_GMT column" HeaderText="Date &amp; Time (UTC/GMT)" UniqueName="TemplateColumn_Date_Time_UTC_GMT">
 <!-- ItemTemplate和HeaderStyle不变 -->
</telerik:GridTemplateColumn>

然后后台赋值的时候改成row["DateTimeUTC"].ToString(),这样代码更干净,也不容易因为特殊字符出问题。

再啰嗦一句为啥原来的方法不行

RadGrid的自动数据绑定只对内置的绑定列(比如GridBoundColumn)有效,模板列是你自定义的内容,框架不知道要把数据放到哪个控件上,所以必须通过ItemDataBound事件手动把数据和控件关联起来。

内容的提问来源于stack exchange,提问作者SilverLight

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:45:15