【问题标题】:Remove an item from a List ASP.NET Webforms从列表中删除项目 ASP.NET Webforms
【发布时间】:2016-04-13 12:57:14
【问题描述】:

我有以下代码:

private List<Student> students;

public List<Student> Collection()
{
    students = new List<Student>
    {
        new Student {Id = 1, Name = "Name1"},
        new Student {Id = 2, Name = "Name2"},
        new Student {Id = 3, Name = "Name3"},
    };
    return students;   
}

protected void LinkButton1_OnClick(object sender, EventArgs e)
{
    var s = (sender as LinkButton).CommandArgument;
    var st = students.Find(c => c.Id == int.Parse(s));
    students.Remove(st);
    repeater.DataBind();
}

LinkButton1_OnClick 事件中,students 为空。为什么?我在 ASP.NET MVC 中有 similar issue,有人告诉我 MVC 是无状态的,我应该使用 session我的问题:ASP.NET Webforms 也是无状态的吗?这个程序通过使用 Sessions 工作,但是如果 ASP.NET Webforms 不是无状态的,为什么我应该在这个例子中使用 Sessions?

【问题讨论】:

  • 你在哪里调用 Collection?
  • @MarcusH...在中继器的 ObjectDataSource 中。
  • LinkButton1_OnClick 调用发生在回发事件中,其中IsPostBack == true,因此您的列表实际上并未在回发事件中填充。
  • @VitorRigoni 请告诉我 ASP.NET Webforms 是否是无状态的?我问了 4-5 个人这个问题,每个人都给了我不同的答案。我在哪里可以找到这方面的参考资料?

标签: c# asp.net webforms


【解决方案1】:

整个网络都是无状态的,除非你做一些事情来添加状态。在 Web 表单中,您可以使用 ViewState 或 Session。或者您可以回发列表所需的数据,或从数据库中重新查询。

请注意,不建议使用 ViewState,因为它会大大增加您的请求和响应的大小。

在代码上使用 ViewState 类似于 Session。

// add to viewstate
List<Student> students = Collection();
ViewState["students"] = students;

// later we can retrieve them
// cast is necessary because it comes back from the ViewState as an object
List<Student> students = (List<Student>)ViewState["students"];

请记住,在 Web 窗体中,每次访问页面时都会创建一个新的页面类实例。因此,字段和属性将失去您为它们设置的值。

【讨论】:

  • 谢谢。所以 ASP.NET Webforms 也是无状态的,我必须在这里使用 Sessions 或 ViewState 吗?没有别的办法吗?
  • @user5032790 是的,您每次都可以从数据库中检索值。或者缓存常用数据。或者将数据存储在表单中并在回发时重新检索它。您选择哪一个取决于数据来自谁以及数据有多大。
  • 再次感谢。我刚刚读到一个回答说 ASP.NET MVC 是无状态的,但 ASP.NET Webforms 不是。这就是我认为我不应该在这里使用 Sessions 的原因。
  • @user5032790 就像我说的 所有 网络都是无状态的,除非你做一些事情来添加状态。在 Web 窗体的情况下,您可以使用 ViewState,这可能就是他们说它是有状态的原因。但是请注意,大多数人并不推荐 ViewState(事实上,软件开发人员倾向于使用 MVC 而不是 Web 表单)。
【解决方案2】:

HTTP 协议是无状态的,其中仅使用请求中包含的信息创建响应。为了能够利用有状态协议的优势,通过 ASP.NET Webforms,引入了 Postback 等概念。两种协议都有优点和缺点。但是在这两种方式中,您都应该手动处理要在响应和请求之间保留的变量。

Stateful-stateless

Web Forms - MVC

【讨论】:

  • 谢谢。您能否告诉我如何在没有会话的情况下仅使用您所说的 Postback 之类的概念来做到这一点?
  • Postback 用于捕捉页面的状态。它不会帮助你保持你的变量。您必须自己将变量存储在内存中。请参阅the page life cycle,然后您应该决定哪种状态更方便您存储变量。
【解决方案3】:

所有HTTP协议都是无状态的。所以在asp.net中保存数据使用

  1. 会话状态
  2. 应用状态
  3. 饼干

Cookie 很方便,因为它在客户端机器上的浏览器上存储数据。Cookie 将数据存储在计划文本中存在一些安全问题。可以通过加密和解密 cookie 来克服。

如果你不使用会话,你可以在 cookie 中保存数据

 private List<Student> students;

        public List<Student> Collection()
        {
            students = new List<Student>
    {
        new Student {Id = 1, Name = "Name1"},
        new Student {Id = 2, Name = "Name2"},
        new Student {Id = 3, Name = "Name3"},
    };
            return students;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            List<Student> students = Collection();
           // ViewState["students"] = students;
            HttpCookie myCookie = new HttpCookie("MyTestCookie");
            DateTime now = DateTime.Now;
            JavaScriptSerializer serializer=new JavaScriptSerializer();
            var data = serializer.Serialize(students);
            // Set the cookie value.
            myCookie.Value = data;
            // Set the cookie expiration date.
            myCookie.Expires = now.AddHours(1);

            // Add the cookie.
            Response.Cookies.Add(myCookie);

            //Response.Write("<p> The cookie has been written.");
        }

并检索数据

 protected void LinkButton1_Click(object sender, EventArgs e)
        {
            HttpCookie myCookie = Request.Cookies["MyTestCookie"];
            IList<Student> persons = new JavaScriptSerializer()
                .Deserialize<IList<Student>>(myCookie.Value);

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-20
    • 1970-01-01
    • 2016-05-08
    • 2011-03-18
    • 2020-12-07
    • 2010-11-29
    • 2016-08-17
    相关资源
    最近更新 更多