简体   繁体   English

使用FindControl时,对象引用未设置为对象的实例

[英]Object reference not set to an instance of an object when using FindControl

I am using the following code. 我使用以下代码。

.ascx file: .ascx文件:

<div class="DemoArea">
    <asp:Button ID="btnCaseComplete" runat="server" Text="Case Complete" CssClass="btn_contentlist"
        onclick="btnCaseComplete_Click" OnClientClick="scroll(0,0);$.loading({mask: true, effect: 'ellipsis update'});"/>
       <ComponentArt:Dialog ID="caDropDownDialog" runat="server" Modal="true" Alignment="MiddleCentre" AllowDrag="true" AllowResize="false" AnimationDuration="1000" 
                            CloseTransition="Fade"  RenderOverWindowedObjects="true" ShowTransition="Fade" AnimationType="Outline" CssClass="ModalMask">
                            <Header><p class="header">Case Complete</p></Header>
                            <Content>
                                <asp:Panel ID="panSelectArea" runat="server" CssClass="modalMaskContent">

                                   <p><span class="red">Please Note:</span>Once you click
                                        <span class="bold">Ok</span>, your 
                                            case will be Submitted to ACR and you will not be able to edit the Case again. 

                                            <span class="style2">To continue editing the case, click </span>
                                            <span class="bold">Cancel</span>. You will be taken back 
                                            to the Case Wizard and your case will not be submitted to ACR.</p>
                                  </asp:Panel>
                            </Content>
                            <Footer>
                              <center class="modalMaskFooter">
                                    <asp:Button ID="btnOK" runat="server" CausesValidation="false" 
                                        CssClass="btn_contentlist" OnClientClick="caDropDownDialog.IsShowing=false" Text="OK" />
                                    <asp:Button ID="btnCancel" runat="server" CausesValidation="false" 
                                        CssClass="btn_contentlist" OnClientClick="caDropDownDialog.Close();" Text="Cancel" />
                                </center>
                            </Footer>
                        </ComponentArt:Dialog>
    </div>

Code behind: 代码背后:

CaseContentList obj = new CaseContentList(); // creating the object of case content list control to this page

        LinkButton lbtn = (LinkButton)((DataList)obj.FindControl("dlstContentList")).FindControl("lbtnDisplay");
        if (lbtn.Text == "Final Page")
        {
            caDropDownDialog.IsShowing = true;
        }
        else
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Window", "alert('Add Final Page First.');", true);

But it's giving an error 'object reference not set to an instance of an object'. 但是它给出了一个错误'对象引用未设置为对象的实例'。

It is caused by result of one of "FindControl()" method. 它是由“FindControl()”方法之一的结果引起的。 The result is NULL and not instance of control. 结果为NULL,而不是控件实例。

You must check for null: 您必须检查null:

bool found = false;
var dlstContentList = obj.FindControl("dlstContentList");
if ( null != dlstConentList ) {
   var lbtnDisplay = dlstContentList.FindControl("lbtnDisplay");
   found = (null != lbtnDisplay);
}

if ( found ) {
   // ... do something
}
else {
  // ... do something else
}
CaseContentList cl = (CaseContentList)this.Parent.TemplateControl.FindControl("ContentList");
        if(cl.IsFinalPage)
            caDropDownDialog.IsShowing = true;
        else
          Page.ClientScript.RegisterStartupScript(this.GetType(), "Window", "alert('Please add Final Page to complete your case');", true);

If it is a DataList and you are looking for a LinkButton, then I would expect you would need to loop through each ListItem within the DataList for the one you want, then find the control in that ListItem 如果它是一个DataList并且您正在寻找一个LinkBut​​ton,那么我希望您需要遍历DataList中的每个ListItem以获得所需的那个,然后在该ListItem中找到该控件

To loop through your ListItems within the DataList you could do this 要遍历DataList中的ListItem,您可以执行此操作

foreach (DataListItem dataListItem in obj.Items) {

if (dataListItem .ItemType == ListItemType.AlternatingItem | dl.ItemType == ListItemType.Item) {
        //
        // find the control here.
}
}

暂无
暂无

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

相关问题 e.Item.FindControl抛出未设置为对象实例的对象引用 - e.Item.FindControl throws Object reference not set to an instance of an object 在 grid.FindControl 期间出现错误“对象引用未设置为对象的实例” - Error 'Object reference not set to an instance of an object' during grid.FindControl 使用会话对象时,对象引用未设置为对象的实例 - Object reference not set to an instance of an object when using a session object 使用参数时,对象引用未设置为对象的实例 - object reference not set to an instance of an object when using parameter 使用自定义模型绑定时,对象引用未设置为对象的实例 - Object reference not set to an instance of an object when using custom model binding 使用Report.DataSourceConnections时:“对象引用未设置为对象的实例” - When using Report.DataSourceConnections: “Object reference not set to an instance of an object” 使用 HttpPostedFileBase[] 发布文件时未将对象引用设置为对象的实例 - Object reference not set to an instance of an object when posting files using HttpPostedFileBase[] 使用Logging Service时,对象引用未设置为对象的实例 - Object reference not set to an instance of an object when using Logging Service 使用包含时,对象引用未设置为LINQ中对象的实例 - Object reference not set to an instance of an object in LINQ when using contains GridView-&gt;从SQL进行数据绑定。 FindControl对象引用未设置为RowDataBound中的实例 - GridView -> Databinding from SQL. FindControl object reference not set to an instance in RowDataBound
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM