Reputation: 13444
I have a web control (ascx). This control has some database connections which perform selects based on a member variable (e.g., shows details based on a certain database ID).
What works: I can add the web control to a page in the designer, set the member variable representing the ID in the Page_Load variable.
What does not: I am trying to add the web control (several, actually) to a panel during the Page_Load. When I do this, the SqlDataSource's on the web control are null.
This is how I am trying to add the web controls.
foreach (DataRow dr in dv.Table.Rows)
{
Label header = new Label();
header.Text = "Blah blah blah";
Panel1.Controls.Add(header);
DetailsControl widgetDetails = new DetailsControl();
widgetDetails.WidgetID = (Int64)dr[1];
Panel1.Controls.Add(widgetDetails);
}
This is the error I get, which is coming from the DetailsControl
.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 19: protected void Page_Load(object sender, EventArgs e)
Line 20: {
Line 21: DataView dv = (DataView)SqlDataSourceFilter.Select(DataSourceSelectArguments.Empty);
EDIT: Actually, it appears that all controls within the DetailsControl are null, but only hwen added dynamically. I must need to call some function to trigger their initialization.
Upvotes: 1
Views: 246
Reputation: 27352
Where do you do: foreach (DataRow dr in dv.Table.Rows)?
Try to move this to the Page_Init, that should help.
Upvotes: 1
Reputation: 28325
A long shot, since I don't have access to your source code...
But maybe you haven't properly constructed your objects? Where is the SqlDataSourceFilter object being constructed? Init method ? Class constructor?
Upvotes: 1