【问题标题】:add data to list with button in datalist or repeater使用数据列表或转发器中的按钮将数据添加到列表
【发布时间】:2014-08-05 13:07:13
【问题描述】:

我无法将数据添加到数据列表中,请帮助我 我想转移一些产品在其他页面进行比较 当我在页面加载中添加值时,它可以工作,但在转发器或数据列表中不起作用

这是我的课

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"));
        }
    }

【问题讨论】:

    标签: asp.net .net arrays list datalist


    【解决方案1】:

    您的代码中的问题是,在第一次单击按钮时,您将值添加到 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; 
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-06
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      • 2022-01-08
      相关资源
      最近更新 更多