2010-07-19 80 views
0

我能够创建控件亲们语法使用下面的代码没有问题:问题创建ASP.NET用户的实例编程控制

FileListReader fReader = (FileListReader)LoadControl("~/Controls/FileListReader.ascx"); 
phFileLists.Controls.Add(fReader); 

不过,我想改变控制,这样我可以给它构造函数是这样的:

public FileListReader(Int32 itemGroupId, Int32 documentType, String HeaderString, String FooterString, bool isAdminUser) 
{ 
    base.Construct(); 
    this.itemGroupId = itemGroupId; 
    this.documentType = documentType; 
    this.HeaderString = HeaderString; 
    this.FooterString = FooterString; 
    this.isAdminUser = isAdminUser; 
} 

,然后,我应该能够调用这样的控制:

FileListReader fReader = (FileListReader)LoadControl(typeof(FileListReader), new Object[] { itemGroupId, 6, "Sell Sheets", "<br /><br />", isAdminUser }); 

但是,当我这样做时,我总是得到一个错误,我在我的FileListReader控件中的页面控件没有被实例化,我得到一个空引用错误。因此,例如我有一个<asp:Label></asp:label>控件,当我尝试在Page_Load方法上设置它的文本时出错。这是什么造成的?我想base.Construct()会解决这个问题,但它显然没有。

回答

1

继承构造正确的方法是这样的:

class FileListReader : WebControl 
{ 
public FileListReader(Int32 itemGroupId, 
          Int32 documentType, 
          String HeaderString, 
          String FooterString, 
          bool isAdminUser) : base() // <-- notice the inherit 
{ 

    this.itemGroupId = itemGroupId; 
    this.documentType = documentType; 
    this.HeaderString = HeaderString; 
    this.FooterString = FooterString; 
    this.isAdminUser = isAdminUser; 
} 
    // ... other code here ... // 
} 

是否改变你的构造一样,解决这个问题?

+0

我仍然得到一个错误'对象引用未设置为实例一个对象。“出于同样的原因,即使使用:base() – 2010-07-19 14:49:15

+0

你的第一个代码片段,你创建和添加控件...你在做什么? – 2010-07-19 15:01:18

+0

我正在从'Page_Load'做它 – 2010-07-19 15:17:38

0

我不知道,调用base.Contruct()是你应该做的事情,尝试调用下面的基类实例的默认构造器:

public FileListReader(Int32 itemGroupId, Int32 documentType, String HeaderString, String FooterString, bool isAdminUser) :base() 
{ 
    base.Construct(); 
    this.itemGroupId = itemGroupId; 
    this.documentType = documentType; 
    this.HeaderString = HeaderString; 
    this.FooterString = FooterString; 
    this.isAdminUser = isAdminUser; 
} 
+0

即使使用:base(),我仍然会得到一个错误:“对象引用未设置为对象的实例。”出于同样的原因。 – 2010-07-19 14:48:43