2009-09-25 22 views
4

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

outputDG创作的CreateChildControls():

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

回答

0

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

0

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