Instantly share code, notes, and snippets.
Last active
December 12, 2015 10:08
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save DTTerastar/4756369 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NavHistory : Control | |
{ | |
private readonly List<Tuple<string, Action<string>>> _setters = new List<Tuple<string, Action<string>>>(); | |
private Dictionary<string, string> _changes = new Dictionary<string, string>(); | |
private ScriptManager _sm; | |
protected override void OnInit(EventArgs e) | |
{ | |
base.OnInit(e); | |
Page.Init += Page_Init; | |
Page.Items[typeof(NavHistory)] = this; | |
} | |
protected override void LoadViewState(object savedState) | |
{ | |
_changes = (Dictionary<string, string>)savedState; | |
} | |
protected override object SaveViewState() | |
{ | |
return _changes; | |
} | |
private void Page_Init(object sender, EventArgs e) | |
{ | |
_sm = ScriptManager.GetCurrent(Page); | |
if (_sm != null) _sm.Navigate += Navigate; | |
} | |
public static NavHistory GetCurrent(Page page) | |
{ | |
if (page == null) | |
throw new ArgumentNullException("page"); | |
return page.Items[typeof(NavHistory)] as NavHistory; | |
} | |
private void Navigate(object sender, HistoryEventArgs e) | |
{ | |
List<string> changed = new List<string>(); | |
Page.Trace.Write("NH.Navigate", e.State.DumpToString()); | |
foreach (string key in e.State) | |
{ | |
string value = e.State[key]; | |
if (_changes.Any(a => a.Key == key && a.Value == value)) | |
break; | |
_changes[key] = value; | |
changed.Add(key); | |
} | |
foreach (var a in _setters) | |
if (changed.Contains(a.Item1)) | |
a.Item2(_changes[a.Item1]); | |
} | |
private void AddHistoryValue(string key, string value) | |
{ | |
if (!_sm.IsInAsyncPostBack) return; | |
_changes[key] = value; | |
foreach (var pair in _changes) | |
_sm.AddHistoryPoint(pair.Key, pair.Value); | |
} | |
private static string Key(Control c) | |
{ | |
return c.ClientID; | |
} | |
public void Wire(string name, RadComboBox c, Action<string> action = null) | |
{ | |
if (c == null) throw new ArgumentNullException("c"); | |
Page.Trace.Write("NH.Wire", c.ID); | |
var key = name ?? Key(c); | |
c.SelectedIndexChanged += (o, args) => AddHistoryValue(key, c.Text); | |
_setters.Add(new Tuple<string, Action<string>>(key, a => | |
{ | |
c.SelectedValue = c.Text = a; | |
})); | |
if (action != null) _setters.Add(new Tuple<string, Action<string>>(key, action)); | |
} | |
public void Wire(string name, RadioButtonList c, Action<string> action = null) | |
{ | |
if (c == null) throw new ArgumentNullException("c"); | |
Page.Trace.Write("NH.Wire", c.ID); | |
var key = name ?? Key(c); | |
c.SelectedIndexChanged += (o, args) => AddHistoryValue(key, c.SelectedIndex.ToString()); | |
_setters.Add(new Tuple<string, Action<string>>(key, a => c.SelectedIndex = a.ToNullableInt32().GetValueOrDefault(-1))); | |
if (action != null) _setters.Add(new Tuple<string, Action<string>>(key, action)); | |
} | |
public void Wire(string name, CheckBoxList c, Action<string> action = null) | |
{ | |
if (c == null) throw new ArgumentNullException("c"); | |
Page.Trace.Write("NH.Wire", c.ID); | |
var key = name ?? Key(c); | |
c.SelectedIndexChanged += (o, args) => AddHistoryValue(key, c.SelectedIndex.ToString()); | |
_setters.Add(new Tuple<string, Action<string>>(key, a => c.SelectedIndex = a.ToNullableInt32().GetValueOrDefault(-1))); | |
if (action != null) _setters.Add(new Tuple<string, Action<string>>(key, action)); | |
} | |
public void Wire(string name, CheckBox c, Action<string> action = null) | |
{ | |
if (c == null) throw new ArgumentNullException("c"); | |
Page.Trace.Write("NH.Wire", c.ID); | |
var key = name ?? Key(c); | |
c.CheckedChanged += (o, args) => AddHistoryValue(key, c.Checked.ToString()); | |
_setters.Add(new Tuple<string, Action<string>>(key, a => c.Checked = a.ToNullableBool().GetValueOrDefault(false))); | |
if (action != null) _setters.Add(new Tuple<string, Action<string>>(key, action)); | |
} | |
public void Wire(string name, RadButton c, Action<string> action = null) | |
{ | |
if (c == null) throw new ArgumentNullException("c"); | |
Page.Trace.Write("NH.Wire", c.ID); | |
var key = name ?? Key(c); | |
c.Click += (o, args) => AddHistoryValue(key, "Clicked"); | |
if (action != null) _setters.Add(new Tuple<string, Action<string>>(key, action)); | |
} | |
public void Wire(string name, RadTextBox c, Action<string> action = null) | |
{ | |
if (c == null) throw new ArgumentNullException("c"); | |
Page.Trace.Write("NH.Wire", c.ID); | |
var key = name ?? Key(c); | |
c.TextChanged += (o, args) => AddHistoryValue(key, c.Text); | |
_setters.Add(new Tuple<string, Action<string>>(key, a => c.Text = a)); | |
if (action != null) _setters.Add(new Tuple<string, Action<string>>(key, action)); | |
} | |
public void Wire(string name, RadTabStrip c, Action<string> action = null) | |
{ | |
if (c == null) throw new ArgumentNullException("c"); | |
Page.Trace.Write("NH.Wire", c.ID); | |
var key = name ?? Key(c); | |
c.TabClick += (o, args) => AddHistoryValue(key, c.SelectedIndex.ToString()); | |
_setters.Add(new Tuple<string, Action<string>>(key, a => c.SelectedIndex = a.ToNullableInt32().GetValueOrDefault(-1))); | |
if (action != null) _setters.Add(new Tuple<string, Action<string>>(key, action)); | |
} | |
public void Wire(string name, RadGrid c) | |
{ | |
if (c == null) throw new ArgumentNullException("c"); | |
Page.Trace.Write("NH.Wire", c.ID); | |
var keyZ = (name ?? Key(c)) + "_z"; | |
var keyS = (name ?? Key(c)) + "_s"; | |
var keyP = (name ?? Key(c)) + "_p"; | |
c.PageIndexChanged += (o, args) => AddHistoryValue(keyP, args.NewPageIndex.ToString()); | |
c.PageSizeChanged += (o, args) => AddHistoryValue(keyZ, args.NewPageSize.ToString()); | |
c.SortCommand += (o, e) => AddHistoryValue(keyS, e.NewSortOrder.ToString() + "|" + e.SortExpression); | |
_setters.Add(new Tuple<string, Action<string>>(keyP, a => c.CurrentPageIndex = a.ToNullableInt32().GetValueOrDefault(0))); | |
_setters.Add(new Tuple<string, Action<string>>(keyZ, a => c.PageSize = a.ToNullableInt32().GetValueOrDefault(25))); | |
_setters.Add(new Tuple<string, Action<string>>(keyS, a => SetSort(c, a))); | |
} | |
private static void SetSort(RadGrid radGrid, string s) | |
{ | |
string[] parts = s.Split('|'); | |
radGrid.MasterTableView.SortExpressions.AddSortExpression(new GridSortExpression { FieldName = parts[1], SortOrder = StringExtensions.ChangeType<GridSortOrder>(parts[0]) }); | |
radGrid.Rebind(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment