0

I am able to create controls pro grammatically using the code below without issue:

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

However, I would like to change the control so that I can give it a constructor like this:

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

and then I should be able to call the control like this:

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

However, when I do this I always get an error that my in page controls within my FileListReader Control have not been instantiated and I get a null reference error. So for example I have an <asp:Label></asp:label> control that errors out when I try to set it's text on the Page_Load method. What is causing this? I figured the base.Construct() would have solved this issue but it obviously has not.

2 Answers 2

1

The proper way to inherit a constructor is like this:

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

Does changing your constructor like that fix the issue?

Sign up to request clarification or add additional context in comments.

4 Comments

I still get an error Object reference not set to an instance of an object. for the same reason, even with the :base()
your first code snippet, where you create and add the control... what method are you doing that in?
I am doing it from Page_Load
Also, my class is public partial class FileListReader : System.Web.UI.UserControl. I don't know if that makes a difference.
0

I am not sure that calling base.Contruct() is what you should be doing, try calling the default contructor of the base class example below:

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

1 Comment

I still get an error Object reference not set to an instance of an object. for the same reason, even with the :base()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.