我无法将数据添加到datalist中的列表中,请帮助我,当我在页面中添加价值时,我想转移一些产品在其他页面中进行比较,加载它工作,但在中继器或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"));
}
}
发布于 2014-06-16 03:04:19
代码中的问题是,在第一次单击按钮时,会将值添加到ArrayList,其状态在回发期间不会保存。这意味着,在Button2_Click事件中,数组列表将始终为空。
更改value属性,如下所示。或者将数组列表值保存到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;
}
}
https://stackoverflow.com/questions/24231299
复制相似问题