简体   繁体   English

中继器内的占位符

[英]Placeholder inside Repeater

I have a repeater and inside that I have a placeholder which should load a different control in to it depending on what comes from the database. 我有一个中继器,并且在内部有一个占位符,应根据数据库中的内容将不同的控件加载到其中。 However, it's falling over on page load and I can't see why. 但是,它已经超出了页面加载的范围,我不知道为什么。

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

The code is: 代码是:

protected void rptTabs_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    DataRowView nRow = null;

    switch (e.Item.ItemType)
    {
        case ListItemType.Item:
        case ListItemType.AlternatingItem:
            nRow = (DataRowView)e.Item.DataItem;
            String NavURL = "" + nRow["ProdTab"];
            NavURL = NavURL.Replace(" ", "");
            NavURL = NavURL.Replace("+", "");
            ((HyperLink)e.Item.FindControl("lnkTabs")).Text = "" + nRow["ProdTab"];
            ((HyperLink)e.Item.FindControl("lnkTabs")).NavigateUrl = "#" + NavURL;
            PlaceHolder PlaceHolder1 = (PlaceHolder)e.Item.FindControl("PlaceHolder1");
            switch (NavURL)
            {
                case "Fire":
                    var uc = LoadControl("~/controls/Fire.ascx");
                    PlaceHolder1.Controls.Add(uc); 
                    break;
            }
            break;
    }
}

The errors on the line: 该行上的错误:

PlaceHolder1.Controls.Add(uc); 

The HTML is: HTML是:

 <asp:Repeater runat="server" id="rptSecondTab" 
               OnItemDataBound="rptSecondTab_ItemDataBound">
     <ItemTemplate>
         <div id="divIcon" ClientIDMode="Static" runat="server" class="tab-pane 
                           overflow-auto">
             <asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
         </div>
     </ItemTemplate>
 </asp:Repeater>

Edit: More info: 编辑:更多信息:

Source Error: 


Line 81:                         case "Fire":
Line 82:                             var uc = LoadControl("~/controls/Fire.ascx");
Line 83:                             PlaceHolder1.Controls.Add(uc);
Line 84:                             break;
Line 85:                     }

Source File: c:\Development\PIDs\PIDs\PIDs\SubPID.aspx.cs    Line: 83 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   PIDs.SubPID.rptTabs_ItemDataBound(Object sender, RepeaterItemEventArgs e) in c:\Development\PIDs\PIDs\PIDs\SubPID.aspx.cs:83
   System.Web.UI.WebControls.Repeater.OnItemDataBound(RepeaterItemEventArgs e) +111
   System.Web.UI.WebControls.Repeater.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem) +138
   System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource) +9546651
   System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +61
   System.Web.UI.WebControls.Repeater.DataBind() +105
   PIDs.SubPID.Setup_Tabs(String Tabs) in c:\Development\PIDs\PIDs\PIDs\SubPID.aspx.cs:61
   PIDs.SubPID.Setup_SPID() in c:\Development\PIDs\PIDs\PIDs\SubPID.aspx.cs:51
   PIDs.SubPID.Page_Load(Object sender, EventArgs e) in c:\Development\PIDs\PIDs\PIDs\SubPID.aspx.cs:29
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +92
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

I believe you are running into a limitation of the out-of-the-box .FindControl() method, where it will not look for controls past its own naming container. 我相信您会遇到开箱即用的.FindControl()方法的局限性,在该方法中,它不会通过自己的命名容器来查找控件。 In other words, .FindControl() does not recursively descend the control hierarchy to children, grand-children, etc. 换句话说, .FindControl()不会将控件层次结构递归地.FindControl()为子代,大.FindControl()代等。

Try using this recursive version instead: 请尝试使用此递归版本:

public static class ControlExtensions
{
    public static Control FindControlRecursive(this Control theControl,
                                               string theControlId)
    {
        if (theControl == null)
        {
            return null;
        }

        // Try to find the control at the current level
        var theControlToBeFound = theControl.FindControl(theControlId);

        if (theControlToBeFound == null)
        {
            // Search the children
            foreach (Control theChildControl in theControl.Controls)
            {
                theControlToBeFound = FindControlRecursive(theChildControl,
                                                           theControlId);

                if (theControlToBeFound != null)
                {
                    break;
                }
            }
        }

        return theControlToBeFound;
    }
}

Usage: 用法:

var PlaceHolder1 = e.Item.FindControlRecursive("PlaceHolder1") as PlaceHolder;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM