【问题标题】:Events not firing for dynamic columns in SharePoint DataGridSharePoint DataGrid 中的动态列未触发事件
【发布时间】:2009-09-25 16:48:30
【问题描述】:

在 Sharepoint 的 Web 部件中,我试图根据用户选择的选项动态地将可变数量/排序的 ButtonColumns 添加到 DataGrid。问题是动态列没有触发我在 DataGrid 上设置的事件(例如 SelectedIndexChanged)。当表最初包含一组静态列时,它们是在 CreateChildControls() 中创建的,并且一切正常。但是,由于它们现在是动态的,所以我必须延迟添加它们,直到搜索按钮的单击事件触发之后。我想知道是否有某个地方需要我将列创建移动到仍然允许它是动态的但也允许它注册/触发事件的地方。

在 CreateChildControls() 中创建 outputDG:

outputDG = new System.Web.UI.WebControls.DataGrid();
outputDG.CellPadding = 4;
outputDG.HeaderStyle.Font.Bold = false;
outputDG.HeaderStyle.Font.Name = "Verdana";
outputDG.HeaderStyle.BackColor = Color.FromArgb(242,242,242);
outputDG.HeaderStyle.ForeColor = Color.FromArgb(128,128,128);
outputDG.HeaderStyle.Wrap = false;
//outputDG.ItemStyle.BorderColor = Color.Navy;
outputDG.HorizontalAlign = HorizontalAlign.Left;
//outputDG.BorderWidth = 1;
outputDG.GridLines = GridLines.Horizontal;
outputDG.Width = propsMgr.SearchGridWidth;
outputDG.PageSize = 10;
outputDG.AllowPaging = true;
outputDG.PagerStyle.Mode = PagerMode.NumericPages;
outputDG.PagerStyle.PageButtonCount = 5;
outputDG.PagerStyle.NextPageText = "Next Page";
outputDG.PagerStyle.PrevPageText = "Previous Page";
outputDG.PagerStyle.Visible = true;
outputDG.PageIndexChanged += new DataGridPageChangedEventHandler(this.outputDG_PageIndexChanged);
outputDG.AllowSorting = false;
outputDG.SortCommand += new DataGridSortCommandEventHandler(this.outputDG_SortCommand);
outputDG.SelectedItemStyle.BackColor = Color.FromArgb(255,244,206);
outputDG.SelectedIndexChanged += new EventHandler(this.outputDG_SelectedIndexChanged);
outputDG.ItemCreated += new DataGridItemEventHandler(this.outputDG_ItemCreated);
outputDG.AutoGenerateColumns = false;
outputDG.ItemCommand += new DataGridCommandEventHandler(outputDG_ItemCommand);
Controls.Add(outputDG);

在搜索按钮的点击事件期间:

ButtonColumn buttonColumnSelect = new ButtonColumn();
buttonColumnSelect.ButtonType = ButtonColumnType.LinkButton;
buttonColumnSelect.CommandName = "Select";
buttonColumnSelect.HeaderText = "Column";
buttonColumnSelect.DataTextField = "columnField";
outputDG.Columns.Add(buttonColumnSelect);

然后在同一事件中,我检查结果集并添加到我的数据行中。就像我提到的那样,当 ButtomColumn 代码在 CreateChildControls() 中启动时,这一切都可以恢复,但是一旦它被移入事件,它就会停止工作。我最好的猜测是,该列的事件没有机会注册以触发,因为它是从不同的事件发生的。如果我需要通过不同的方式构建 DataGrid 来解决这个问题,我绝对愿意;我只需要能够动态指定要使用的不同列。

【问题讨论】:

    标签: c# sharepoint events datagrid


    【解决方案1】:

    也许你需要在 DataGrid 上设置 ID-Attribute。 否则asp很难找到你的控件。

    【讨论】:

      【解决方案2】:

      我的情况的解决方法是将添加列移动到页面的加载事件,并在添加所有列之后将 DataGrid 添加到控件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多