Monday, March 3, 2025

Get Validation Error Message, Page.Isvaild False Section

if you wanted to know the route cause of error message then we can write below code in aso.net 

 foreach (BaseValidator validator in Page.Validators)

            {

                if (validator.Enabled && !validator.IsValid)

                {

                    // Put a breakpoint here

                    string clientID = validator.ClientID;

                    string aa = validator.ErrorMessage;

                }

            }

Monday, February 24, 2025

Session Kill on Page Refresh OR On Page/Browser refresh session kill in asp.net with c#

Create a Global Variable

bool IsPageRefresh = false;

Write Below Code on Page Load

  #region To Maintain Refresh event
  //Section of code checks if the page postback is due to genuine submit by user 
  //or by pressing "refresh" or F5
  if (!IsPostBack)
  {
       //Session.Abandon(); Session.Clear();
       ViewState["ViewStateId"] = System.Guid.NewGuid().ToString();
       Session["SessionId"] = ViewState["ViewStateId"].ToString();
  }
  else
  {
      if (ViewState["ViewStateId"] != null && Session["SessionId"] != null)
        {
          if (ViewState["ViewStateId"].ToString() != Session["SessionId"].ToString())
            {
               IsPageRefresh = true;
            }
        }

        Session["SessionId"] = System.Guid.NewGuid().ToString();
        ViewState["ViewStateId"] = Session["SessionId"].ToString();
    } 
    #endregion 

Check Global variable where you made the condition like on events

    #region To Maintain Refresh event
     if (IsPageRefresh)
        {
          Session.Abandon(); Session.Clear();
        }
    #endregion

    if (Session["MyFlag"] == null) Response.Redirect("invalid.aspx?msg=Session expired. ", true);