Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用FindControl时未将对象引用设置为对象的实例_C#_Asp.net - Fatal编程技术网

C# 使用FindControl时未将对象引用设置为对象的实例

C# 使用FindControl时未将对象引用设置为对象的实例,c#,asp.net,C#,Asp.net,我正在使用以下代码 .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: '

我正在使用以下代码

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

但是它给出了一个错误“对象引用未设置为对象的实例”。

如果它是一个数据列表,并且您正在寻找一个链接按钮,那么我希望您需要在数据列表中的每个列表项中循环找到所需的列表项,然后在该列表项中找到控件

要在DataList中循环查看ListItems,可以执行以下操作

foreach (DataListItem dataListItem in obj.Items) {

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

它是由“FindControl()”方法之一的结果引起的。结果为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
}

这意味着您不是错误地强制转换它,就是没有引用正确的对象。请粘贴您的aspx代码。我认为您的
FindControl
语句之一失败。在使用已建立的控件之前,尝试添加null检查。在哪个事件上调用此代码?请告诉我循环如何通过loopcheck编辑的答案找到它。循环浏览DataList DataListItems情况相同。您可以在创建控件之前运行代码。控件是在页面的“初始化”阶段创建的。
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);