Skip to content

Instantly share code, notes, and snippets.

@DTTerastar
Created December 27, 2012 21:18
Show Gist options
  • Save DTTerastar/4392065 to your computer and use it in GitHub Desktop.
Save DTTerastar/4392065 to your computer and use it in GitHub Desktop.
public class EnsureDataBound : Control
{
private int _count;
public bool AllowMultiple { get; set; }
public bool EveryPostBack { get; set; }
public When When { get; set; }
public override void DataBind()
{
if (!AllowMultiple && _count > 0) return;
_count++;
base.DataBind();
}
public void ReDataBind()
{
_count++;
base.DataBind();
}
protected override void OnInit(EventArgs e)
{
if (When == When.Init && (EveryPostBack || !Page.IsPostBack)) DataBind();
base.OnInit(e);
}
protected override void OnLoad(EventArgs e)
{
if (When == When.Load && (EveryPostBack || !Page.IsPostBack)) DataBind();
base.OnLoad(e);
}
protected override void OnPreRender(EventArgs e)
{
if (When == When.PreRender && (EveryPostBack || !Page.IsPostBack)) DataBind();
base.OnPreRender(e);
}
}
public enum When
{
Load,
Init,
PreRender
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment