自动回发重置我的列表

发布于 2024-10-16 17:31:08 字数 949 浏览 3 评论 0原文

我想将项目存储到列表中,但每次我按下确认按钮将一项添加到列表中时,页面都会刷新并重置我的列表。我如何阻止这种情况发生,同时有一个添加按钮来继续添加到列表中?

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
    }
    else
    {
        slist = (List<Shopping>)ViewState["Items"];
    }
}

列表:

private static List<Shopping> slist;
        public List<Shopping> GetShopping()
        {
            return slist;
        } 

按钮 点击:

slist = new List<Shopping>(); 
            Shopping s = new Shopping();
            s.Item1 = txtItem1.Text;
            s.Item2 = txtItem2.Text;
            s.Item3 = txtItem3.Text;
            s.Item4 = txtItem3.Text;

            slist.Add(s);
            ViewState["Items"] = slist;
            showShopping();

方法:

showShopping()
{
GridView1.DataSource = GetShopping();
            GridView1.DataBind();
}

I want to store items to a list but everytime i press the confirm button to add one item to the list the page will refresh and reset my list. How do i stop this from happening whilst at the same time have an add button to keep adding to the list?

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
    }
    else
    {
        slist = (List<Shopping>)ViewState["Items"];
    }
}

List:

private static List<Shopping> slist;
        public List<Shopping> GetShopping()
        {
            return slist;
        } 

Button Click:

slist = new List<Shopping>(); 
            Shopping s = new Shopping();
            s.Item1 = txtItem1.Text;
            s.Item2 = txtItem2.Text;
            s.Item3 = txtItem3.Text;
            s.Item4 = txtItem3.Text;

            slist.Add(s);
            ViewState["Items"] = slist;
            showShopping();

Method:

showShopping()
{
GridView1.DataSource = GetShopping();
            GridView1.DataBind();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如果没有 2024-10-23 17:31:08

停止在页面加载时构建/绑定列表而不检查 Page.IsPostBack

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
       // bind / load lists and controls here
    }
    else
    {
       //this is a post back, don't reload everything
    }
}

编辑 *

如果您想一次支持超过 1 个购物者,切勿执行此操作。

private static List<Shopping> slist; 

用户最终会重写彼此的列表,然后不断地显示错误的列表。删除“静态”修饰符。

Stop building/binding the list on your page load without checking Page.IsPostBack

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
       // bind / load lists and controls here
    }
    else
    {
       //this is a post back, don't reload everything
    }
}

Edit *

Never do this if you ever want to support more then 1 shopper at a time.

private static List<Shopping> slist; 

The users will end up writing over each others lists, then displaying the wrong one constantly. Remove the 'static' modifier.

坏尐絯℡ 2024-10-23 17:31:08

不要将业务对象存储在 ViewState 中。使用会话

private List<Shopping> ShoppingList
{
   get { 
          var shopping = Session["Shopping"] as List<Shopping>;
          if (shopping == null)
          {
              shopping = new List<Shopping>();
              Session["Shopping"] = shopping;
          }
          return shopping;
       }
   set { Session["Shopping"] = value; }
}

按钮处理程序

var shopping = ShoppingList;
shopping.Add(new Shopping());
...

Do not store business objects in ViewState. Use the Session

private List<Shopping> ShoppingList
{
   get { 
          var shopping = Session["Shopping"] as List<Shopping>;
          if (shopping == null)
          {
              shopping = new List<Shopping>();
              Session["Shopping"] = shopping;
          }
          return shopping;
       }
   set { Session["Shopping"] = value; }
}

Button handler

var shopping = ShoppingList;
shopping.Add(new Shopping());
...
一抹淡然 2024-10-23 17:31:08

自从我完成 WebForms 以来已经有一段时间了,但看起来您正在分配多个项目,但按钮单击事件上只有一个“购物”,然后调用:

showShopping()
{
GridView1.DataSource = GetShopping();
GridView1.DataBind();

如果您希望显示项目而不是绑定到的 Shopping 对象,请仔细检查您的 GridView1 绑定

Been awhile since I have done WebForms, but it looks like you are assigning multiple items, but only one "Shopping" on the button click event then calling:

showShopping()
{
GridView1.DataSource = GetShopping();
GridView1.DataBind();
}

Double check your GridView1 binding if you want the items to be displayed versus the Shopping object that you are binding to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文