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

时间:2011-05-10 11:01:38

标签: 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: '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>

代码背后:

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

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

如果它是一个DataList并且您正在寻找一个LinkBut​​ton,那么我希望您需要遍历DataList中的每个ListItem以获得所需的那个,然后在该ListItem中找到该控件

要遍历DataList中的ListItem,您可以执行此操作

foreach (DataListItem dataListItem in obj.Items) {

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