Asp.net Can';t访问列表视图中的控件

Asp.net Can';t访问列表视图中的控件,asp.net,Asp.net,无法访问listview中的控件 错误 对象引用未设置为对象的实例 .cs public void lnkaddtocart_Command(Object sender, CommandEventArgs e) { ((TextBox)ListView_ProductDetails.FindControl("txtbox_pqty")).Visible = false; } .aspx <asp:ListView runat="server" ID="ListView_Prod

无法访问listview中的控件

错误

对象引用未设置为对象的实例

.cs

public void lnkaddtocart_Command(Object sender, CommandEventArgs e)
{
  ((TextBox)ListView_ProductDetails.FindControl("txtbox_pqty")).Visible = false;   
}
.aspx

<asp:ListView runat="server" ID="ListView_ProductDetails">
     <LayoutTemplate>
        <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
     </LayoutTemplate>
     <ItemTemplate>
              <div class="qty">
                  Qty:
                  <asp:TextBox ID="txtbox_pqty" Text="1" runat="server"/>
                  <input type="hidden" name="product_id" size="2" value="41" />
                       <asp:LinkButton ID="lnkaddtocart" runat="server" 
                            CommandArgument='<%#Eval("pid") %>' 
                            OnCommand="lnkaddtocart_Command"  
                            cssclass="button">
                               <span>Add to Cart</span>
                        </asp:LinkButton>
                  </div>
    </ItemTemplate>
</asp:ListView>

数量:
添加到购物车

您正在项目模板中使用文本框,因此将有多个文本框(每个项目一个)。话虽如此,Listview将不知道应该获取哪些文本框

您必须在单击链接按钮的特定行上查找文本框

例如:

public void lnkaddtocart_Command(Object sender, CommandEventArgs e)
{
    var item = ((Control)sender).NamingContainer as ListViewItem;
    if (item != null)
    {
        ((TextBox)item.FindControl("txtbox_pqty")).Visible = false;
    }
}

您需要在
文本框
NamingContainer
上使用
FindControl
,它是而不是
列表视图
。因此,您可以使用
LinkButton的
属性来查找


您要查找的控件实际上存在于ListView产品详细信息的重复元素中。要找到控件,需要遍历控件层次结构

让我们从这里开始,用你的方法。首先要做的是获取对包含按钮的ListViewItem的引用。在这些.NET事件签名中,sender引用引发事件的控件

public void lnkaddtocart_Command(Object sender, CommandEventArgs e)
{
    // Attempt to cast sender to a LinkButton
    LinkButton originator = sender as LinkButton;

    // Check that we've found it
    if ( originator != null )
    {
       // Now traverse the control hierarchy to get a ListViewItem
       var parentItem = originator.Parent as ListViewItem;

       if ( parentItem != null 
            && parentItem.ItemType == ListViewItemType.DataItem)
       {
          var textBox = parentItem.FindControl("txtbox_pqty") as TextBox;

          if ( textBox != null )
          {
             textBox.Visible = false;
          }
       }
    }
}
除了“Mario”之外,在listview中添加如下事件:

<asp:ListView runat="server" ID="ListView_ProductDetails" onitemcommand="lnkaddtocart_Command">
    <LayoutTemplate>
        <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
    </LayoutTemplate>
    <ItemTemplate>
        <div class="qty">
            Qty:
            <asp:TextBox ID="txtbox_pqty" text='<%#Eval("pid") %>' runat="server" />
            <input type="hidden" name="product_id" size="2" value="41" />
            <asp:LinkButton ID="lnkaddtocart" runat="server" text='<%#Eval("pid") %>' CommandArgument='<%#Eval("pid") %>'
            cssclass="button"><span>Add to Cart</span></asp:LinkButton>
        </div>
    </ItemTemplate>
</asp:ListView>

内联代码:

<asp:ListView runat="server" ID="ListView_ProductDetails" DataSourceID="SqlDataSource1"
            OnItemCommand="ListView_ProductDetails_ItemCommand">
            <LayoutTemplate>
                <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
            </LayoutTemplate>
            <ItemTemplate>
                <div>
                    Qty:
                    <asp:TextBox ID="txtbox_pqty" Text="1" runat="server" />
                    <input type="hidden" name="product_id" size="2" value="41" />
                    <asp:LinkButton ID="lnkaddtocart" CommandName="addtocart" runat="server" CommandArgument='<%#Eval("pid") %>'
                        CssClass="button"><span>Add to Cart</span></asp:LinkButton>
                </div>
            </ItemTemplate>
 </asp:ListView>

希望这有帮助。

OP处理
LinkButton的
ItemCommand
事件而不是ListView的ItemDataBound。@TimSchmelter您是对的,尽管我只是想向OP展示一个在ListView中如何常用FindControl的示例,但我可以更改它来显示如何在ItemCommand中使用它。我刚才提到它是因为这是两个完全不同的事件,也是使用FindControl的两种不同方式(ItemDataBound已隐式地为每个listviewitem调用,而RowCommand仅为该LinkButton调用)。;)@TimSchmelter是的,对不起,应该更具体一些。我更新了代码示例,现在使用NamingContainer。
public void lnkaddtocart_Command(object sender, ListViewCommandEventArgs e)
    {
        TextBox txt = (TextBox)e.Item.FindControl("txtbox_pqty");
        txt.Visible = false;
    }
<asp:ListView runat="server" ID="ListView_ProductDetails" DataSourceID="SqlDataSource1"
            OnItemCommand="ListView_ProductDetails_ItemCommand">
            <LayoutTemplate>
                <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
            </LayoutTemplate>
            <ItemTemplate>
                <div>
                    Qty:
                    <asp:TextBox ID="txtbox_pqty" Text="1" runat="server" />
                    <input type="hidden" name="product_id" size="2" value="41" />
                    <asp:LinkButton ID="lnkaddtocart" CommandName="addtocart" runat="server" CommandArgument='<%#Eval("pid") %>'
                        CssClass="button"><span>Add to Cart</span></asp:LinkButton>
                </div>
            </ItemTemplate>
 </asp:ListView>
protected void ListView_ProductDetails_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "addtocart")
        {
            ((TextBox)e.Item.FindControl("txtbox_pqty")).Visible = false;
        }
    }