4

无法访问列表视图中的控件

错误

你调用的对象是空的。

。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>
4

5 回答 5

2

您在项目模板中使用文本框,因此将有多个文本框(每个项目一个)。话虽如此,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;
    }
}
于 2012-10-18T07:57:30.537 回答
2

您需要FindControlNamingContainerof theTextBox上使用,ListViewItem而不是ListView. 因此,您可以使用该LinkButton's NamingContainer属性来查找ListViewItem.

var ctrl = (Control) sender;
var lvi = (ListViewItem) ctrl.NamingContainer;
var txt = (TextBox)lvi.FindControl("txtbox_pqty");
txt.Visible = false; 
于 2012-10-18T08:01:29.427 回答
1

除了“马里奥”之外,在您的列表视图中添加事件,如下所示:

<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>

CS:

public void lnkaddtocart_Command(object sender, ListViewCommandEventArgs e)
    {
        TextBox txt = (TextBox)e.Item.FindControl("txtbox_pqty");
        txt.Visible = false;
    }
于 2012-10-18T08:12:57.730 回答
1

代码内联:

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

希望这可以帮助。

于 2012-10-18T08:20:52.967 回答
0

您要查找的控件实际上存在于 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;
          }
       }
    }
}
于 2012-10-18T08:02:12.567 回答