c# - 事件未触发 SharePoint DataGrid 中的动态列

标签 c# sharepoint events datagrid

在 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 来解决这个问题,我绝对愿意;我只需要能够动态指定要使用的不同列。

最佳答案

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

关于c# - 事件未触发 SharePoint DataGrid 中的动态列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1478387/

相关文章:

swing - 是否可以有一个带有事件处理程序的项目符号列表?

c# - 在 ui 线程中执行委托(delegate)(使用消息泵)

sharepoint - 在没有本地 sharepoint 安装的情况下开发 sharepoint 项目

asp.net - 使用 HttpModules 将事件附加到 Session OnStart 和 OnEnd

powershell - 如何使用 PowerShell 设置新 SharePoint 字段的内部名称?

selenium - 如何使用 selenium WebDriver 捕获全页屏幕截图

javascript - 无法将事件附加到上下文片段中的元素

c# - SQL/C# 为客户预订行程

javascript - 可编辑的cshtml查看页面另存为PDF

c# - 如何在 TreeView 中获取原始值?