Skip to content

Instantly share code, notes, and snippets.

@shamp00
Created August 30, 2012 17:05
Show Gist options
  • Save shamp00/3533491 to your computer and use it in GitHub Desktop.
Save shamp00/3533491 to your computer and use it in GitHub Desktop.
Fix for XAF selection box which does not deselect after clearing the selection.
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Templates;
using DevExpress.ExpressApp.Utils;
using DevExpress.ExpressApp.Web;
using DevExpress.ExpressApp.Web.Editors.ASPx;
using DevExpress.ExpressApp.Web.Templates;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;
using NetModule.Controllers;
namespace MyModule.Web
{
public class MyClass : ViewController<ListView>
{
private ISupportViewChanged template;
protected override void OnActivated()
{
base.OnActivated();
template = Frame.Template as ISupportViewChanged;
if (template != null)
{
template.ViewChanged += new EventHandler<TemplateViewChangedEventArgs>(MyClass_ViewChanged);
}
}
protected override void OnDeactivated()
{
if (template != null)
{
template.ViewChanged -= new EventHandler<TemplateViewChangedEventArgs>(MyClass_ViewChanged);
}
base.OnDeactivated();
}
void MyClass_ViewChanged(object sender, TemplateViewChangedEventArgs e)
{
ASPxGridListEditor editor = View.Editor as ASPxGridListEditor;
if (editor != null && editor.Grid != null)
{
if (editor.Grid.Parent != null & editor.Grid.Page != null)
{
foreach (GridViewColumn column in editor.Grid.Columns)
{
if (column is XafGridViewCommandColumn & column.HeaderTemplate != null & column.HeaderTemplate is SelectionColumnHeaderTemplate)
{
SelectionColumnHeaderTemplate selectionColumnHeaderTemplate = column.HeaderTemplate as SelectionColumnHeaderTemplate;
if (selectionColumnHeaderTemplate != null)
selectionColumnHeaderTemplate.Dispose();
column.HeaderTemplate = new FixedSelectionColumnHeaderTemplate();
}
}
}
}
}
}
public class FixedSelectionColumnHeaderTemplate : ITemplate, IDisposable
{
public const string SelectAllCommandName = "SelectAll";
public const string UnselectAllCommandName = "UnselectAll";
private ASPxCheckBox selectAll;
private GridViewHeaderTemplateContainer templateContainer;
private bool delayedLoadPostData;
private void TryLoadPostDataToCheckBox()
{
if (selectAll.Page != null)
{
TryLoadPostData();
}
else
{
delayedLoadPostData = true;
}
}
private XafCallbackManager CallbackManager
{
get
{
Page page = GridControl != null ? GridControl.Page : null;
Guard.ArgumentNotNull(page, "Page");
Guard.TypeArgumentIs(typeof(ICallbackManagerHolder), page.GetType(), "Page");
return ((ICallbackManagerHolder)page).CallbackManager;
}
}
private ASPxGridView GridControl
{
get { return templateContainer != null ? templateContainer.Grid : null; }
}
private void selectAll_Load(object sender, EventArgs e)
{
selectAll.Load -= new EventHandler(selectAll_Load);
string clientScript = CallbackManager.GetScript(GridControl.UniqueID, string.Format("s.GetChecked() ? '{0}' : '{1}'", SelectAllCommandName, UnselectAllCommandName));
selectAll.ClientSideEvents.CheckedChanged = string.Format(@"function(s, e) {{ {0} }}", clientScript);
// Is this the part to comment out Dennis?
//if (delayedLoadPostData)
//{
// TryLoadPostData();
// delayedLoadPostData = false;
//}
}
private void TryLoadPostData()
{
if (System.Web.HttpContext.Current.Request.Params[selectAll.UniqueID] != null)
{
((IPostBackDataHandler)selectAll).LoadPostData(selectAll.UniqueID, System.Web.HttpContext.Current.Request.Params);
}
}
public ASPxCheckBox SelectAll
{
get
{
return selectAll;
}
}
#region ITemplate Members
public void InstantiateIn(Control container)
{
templateContainer = (GridViewHeaderTemplateContainer)container;
Panel panel = new Panel();
panel.HorizontalAlign = HorizontalAlign.Center;
container.Controls.Add(panel);
selectAll = RenderHelper.CreateASPxCheckBox();
selectAll.ID = "SCh";
selectAll.EnableClientSideAPI = true;
selectAll.AutoPostBack = false;
selectAll.Load += new EventHandler(selectAll_Load);
panel.Controls.Add(selectAll);
TryLoadPostDataToCheckBox();
}
#endregion
#region IDisposable Members
public void Dispose()
{
if (selectAll != null)
{
selectAll.Load -= new EventHandler(selectAll_Load);
selectAll.Dispose();
selectAll = null;
}
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment