gpt4 book ai didi

c# - 在 ASP.NET 页面中存储 session 变量?

转载 作者:行者123 更新时间:2023-12-02 10:51:05 24 4
gpt4 key购买 nike

如何在 ASP.NET 页面上放置具有整个 session 范围的变量(我的意思是在 aspx 页面后面的类中)?是将变量放入 Session 对象的唯一方法吗?

例如:

public partial class Test_ : System.Web.UI.Page
{
private int idx = 0;

protected void Button1_Click(object sender, EventArgs e)
{
Button1.Text = (idx++).ToString();
}
}

我希望每次点击此按钮时我的索引都会上升。在不使用 Session 对象的情况下如何做到这一点?

最佳答案

您可以将其放在ViewState

public partial class Test_ : System.Web.UI.Page {

protected void Button1_Click(object sender, EventArgs e) {
if(ViewState["idx"] == null) {
ViewState["idx"] = 0;
}
int idx = Convert.ToInt32(ViewState["idx"]);

Button1.Text = (idx++).ToString();

ViewState["idx"] = idx;
}
}

关于c# - 在 ASP.NET 页面中存储 session 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2052427/

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