从数据库中获取所选列表视图项行

时间:2014-03-07 13:52:33

标签: c# asp.net listview

我有一个绑定到数据库的列表视图:

//C# code
protected void Page_Load(object sender, EventArgs e)
    {
        DataSet1.ProductDataTable pTable = new ProductTableAdapter().GetDataByCategory();

        productListview.DataSource = pTable.Rows;
        productListview.DataBind();
    }

当我点击链接按钮时,我想从所选择的数据库中获取所有列。

    //ASP.NET
    <asp:Listview ID="productListview" runat="server" GroupItemCount="3">
    <LayoutTemplate>
        <table runat="server">
            <tr>
              <th colspan="3">PRODUCTS LIST</th>
            </tr>
            <tr runat="server" id="groupPlaceholder" />
        </table>
    </LayoutTemplate>
    <GroupTemplate>
      <tr>
        <td runat="server" id="itemPlaceholder" />
      </tr>
    </GroupTemplate>
    <ItemTemplate>
                <td>
                    <img src='<%# Eval("PicURL") %>' width="150" height="150"/><br />
                    <asp:LinkButton 
                        ID="chosenLinkButton" runat="server" 
                        CommandName="AddProduct"
                        CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID") %>' 
                        Text='<%#Eval("ProductName") %>'><br />
                    <asp:Label ID="LinkButton1" runat="server" Text='<%#Eval("Price") %>'></asp:Label><br />
                    <asp:Label ID="Label1" runat="server" Text='<%#Eval("Size") %>'></asp:Label><br />
                    <asp:Label ID="Label2" runat="server" Text='<%#Eval("Description") %>'></asp:Label><br />
                    <asp:Label ID="Label3" runat="server" Text='<%#Eval("SalesQuantity") %>'></asp:Label> 
                </td>
    </ItemTemplate>
</asp:Listview>

在此之后我想获取行值并将它们放在一个名为“cartItems”的类中 我有价格,名称,描述等属性,如下所示:

private string ProductName;
    public string _ProductName
    {
        get { return ProductName; }
        set
        {
            ProductName = value;
        }
    }

下面更新! 这项工作。它从数据库中获取价格,我做错了什么?

        protected void productListview_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        //Made a adapter to database cause I thought I could get the selected talbe colums from here...
        DataSet1.ProductDataTable productTable = new ProductTableAdapter().GetDataByCategory();

        if (e.CommandName == "AddProduct")
        {
            //create a cartItem instance that contains ProductName, UnitPrice, type & decription
            var currentPrimaryKey = Convert.ToInt32(e.CommandArgument);

            CartItem chosenItem = new CartItem();
            chosenItem.UnitPrice = (decimal)productTable.Columns.IndexOf("Price");
                //how can I add the price from the database here? 

        }
    }

2 个答案:

答案 0 :(得分:0)

将您的代码连接到ListView

上的ItemCommand事件

使用类似于以下链接的代码;

 <asp:LinkButton ID="lnkEdit" runat="server" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID") %>'
                                    CommandName="Select" > <%# Convert.ToString(Eval("Description")) %></asp:LinkButton>

答案 1 :(得分:0)

解决方案

 foreach (DataSet1.ProductRow item in pTable.Rows)
            {
                //if the current item in the table is matching the item that is selected by the user
                if (item.ID == Convert.ToInt32(e.CommandArgument)) 
                {
                    //then assign values to a created instance of a cartItem (a class that I have)
                    CartItem currentItem = new CartItem();
                    currentItem.UnitPrice = item.Price;  
                }
            }

<强>结论: 使用Convert.ToInt32()方法将abject转换为整数。