本文介绍了按刷新或退格键清除页面上的所有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在按下刷新或退格键时清除页面上的所有字段?

how to clear all the field on a page on pressing refresh or backspace?

推荐答案

public void ClearAll(form frm)
{
     foreach(Control ctl in frm.Controls)
     {
        if(ctl is Textbox)
          {
             ctl.text="";
          }
        if(ctl is checkBox)
          {
            ctl.selectedIndex=0;
          }
     }
}


protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["CheckRefresh"] = Server.UrlDecode(System.DateTime.Now.ToString());
        }
if (Session["CheckRefresh"].ToString() == ViewState["CheckRefresh"].ToString())
        {
            Label1.Text = "Hello";
            Session["CheckRefresh"] = Server.UrlDecode(System.DateTime.Now.ToString());
        }
        else
        {
            Label1.Text = "Page Refreshed";
            string str = "your code will save here or call save() method here";
//and here you can call clearallfields() method
        }


    }


protected void Page_PreRender(object sender, EventArgs e)
    {
        ViewState["CheckRefresh"] = Session["CheckRefresh"];
    }


这篇关于按刷新或退格键清除页面上的所有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 12:30