c#请问为什么要有if (postPanel != null)?这是一个检测登陆,登录时postPanel出现,就可以发表信息了。

protected void CheckLogin()
{
Panel postPanel = (Panel)ContentPlaceHolder1.FindControl("PostPanel");

if (Session["UserName"] == null)
{
MessageLabel.Text = "";
LoginPanel.Visible = true;
if (postPanel != null)
{
postPanel.Visible = false;
}
LogoutButton.Visible = false;
}
else
{
LoginPanel.Visible = false;
LogoutButton.Visible = true;
MessageLabel.Text = "Welcome, " + Session["UserName"];
if (postPanel != null)
{
postPanel.Visible = true;
}
}
}

是为了容错,以防遇到Panel的实例postPanel为空的情况,因为FindControl()的结果可能为null;
如果遇到这种意外情况,发表信息的地方无法显示,用户自然无法发表。
温馨提示:答案为网友推荐,仅供参考
相似回答