listview项目据说是" Null"

时间:2016-03-02 02:59:17

标签: c# listview

我有一个下面的列表视图..... enter image description here

我正在尝试根据 ProductID

读取数量可用

我想阅读listview中的数据然后如果Quantity > Available

我会查看错误

现在,当我这样做时,这些项目 Null

这是ASPnet:

<asp:ListView ID="lvCart" runat="server" onitemcommand="lvCart_ItemCommand">
    <ItemTemplate>
    <tr>
    <td>
         <asp:Label ID="lbproductid" runat="server"
            class="form-control" Width="50"
            Text='<%# Eval("ProductID") %>' />
    </td>
    <td>
        <%# Eval("Name") %>
        <asp:Literal ID="ltRefNo" runat="server"
            Text='<%# Eval("RefNo") %>' Visible="false" />
    </td>
    <td>
        Php <%# Eval("Price", "{0: #,###.00}") %>
    </td>
    <td>
        <asp:TextBox ID="txtQty" runat="server"
            class="form-control" type="number"
            min="1" max="99" MaxLength="2" Width="70"
            Text='<%# Eval("Quantity") %>' required />

            <asp:Label ID="avail" runat="server"
            class="form-control" Width="50"
            Text='<%# Eval("Available") %>' />
    </td>

代码背后1: (这是我试过的)

void getavail()
{
    Label lbproductid = (Label)lvCart.FindControl("lbproductid");
    Label avail = (Label)lvCart.FindControl("avail");
    //ITemplate ID = (ITemplate)lvCart.FindControl("ProductID");
    TextBox txtQty = (TextBox)lvCart.FindControl("txtQty");

    if (int.Parse(txtQty.Text) > int.Parse(avail.Text))
    {
        error.Visible = true;
    }
    con.Close();
 }

代码落后于2: (第二次试验)

foreach (ListViewDataItem dataitem in lvCart.Items)
{
    Label lbproductid = (Label)lvCart.FindControl("lbproductid");
    Label avail = (Label)lvCart.FindControl("avail");
    //ITemplate ID = (ITemplate)lvCart.FindControl("ProductID");
    TextBox txtQty = (TextBox)lvCart.FindControl("txtQty");

    if (int.Parse(txtQty.Text) > int.Parse(avail.Text))
    {
        error.Visible = true;
    }
    con.Close();
}

尝试第二个代码时,它不会读取代码。从这一行开始:

Label lbproductid = (Label)lvCart.FindControl("lbproductid"); 

然后它会指向最后一个括号,关闭();

我将此GetAvail放在页面加载事件中,因为我希望它立即显示。

帮助我这几个小时。提前谢谢

0 个答案:

没有答案