我无法将数据添加到datalist列表中请帮帮我 我想转移一些产品在其他页面进行比较 当我在页面加载中添加值时,它可以工作,但在转发器或数据列表中不起作用
这是我的班级
public class CAR
{
private int carid;
private string title;
public CAR(int carid, string title)
{
this.carid = carid;
this.title = title;
}
public int CARID
{
get
{
return carid;
}
}
public string TITLE
{
get
{
return title;
}
}
}
这是html方面
<asp:DataList ID="DataList1" OnItemCommand="DataList1_ItemCommand" runat="server" DataKeyField="id" DataSourceID="SqlDataSource1">
<ItemTemplate>
id:
<asp:Label Text='<%# Eval("id") %>' runat="server" ID="idLabel" /><br />
title:
<asp:Label Text='<%# Eval("title") %>' runat="server" ID="titleLabel" /><br />
<asp:Button ID="Button1" runat="server" CommandName="compare" Text="Button" />
<br />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:takyabConnectionString %>' SelectCommand="SELECT * FROM [tbl_ad]"></asp:SqlDataSource>
<asp:Button ID="Button2" OnClick="Button2_Click" runat="server" Text="Button" />
它是代码隐藏
ArrayList value = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
Session.Add("v", value);
Response.Redirect("webform2.aspx");
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "compare")
{
value.Add(new CAR(1,"ok"));
}
}
答案 0 :(得分:0)
您的代码中的问题是,在第一个按钮单击时,您要将值添加到ArrayList,其中在回发期间不保存状态。这意味着,在Button2_Click事件中,数组列表将始终为空。
更改您的值属性。或者将Arraylist值保存到DataList1_ItemCommand事件本身的会话中
ArrayList value
{
get
{
ArrayList values = null;
if(ViewState["selectedValues"] != null)
values = (ArrayList)ViewState["selectedValues"];
else
{
values = new ArrayList();
ViewState["selectedValues"] = values;
}
return values;
}
}