单击按钮时Asp.net属性值设置为零

单击按钮时Asp.net属性值设置为零,asp.net,Asp.net,在我的ASP.net页面上,我在page\u Load private int userid{ get; set;} if (!IsPostBack) { if (Session["userid"] != null) { userid= int.Parse(Session["userid"].ToString()); } } 在我的添加按钮点击事件中,我使用的是userid protected void btnAdd_Click(object sender,

在我的ASP.net页面上,我在
page\u Load

private int userid{ get; set;} 

if (!IsPostBack)
{
   if (Session["userid"] != null)
   {
       userid= int.Parse(Session["userid"].ToString());
   }
}
在我的添加按钮点击事件中,我使用的是
userid

protected void btnAdd_Click(object sender, EventArgs e)
{
  doSomething(userid);
}

最初,该值会被检索并存储在
userid
属性中,但在
btnAdd\u单击时,
userid
变为0。我将代码移出了
if(!IsPostBack)
块,这解决了问题,但让我想知道,在页面加载中设置了
userid
属性之后,是什么将其设置为
zero
。不清楚单击按钮是否会返回页面的新实例或返回什么?有什么指导吗?

您可以从页面加载中删除内容,因为数据在会话中,只需将属性更改为以下内容:

private int userid{ get{ return int.Parse(Session["userid"].ToString()); } }