Tuesday, August 31, 2010

How to stop page refreshing in asp.net

step1)Declare variable
private bool _refreshState;
private bool _isRefresh;
step2)Add below code into your .cs file or load the below two methods

protected override void LoadViewState(object savedState)
{
object[] AllStates = (object[])savedState;
base.LoadViewState(AllStates[0]);
_refreshState = Convert.ToBoolean(AllStates[1].ToString());
_isRefresh = (_refreshState == Convert.ToBoolean(Session["__ISREFRESH"].ToString()));
}
protected override object SaveViewState()
{
Session["__ISREFRESH"] = _refreshState;
object[] AllStates = new object[3];
AllStates[0] = base.SaveViewState();
AllStates[1] = !(_refreshState);
return AllStates;
}

step3) add code to your button

if (!(_isRefresh))
{
code
}