使用资源键绑定字段中的标题文本

本文关键字:标题 文本 字段 绑定 资源 | 更新日期: 2023-09-27 18:23:54

我正试图将Resource键添加到网格视图的标题文本中,但过了很长时间,我无法解决这个问题。

我尝试过像这样的编码

<asp:BoundField DataField="CategoryName"  HeaderText="<%$ Resources:ViewTC_CourseCategory,HeaderCategoryName %>"
                 ItemStyle-Width="12">
    <ItemStyle HorizontalAlign="Center" />
</asp:BoundField>

我的资源文件包含如下值:

HeaderCategoryName.Text           Category Name

显示以下错误:The resource object with key 'HeaderCategoryName' was not found

实现这一要求的正确方法是什么。请帮忙。

使用资源键绑定字段中的标题文本

试试这个

  void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {
    if(e.Row.RowType == DataControlRowType.HeaderRow)
    {
      // use the index of your cell
      e.Row.Cells[0].Text = "Your Resource file string";
    }
  }

请确保资源文件名和页面名符合ASP.NET用于搜索本地资源文件的约定:

1.)如果页面名称为:Sample.ascx,则资源文件名必须为:Sample.ascx.resx.

2.)还要确保App_LocalResource文件夹(包含sample.ascx.resx)位于UserControls文件夹内。