Wednesday 14 October 2015

How to clear the values entered in the textbox by pressing F5 or Refreshing the page

Hi guys, today I will show you how to clear the textbox value when you press F5, This will also prevent you from submission of data when you press F5.

First create a design page like this
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       First Name :  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
       Last Name : <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
       <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>

Now write the following code in aspx.cs page

protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            Session["Submitted"] = false;
        }
        if(Convert.ToBoolean(Session["submitted"].ToString())==true)
        {
            TextBox1.Text = string.Empty;
            TextBox2.Text = string.Empty;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["Submitted"] = true;
    }

No comments:

Post a Comment