UserControl with Gridview: Set DataSource: NullReferenceException

O

OJ

Hi,
I have a WebUserCOntrol with a GridView and I am attempting to set the
DataSource of the Gridview in the User control from the parent ASPX
page. I have the following property in the user control...

public DataTable DataSourceTable
{
get
{
return this.gvwMain.DataSource as DataTable;
}
set
{
this.gvwMain.DataSource = value;
}
}


Where gvwMain is the name of the gridview. However, when I try to set
the datasource I get a NullReferenceException saying that gvwMain (the
gridview) is Null....what am I missing then? In 2.0 using VWD2005...

Thanks,
Oli
 
S

Shaun McDonnell

In what part of the page event process are you accessing this property?

Shaun McDonnell
 
O

OJ

Hi Shaun,
Thanks..I create a new instance of the UserCOntrol like this..

UserControls_SABSHolidays holidayTable = new
UserControls_SABSHolidays();

Try to set the datasource....(gvwMainDS is a datatable with 100 rows)

holidayTable.DataSourceTable = gvwMainDS;

which calls the code in my first post....

Then add it to the pages content holder...
Master.MiddleContent.Controls.Add(holidayTable);

O
 
O

OJ

Hi,
when the ContentPlaceHolder already has the UserControl this code
works. It is only when I try to add it programmatically....

I guess that means it has something to do with the Page lifecycle, but
I dont really know where to start looking. When to the server controls
in an ASCX Usercontrol become initialized. Can I overrride the New
method perhaps? Anyone?

Thanks
O
 
T

Thiru.net

hi oli,
i guess you tried to create a user control as you do in windows
application.

webuser controls are different from windows user controls.

though you assign datatable value to the datagrid datasource, when you
access the datatable assigned, it will say NullReferenceException only.
because when you submit your parent again, the webcontrol loses the
datagrid's datasource value.

so you have the store in Datatable which you assign in ViewState for
the value to be persist ed throughout all the page submits.
 
O

OJ

Hi,
The data is new. The user control caches the table before it trys to
set the datasource of the gridview. The data is all there and present.
It seems that the GridView in the UserCOntrol has not been initialized
when I call a new instance of the UserControl. The usercontrol exists
as does the reference to the gridview but it seems the gridview itself
has not yet been initialised. Does anybody know when server controls
inside a WebUserControl are actually instantiated?

Thanks,
Oli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top