Create a simple web page to count the number of times the current webpage is submitted to the server onclick event of a Button.

Program :


Default.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default14.aspx.cs" Inherits="Default14" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
 
    </div>
    </form>
</body>
</html>



Default.aspx.cs



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default14 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (ViewState["scount"] == null)
        {
            ViewState.Add("scount", 1);
            Response.Write("<h1>Page Submit Count is 1</h1>");
        }
        else
        {
            int x = int.Parse(ViewState["scount"].ToString());
            x++;
            ViewState["scount"] = x;
            Response.Write("<h1>Page Submit Count is " + x);
        }
    }
}

Comments