Hi. I've searched the forums and cannot find a solution to this problem. Hopefully I did not miss something.
I have created a self-referencing grid completely in the code behind. Everything works great except when I collapse a section, I cannot get it to re-appear when I click the +. The page reloads but stays in the exact same state as previously.
So, I guess I'm trying to figure out what code is actually making the child tables display or hide. It does not seem like they are being generated, but I cannot figure out why they would not be generated.
This is my ASCX file:
This is my C# code-behind which generates everything:
I had this entire page working properly with a grid defined in the ASCX file. But when I moved the code to the code-behind so it could be formatted as the client requested, then the expand stopped working.
Thanks in advance for any help you can give!
Chris
I have created a self-referencing grid completely in the code behind. Everything works great except when I collapse a section, I cannot get it to re-appear when I click the +. The page reloads but stays in the exact same state as previously.
So, I guess I'm trying to figure out what code is actually making the child tables display or hide. It does not seem like they are being generated, but I cannot figure out why they would not be generated.
This is my ASCX file:
<div id="myCoursesPage"> |
<asp:PlaceHolder ID="phCatalog" runat="server" /> |
</div> |
This is my C# code-behind which generates everything:
public partial class modules_MyCourses_admin_ManageCurricula_ucShowCatalog : System.Web.UI.UserControl |
{ |
CurriculaManager CM = new CurriculaManager(); |
DataSet catalogDS = null; |
HtmlGenericControl CatalogHeader = null; |
HtmlGenericControl CatalogDiv = null; |
GridControl currentGrid = null; |
private bool _showUnassigned = true; |
public bool ShowUnassigned |
{ |
get |
{ |
return _showUnassigned; |
} |
set |
{ |
_showUnassigned = value; |
} |
} |
protected void Page_Load(object sender, EventArgs e) |
{ |
this.Page.PreRenderComplete += new EventHandler(Page_PreRenderComplete); |
if (catalogDS == null) |
{ |
GetDataset(); |
Session["CatalogGridSource"] = catalogDS; |
} |
LoadCatalog(catalogDS); |
} |
protected void Page_PreRenderComplete(object sender, EventArgs e) |
{ |
foreach (Control ctrl in phCatalog.Controls) |
{ |
foreach (Control ctrldiv in ctrl.Controls) |
{ |
if (ctrldiv.ID.StartsWith("CatalogGrid_")) |
{ |
RadGrid catalog = ctrldiv as RadGrid; |
HideExpandColumnRecursive(catalog.MasterTableView); |
} |
} |
} |
} |
public void GetDataset() |
{ |
catalogDS = CM.ClassificationsGetCatalogList(((PortalPage)this.Page).UsePortalID, Convert.ToInt32(Session["UserID"]), 1); |
} |
protected void CatalogGrid_ColumnCreated(object sender, GridColumnCreatedEventArgs e) |
{ |
if (e.Column is GridExpandColumn) |
{ |
e.Column.Visible = false; |
} |
//else if (e.Column is GridBoundColumn) |
//{ |
// e.Column.HeaderStyle.Width = Unit.Pixel(100); |
//} |
if (e.Column is GridBoundColumn || e.Column is GridTemplateColumn) |
{ |
e.Column.HeaderStyle.Width = Unit.Pixel(100); |
} |
} |
private void CreateGrid(int CatalogID) |
{ |
currentGrid = new GridControl(); |
currentGrid.EnableEmbeddedSkins = false; |
currentGrid.Skin = "Default"; |
currentGrid.EnableViewState = false; |
currentGrid.HorizontalAlign = HorizontalAlign.NotSet; |
currentGrid.GridLines = GridLines.None; |
currentGrid.ShowHeader = false; |
currentGrid.AllowPaging = false; |
currentGrid.ColumnCreated += new GridColumnCreatedEventHandler(CatalogGrid_ColumnCreated); |
currentGrid.ID = "CatalogGrid_" + CatalogID; |
currentGrid.MasterTableView.DataKeyNames = new string[2] { "CatalogID", "ParentClassificationID" }; |
currentGrid.Width = Unit.Percentage(100); |
currentGrid.AutoGenerateColumns = false; |
currentGrid.GroupingEnabled = false; |
currentGrid.ShowGroupPanel = false; |
currentGrid.AllowSorting = true; |
currentGrid.MasterTableView.HierarchyDefaultExpanded = true; |
currentGrid.MasterTableView.HierarchyLoadMode = GridChildLoadMode.ServerOnDemand; |
currentGrid.MasterTableView.SelfHierarchySettings.KeyName = "CatalogID"; |
currentGrid.MasterTableView.SelfHierarchySettings.ParentKeyName = "ParentClassificationID"; |
currentGrid.MasterTableView.SelfHierarchySettings.MaximumDepth = 10; |
currentGrid.MasterTableView.TableLayout = GridTableLayout.Fixed; |
if (Assembly.GetAssembly(typeof(ScriptManager)).FullName.IndexOf("3.5") != -1) |
{ |
currentGrid.MasterTableView.FilterExpression = @"it[""ParentClassificationID""] = " + CatalogID; |
} |
else |
{ |
currentGrid.MasterTableView.FilterExpression = "ParentClassificationID = " + CatalogID; |
} |
currentGrid.ClientSettings.AllowExpandCollapse = true; |
GridTemplateColumn templateColumn = new GridTemplateColumn(); |
templateColumn.UniqueName = "CatalogName"; |
templateColumn.ItemTemplate = new MyTemplate(templateColumn.UniqueName); |
currentGrid.MasterTableView.Columns.Add(templateColumn); |
currentGrid.ItemCreated += new GridItemEventHandler(CatalogGrid_ItemCreated); |
currentGrid.ItemDataBound += new GridItemEventHandler(CatalogGrid_ItemDataBound); |
currentGrid.NeedDataSource += new GridNeedDataSourceEventHandler(currentGrid_NeedDataSource); |
} |
private class MyTemplate : ITemplate |
{ |
protected Button btExpandCollapse; |
protected Image imClassification; |
protected Label lbCatalogName; |
protected HyperLink hyCurriculumName; |
protected Literal ltShortDescription; |
protected HtmlGenericControl divAround; |
private string colname; |
public MyTemplate(string cName) |
{ |
colname = cName; |
} |
public void InstantiateIn(System.Web.UI.Control container) |
{ |
btExpandCollapse = new Button(); |
btExpandCollapse.ID = "btExpandCollapse"; |
btExpandCollapse.CommandName = "ExpandCollapse"; |
imClassification = new Image(); |
imClassification.ID = "imClassification"; |
imClassification.Visible = false; |
lbCatalogName = new Label(); |
lbCatalogName.ID = "lbCatalogName"; |
hyCurriculumName = new HyperLink(); |
hyCurriculumName.ID = "hyCurriculumName"; |
ltShortDescription = new Literal(); |
ltShortDescription.ID = "ltShortDescription"; |
divAround = new HtmlGenericControl(); |
divAround.ID = "divAround"; |
divAround.Controls.Add(btExpandCollapse); |
divAround.Controls.Add(imClassification); |
divAround.Controls.Add(lbCatalogName); |
divAround.Controls.Add(hyCurriculumName); |
divAround.Controls.Add(ltShortDescription); |
container.Controls.Add(divAround); |
} |
} |
void currentGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e) |
{ |
GridControl grid = (GridControl)source; |
int ClassificationID = Convert.ToInt32(grid.ID.Substring(grid.ID.IndexOf("_") + 1)); |
if (catalogDS == null) |
{ |
GetDataset(); |
Session["CatalogGridSource"] = catalogDS; |
} |
grid.DataSource = GetCatalogRows(catalogDS, ClassificationID); |
} |
private DataSet GetCatalogRows(DataSet ds, int CatalogID) |
{ |
DataSet dsNew = new DataSet(); |
dsNew.Tables.Add(ds.Tables[0].Clone()); |
dsNew.Tables[0].Clear(); |
DataRow[] rows = ds.Tables[0].Select("ParentClassificationID = " + CatalogID); |
dsNew.Merge(rows); |
foreach(DataRow row in rows) |
{ |
dsNew.Merge(GetCatalogRows(ds, Convert.ToInt32(row["CatalogID"]))); |
} |
return dsNew; |
} |
public void HideExpandColumnRecursive(GridTableView tableView) |
{ |
GridItem[] nestedViewItems = tableView.GetItems(GridItemType.NestedView); |
foreach (GridNestedViewItem nestedViewItem in nestedViewItems) |
{ |
foreach (GridTableView nestedView in nestedViewItem.NestedTableViews) |
{ |
nestedView.Style["border"] = "0"; |
Button MyExpandCollapseButton = (Button)nestedView.ParentItem.FindControl("btExpandCollapse"); |
if (nestedView.Items.Count == 0) |
{ |
if (MyExpandCollapseButton != null) |
{ |
MyExpandCollapseButton.Style["visibility"] = "hidden"; |
} |
GridDataItem dataItem = (GridDataItem)nestedView.ParentItem; |
TableCell cell = dataItem[dataItem.OwnerTableView.ExpandCollapseColumn.UniqueName]; |
cell.Controls[0].Visible = false; |
nestedViewItem.Visible = false; |
} |
else |
{ |
if (MyExpandCollapseButton != null) |
{ |
MyExpandCollapseButton.Style.Remove("visibility"); |
} |
} |
if (nestedView.HasDetailTables) |
{ |
HideExpandColumnRecursive(nestedView); |
} |
} |
} |
} |
protected void CatalogGrid_ItemCreated(object sender, GridItemEventArgs e) |
{ |
if (e.Item is GridCommandItem) |
{ |
e.Item.Style["display"] = "none"; |
} |
if (e.Item is GridHeaderItem) |
{ |
//Hides the headers |
e.Item.Style["display"] = "none"; |
} |
if (e.Item is GridNestedViewItem) |
{ |
e.Item.Cells[0].Visible = false; |
e.Item.Cells[0].Width = Unit.Pixel(150); |
} |
if (e.Item is GridNoRecordsItem) |
{ |
e.Item.Style["display"] = "none"; |
e.Item.OwnerTableView.Visible = false; |
} |
} |
protected void CatalogGrid_ItemDataBound(object sender, GridItemEventArgs e) |
{ |
if (e.Item is GridDataItem) |
{ |
string Path = Server.MapPath("~\\Portals\\" + ((PortalPage)this.Page).UsePortalID.ToString() + "\\img\\Catalog\\"); |
GridDataItem gridDataItem = e.Item as GridDataItem; |
Image imClassification = gridDataItem["CatalogName"].FindControl("imClassification") as Image; |
Label lbCatalogName = gridDataItem["CatalogName"].FindControl("lbCatalogName") as Label; |
HyperLink hyCurriculumName = gridDataItem["CatalogName"].FindControl("hyCurriculumName") as HyperLink; |
if (Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "IsCurriculum").ToString()) == 1) |
{ |
hyCurriculumName.Text = DataBinder.Eval(e.Item.DataItem, "CatalogName").ToString() + " (" + DataBinder.Eval(e.Item.DataItem, "NumCourses").ToString() + ")"; |
hyCurriculumName.NavigateUrl = "~/modules/catalog/Package.aspx?CourseGroupID=" + DataBinder.Eval(e.Item.DataItem, "CatalogID").ToString(); |
lbCatalogName.Visible = false; |
} |
else |
{ |
lbCatalogName.Text = DataBinder.Eval(e.Item.DataItem, "CatalogName").ToString(); |
hyCurriculumName.Visible = false; |
} |
if (DataBinder.Eval(e.Item.DataItem, "ImageName").ToString() != "") |
{ |
imClassification.ImageUrl = Path + DataBinder.Eval(e.Item.DataItem, "ImageName").ToString(); |
imClassification.Visible = true; |
} |
Literal ltShortDescription = gridDataItem["CatalogName"].FindControl("ltShortDescription") as Literal; |
if (DataBinder.Eval(e.Item.DataItem, "ShortDescription").ToString() != "") |
{ |
ltShortDescription.Text = "<p>" + DataBinder.Eval(e.Item.DataItem, "ShortDescription").ToString() + "</p>"; |
} |
else |
{ |
ltShortDescription.Visible = false; |
} |
//Create Levels |
Button btExpandCollapse = gridDataItem["CatalogName"].FindControl("btExpandCollapse") as Button; |
btExpandCollapse.Click += new EventHandler(button_Click); |
if (!IsPostBack) |
{ |
btExpandCollapse.CssClass = (Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "ShowExpanded").ToString()) == 0) ? "rgCollapse" : "rgExpand"; |
} |
else |
{ |
btExpandCollapse.CssClass = (e.Item.Expanded) ? "rgCollapse" : "rgExpand"; |
} |
btExpandCollapse.CssClass = (e.Item.Expanded) ? "rgCollapse" : "rgExpand"; |
HtmlGenericControl divAround = gridDataItem["CatalogName"].FindControl("divAround") as HtmlGenericControl; |
int level = (e.Item.ItemIndexHierarchical.Split(':').Length > 0 ? e.Item.ItemIndexHierarchical.Split(':').Length - 1 : e.Item.ItemIndexHierarchical.Split(':').Length); |
if (level > 0) |
{ |
divAround.Style["margin-left"] = (level * 20) + "px"; |
} |
//if (e.Item.OwnerTableView.HierarchyLoadMode == GridChildLoadMode.Client) |
//{ |
// string script = String.Format(@"$find(""{0}"")._toggleExpand(this, event); return false;", gridDataItem.Parent.Parent.ClientID); |
// btExpandCollapse.OnClientClick = script; |
//} |
} |
} |
private void LoadCatalog(DataSet ds) |
{ |
DataSet Catalogds = new DataSet(); |
Catalogds.Tables.Add(ds.Tables[0].Clone()); |
Catalogds.Tables[0].Clear(); |
DataRow[] rows = null; |
rows = ds.Tables[0].Select("ParentClassificationID IS NULL"); |
foreach (DataRow row in rows) |
{ |
Catalogds.Tables[0].ImportRow(row); |
} |
int prevClassificationID = -1; |
for (int counter = 0; counter < Catalogds.Tables[0].Rows.Count; counter++) |
{ |
DataRow row = Catalogds.Tables[0].Rows[counter]; |
int ClassificationID = -1; |
if (row["CatalogID"] != DBNull.Value) |
{ |
ClassificationID = Convert.ToInt32(row["CatalogID"].ToString()); |
} |
if (ClassificationID != prevClassificationID) |
{ |
CatalogDiv = new HtmlGenericControl("div"); |
CatalogDiv.ID = "CatalogDiv" + row["CatalogID"].ToString(); |
CatalogDiv.Attributes.Add("class", "root-catalog"); |
CatalogHeader = new HtmlGenericControl("h2"); |
CatalogHeader.ID = "CatalogHeader" + row["CatalogID"].ToString(); |
CatalogHeader.InnerText = row["CatalogName"].ToString(); |
CatalogDiv.Controls.Add(CatalogHeader); |
prevClassificationID = ClassificationID; |
} |
CreateGrid(ClassificationID); |
CatalogDiv.Controls.Add(currentGrid); |
phCatalog.Controls.Add(CatalogDiv); |
} |
} |
void button_Click(object sender, EventArgs e) |
{ |
((Button)sender).CssClass = (((Button)sender).CssClass == "rgExpand") ? "rgCollapse" : "rgExpand"; |
} |
I had this entire page working properly with a grid defined in the ASCX file. But when I moved the code to the code-behind so it could be formatted as the client requested, then the expand stopped working.
Thanks in advance for any help you can give!
Chris