【问题标题】:Text beside a dynamic tickbox in gridviewgridview中动态复选框旁边的文本
【发布时间】:2014-09-02 00:57:41
【问题描述】:

我有一个gridview,它在gridview 的headerTemplate 中动态填充复选框。标题是一个复选框和一个动态文本/标签。但是我无法让 headertemplate 显示文本:

页面加载:

   foreach (Module m in listModule)
        {
            TemplateField tfield = new TemplateField();
            CheckBox cbAll = new CheckBox();
            tfield.HeaderTemplate = new TickColumn();
            tfield.HeaderText = m.ModuleName; //<- This is not getting displayed.
            gvUsers.Columns.Add(tfield);
        }

class TickColumn : ITemplate
    {
        public void InstantiateIn(System.Web.UI.Control container)
        {
            CheckBox cbAll = new CheckBox();
            cbAll.ID = "cbAll";
            container.Controls.Add(cbAll);
        }
    }

由于某种原因,没有显示 HeaderText。

或者 我尝试在类中添加一个标签,但我不知道如何将值传递给:

static string lblname;

页面加载:

foreach (Module m in listModule)
        {
            TemplateField tfield = new TemplateField();
            CheckBox cbAll = new CheckBox();
            tfield.HeaderTemplate = new TickColumn();
            lblname = m.ModuleName;
            gvUsers.Columns.Add(tfield);
        }


class TickColumn : ITemplate
    {
        public void InstantiateIn(System.Web.UI.Control container)
        {
            CheckBox cbAll = new CheckBox();
            Label lbl = new Label();
            lbl.Text = lblName;
            cbAll.ID = "cbAll";
            container.Controls.Add(cbAll);
            container.Controls.Add(lbl);
        }
    } 

使用此代码,每个动态列标签的文本变得相同,最后一个元素名称显示在所有标签中。我该如何解决这个问题?

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    我意识到我实际上可以使用构造函数传递值:

    class TickColumn : ITemplate
        {
    private String _label;
            public TickColumn(String p_label)
            {
                this._label = p_label;
            }
            public void InstantiateIn(System.Web.UI.Control container)
            {
                CheckBox cbAll = new CheckBox();
                cbAll.ID = "cbAll";
                cbAll.Text = this._label;
                container.Controls.Add(cbAll);
            }
       }
    

    【讨论】:

      猜你喜欢
      • 2015-03-20
      • 2013-08-22
      • 1970-01-01
      • 2019-08-03
      • 1970-01-01
      • 2021-06-23
      • 2012-05-03
      • 2023-03-27
      • 1970-01-01
      相关资源
      最近更新 更多