"加入购物车" DataList ASP.NET中的按钮

时间:2015-01-23 10:02:08

标签: c# asp.net call postback datalist

我需要在Data-list中添加添加到购物车按钮。

问题是,当我点击btn 添加按钮时,永远不会达到Datalist1_ItemCommand(),但会发生回发事件

我需要添加一个按钮来读取当前项目值&对它执行一些操作

ASP代码

    <asp:DataList ID="DL_Products" runat="server" RepeatColumns="3" OnItemCommand="Datalist1_ItemCommand">    
    <ItemTemplate>
        <div class="Item">
            <div class="title">
                <asp:Label ID="lbl_Brand" runat="server" Text='<%# Eval("Brand") %>'></asp:Label>                    
            </div>                
            <div class="Info">  
                Price/Piece :
                <asp:Label ID="lbl_Price" runat="server" Text='<%# Eval("Price") %>'></asp:Label>
                Rs
                <br />
                <asp:Button ID="btn" CommandName="AddtoCart" runat="server" Text="Add" />
            </div>    
        </div>
    </ItemTemplate>
</asp:DataList>

C#/ Code Behind:

protected void Datalist1_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName == "AddtoCart")
    {
        Label l1 = (Label)e.Item.FindControl("lbl_Price");
        string a = l1.Text;
        Response.Write(l1.Text);
    }
}


protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        load_User_data();
        Brand_SelectedIndexChanged(null, null);
        load_data();
    }
}

1 个答案:

答案 0 :(得分:-1)

在页面加载时添加if(!ispostback){bind datalist}