簡體   English   中英

如何從動態用戶控件在內容頁面上查找控件

[英]How to FindControl on content page from dynamic User Control

嘗試在按鈕上執行FindControl時,出現空引用異常。 我有一個購物車設置,其中有一個基於母版頁的內容頁(.aspx)。 在內容頁面上,有一個占位符控件,我在其中動態添加用戶控件(每個產品一個控件,每個控件中都有一個“添加到購物車”按鈕)。

當用戶單擊按鈕將商品添加到購物車時,我可以成功地將其添加到購物車,然后計算購物車中的商品數量,如果數量超過1,則嘗試顯示“結帳”按鈕,或者如果沒有顯示購物車將其隱藏。

我正在使用FindControl,但得到的是空引用錯誤。 為什么找不到結帳按鈕? 我當前的代碼如下:

母版頁(template.Master):

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="template.master.cs" Inherits="OUWP.template" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head runat="server"></head>
<body>
    <form id="form1" runat="server">
        <asp:ContentPlaceHolder ID="m_cph_body" runat="server"></asp:ContentPlaceHolder>
    </form> 
</body>
</html>

內容頁(shop.aspx)

<%@ Page Title="" Language="C#" MasterPageFile="~/template.Master" AutoEventWireup="true" CodeBehind="shop.aspx.cs" Inherits="OUWP.shop" %>
<%@ MasterType VirtualPath="~/template.Master" %>

<asp:Content ID="Content2" ContentPlaceHolderID="m_cph_body" runat="server">
    <asp:PlaceHolder ID="Catalogue" runat="server">
        <!-- this is where the user controls are dynamically generated-->
    </asp:PlaceHolder>
    <asp:Panel ID="pnl_Basket" runat="server">
        <div>
            <asp:LinkButton ID="lbtnCheckout" runat="server">Continue to Checkout</asp:LinkButton>
        </div>
    </asp:Panel>
</asp:Content>

用戶控制頁(PurchasableProduct.ascx)

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PurchasableProduct.ascx.cs" Inherits="OUWP.CustomControls.PurchasableProduct" %>

<asp:UpdatePanel ID="udpBody" runat="server">
    <ContentTemplate>
        <asp:Panel ID="pnlPurchasableProduct" runat="server">
            <asp:LinkButton ID="lbtnAddLine" runat="server" OnClick="lbtnAddLine_Click"></asp:LinkButton>
        </asp:Panel>
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="lbtnAddLine" />
    </Triggers>
</asp:UpdatePanel>

用戶控制代碼隱藏(PurchasableProduct.ascx.cs)

protected void lbtnAddLine_Click(object sender, EventArgs e)
{
    // try to find checkout button on parent page of control
    System.Web.UI.Page page = (System.Web.UI.Page)this.Page;
    LinkButton Target1 = (LinkButton)page.FindControl("lbtnCheckout");

    // count the items in the cart (saved in session variable)
    DataTable dt = (DataTable)Session["varDataTableCart"];
    Int32 ItemCount = 0;
    Int Line Quantity = 0;
    foreach (DataRow dr in dt.Rows)
    {
        Int32 LineQuantity = Convert.ToInt32(dt.Rows[dt.Rows.IndexOf(dr)]["Quantity"].ToString());
        ItemCount = ItemCount + LineQuantity;
    }

    // if 1 or more items in cart, try to make button visible
    if (ItemCount > 0)
    {
        Target1.Visible = true;
    }
    // otherwise no items in cart, so try to hide checkout button
    else
    {
        Target1.Visible = false;
    }
}

我還嘗試使用下面的代碼通過母版頁進行操作,但這也不起作用:

MasterPage mp1 = (MasterPage)page.Master;
LinkButton Target1 = (LinkButton)mp1.Page.FindControl("lbtnCheckout");

好的,所以我靠運氣嘗試了這段代碼,它起作用了,找到了我的按鈕。 使用當前控件的“父級”。

LinkBut​​ton Target1 =(LinkBut​​ton)this.Parent.FindControl(“ lbtnCheckout”);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM