|G|第三帖,Session和ViewState的效果不一样
cpp2017 木百胸
代码:用Session
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
Session[ "CanClick "] = true;
}
}
/// <summary>
/// Right
/// </summary>
/// <param name= "sender "> </param>
/// <param name= "e "> </param>
protected void Button1_Click(object sender, EventArgs e)
{
if ((bool)Session[ "CanClick "])
{
//do something
string strConn = @ "data source=admin\SQLEXPRESS;initial catalog=hexiesoft;persist security info=False;user id=sa;password=6503;packet size=4096;pooling=true; ";
string sQuery = "insert into testClick(i,memo) values (2, 'test ') ";
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sQuery, conn);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
Session[ "CanClick "] = false;
}
else
{
Response.Write( "不可多次点击! ");
}
}
========
没有问题 打死也执行不了2次!
然后我改成ViewState 如果点的快的话,就会执行多次!
为什么 ?
------解决方案--------------------因为viewstate在页面上存储 , session 存在服务器的内存中
点击多次时 viewstate 还没来得及提交
------解决方案--------------------