自动回发重置我的列表
我想将项目存储到列表中,但每次我按下确认按钮将一项添加到列表中时,页面都会刷新并重置我的列表。我如何阻止这种情况发生,同时有一个添加按钮来继续添加到列表中?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
停止在页面加载时构建/绑定列表而不检查 Page.IsPostBack
编辑 *
如果您想一次支持超过 1 个购物者,切勿执行此操作。
用户最终会重写彼此的列表,然后不断地显示错误的列表。删除“静态”修饰符。
Stop building/binding the list on your page load without checking Page.IsPostBack
Edit *
Never do this if you ever want to support more then 1 shopper at a time.
The users will end up writing over each others lists, then displaying the wrong one constantly. Remove the 'static' modifier.
不要将业务对象存储在 ViewState 中。使用会话
按钮处理程序
Do not store business objects in ViewState. Use the Session
Button handler
自从我完成 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.