gpt4 book ai didi

ASP.NET 私​​有成员字段在回发时丢失值

转载 作者:行者123 更新时间:2023-12-02 09:48:24 26 4
gpt4 key购买 nike

考虑以下代码:

    public partial class TeacherControlPanel : System.Web.UI.Page
{
protected string username = string.Empty;

protected void Page_Load(object sender, EventArgs e)
{
username = (string)Request.QueryString["username"];

Ice_Web_Portal.BO.Teacher teacher = Ice_Web_Portal.BO.Teacher.GetTeacherByUsername(username);

if (teacher != null)
{
labUsername.Text = username;
labName.Text = teacher.TeacherName;
labTeacherCode.Text = teacher.TeacherCode;

Dept dept = teacher.Department;

if (dept != null)
{
labDepartment.Text = dept.DeptName;
}
}
else
{
//labErrorMessage.Text = "No teacher found";
}
}

protected void btnSendMail_Click(object sender, EventArgs e)
{
Response.Redirect(@"~/Teacher/TeacherComposeMail.aspx?username=mahabub" + username);
}
}

在此代码中,当我将“用户名”声明为私有(private)时,它会在后续回发后初始化为 null。

为什么?

secret 是什么?

最佳答案

因为 ASP.NET 是无状态的,这意味着它不会在回发之间保持状态。将用户保存到 View 状态、 session 或应用程序,以便在回发时查看它。

#region UserName
public string UserName
{
get
{
if (this.ViewState["UserName"] == null)
return string.Empty;

return (string)this.ViewState["UserName"];
}
set { this.ViewState["UserName"] = value; }
}
#endregion

关于ASP.NET 私​​有成员字段在回发时丢失值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1313697/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com