How to check if Page is refreshed in ASP.Net

In this post, i have posted the code to detect if page is refreshed in asp.net. But before that, i would like to share the scenario where it is required.

Question
why there is need to detect if page refreshed in asp.net?.

Answer
Suppose you have a button "Save" on the page and on click of it, you are inserting a record in the database. Now if user click on Save, a record get inserted in the database but now if he/she refreshes the page, the Save Click event gets fire again and as a result same record get inserted again in the database. In order to handle this, we need to detect if page is refreshed, so that we can stop reinserting the record in the database.

PageRefresh.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageRefresh.aspx.cs" Inherits="PageRefresh" %>
<html>
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" />
    </div>
    </form>
</body>
</html>

PageRefresh.aspx.cs
using System;
using System.Web.UI;

public partial class PageRefresh : System.Web.UI.Page
{
    protected void Page_PreRender(object sender, EventArgs e)
    {
        ViewState["IsPageRefreshed"] = Session["IsPageRefreshed"];
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["IsPageRefreshed"] = Server.UrlDecode(System.DateTime.Now.ToString());
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Session["IsPageRefreshed"].ToString() == ViewState["IsPageRefreshed"].ToString())
        {
            Session["IsPageRefreshed"] = Server.UrlDecode(System.DateTime.Now.ToString());
            ScriptManager.RegisterClientScriptBlock(this, this.Page.GetType(), "abc", "alert('Save Clicked')", true);
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this, this.Page.GetType(), "abc", "alert('Page Refreshed')", true);
        }
    }
}

Split Function in SQL

The purpose of split function (table valued function) is to split a delimited value into multiple values based on delimiter and display in tabular form.

Split Function takes two parameters
  • Delimited Value: The delimited value to be split in multiple values.
  • Delimiter: The delimiter like comma, colon, semicolon, pipe etc. on the basis of which delimited value to be split.
and returns table of multiple values delimited on the basis of delimiter.

CREATE FUNCTION dbo.split(
    @delimited NVARCHAR(MAX),
    @delimiter NVARCHAR(100)
) RETURNS @t TABLE (id INT IDENTITY(1,1), val NVARCHAR(MAX))
AS
BEGIN
    DECLARE @xml XML
    SET @xml = N'<t>' + REPLACE(@delimited,@delimiter,'</t><t>') + '</t>'
    INSERT INTO @t(val)
    SELECT  r.value('.','varchar(MAX)') as item
    FROM  @xml.nodes('/t') as records(r)
    RETURN
END
Step wise explanation of split Function
  • Converting the delimited value into XML using replace function, where all delimiters are getting replaced with tag to make it as XML and assigning the same to XML Variable
  • Reading the Node from XML variable and storing the values in table variable @t.
  • Returning the table variable @t

Let's take an example to split a comma delimited value into multiple values on the basis of delimiter (comma) using split function.

SELECT * FROM dbo.split('val1,val2,val3', ',')


Now let's take another example where you have multiple delimited value stored in a table against an ID and each value needs to split on the basis of delimiter.

We would be using Cross Apply  clause in the example.

DECLARE @TAB TABLE(
    id int, list varchar(100)
)
INSERT INTO @TAB
SELECT 1, 'apple;banana;grapes;orange'
UNION ALL SELECT 2, 'potato;onion;carrot;brinjal'
SELECT * FROM @TAB
SELECT    t.id, s.val
FROM    @TAB t
CROSS APPLY dbo.split(t.list, ';') s
RESULT