What's the problem with Page_Load?

iieixr

New Member
Hi,<BR><BR>I have a simple UserControl that queries a DB and displays the query result in a DropDownList. The page holding the UserControl should have access to the value of the selected item in this DropDownList via a Public Property of the UserControl called "PostedBy" (all code below).<BR><BR>The code seems to work fine if I access the .PostedBy property of the UserControl in an "onclick" event handler subroutine. But if I try to access this property (using the same code!) in Page_Load I get the following error:<BR> <BR> Object reference not set to an instance of an object.<BR><BR>My code seems fine to me, I can't work it out! Hope someone can tell me what's going on!<BR><BR>Thanks in advance,<BR><BR>JON<BR><BR><BR><BR><BR>*****************************************<BR>MAINPAGE.aspx - displays the User Control<BR>*****************************************<BR><BR><BR><BR><%@ Page Language="VB" %><BR><%@ Import Namespace="System.Data" %><BR><%@ Import Namespace="System.Data.Oledb" %><BR><BR><%@ Register TagPrefix="PostedByDDL" TagName="UserControl" src=http://aspmessageboard.com/archive/index.php/"POSTEDBY.ascx" %><BR><BR><BR><script language="VB" runat="server"><BR><BR><BR>Sub Page_Load(obj as object, e as EventArgs)<BR><BR> '***********************************<BR> '* THIS DOES NOT WORK IN PAGE_LOAD *<BR> '***********************************<BR> Dim iExampleInteger as Integer = ucPostedByDDL.PostedBy<BR> Response.Write("iExampleInteger is :" & iExampleInteger)<BR><BR><BR> If not Page.IsPostBack<BR> DataBind() <BR> end if<BR><BR><BR>end sub<BR><BR><BR>Sub Cheese(obj as object, e as eventargs)<BR> '****************************************<BR> '* BUT IT WORKS FINE IN THIS SUBROUTINE *<BR> '****************************************<BR> Dim iExampleInteger as Integer = ucPostedByDDL.PostedBy<BR> Response.Write("iExampleInteger is :" & iExampleInteger)<BR>End sub<BR><BR><BR></script><BR><BR><BR><html><BR><form runat="server"><BR><BR> PostedBy: <PostedByDDL:UserControl id="ucPostedByDDL" runat="server" /><BR> <BR><BR> <asp:LinkButton runat="server" OnClick="Cheese" Text="Cheese" /><BR><BR></form><BR></html><BR><BR><BR><BR><BR><BR>*****************************************<BR>POSTEDBY.ascx - a simple User Control<BR>*****************************************<BR><BR><BR><%@ Control Language="VB" %><BR><%@ Import Namespace="System.Data" %><BR><%@ Import Namespace="System.Data.Oledb" %><BR><BR><script language="VB" runat="server"><BR><BR>Dim objDatabase as new BusinessObjects.Database<BR>Dim ds as new DataSet()<BR><BR><BR>Sub Page_Load(obj as object, e as eventargs)<BR><BR> objDatabase.ConnectionString = ConfigurationSettings.AppSettings("ConnectionString")<BR> objDatabase.DataSet = ds<BR> <BR> objDatabase.CreateDataTable("tblPostedBy", "SELECT * FROM tblPeople;")<BR> ddPostedBy.DataSource = ds.Tables("tblPostedBy")<BR> ddPostedBy.DataTextField = "People"<BR> ddPostedBy.DataValueField = "PersonID"<BR> <BR> If not Page.IsPostBack then<BR> ddPostedBy.DataBind()<BR> ddPostedBy.Items.FindByText("Smith, Mike").Selected = true<BR> end if<BR><BR>end sub<BR><BR><BR><BR>Public Property PostedBy As Integer<BR> Get<BR> Return ddPostedBy.SelectedItem.Value <BR> End Get<BR> Set<BR> ddPostedBy.SelectedItem.Selected = false<BR> ddPostedBy.Items.FindByValue(value).Selected = true<BR> End Set<BR>End Property<BR><BR> <BR></script><BR><BR><BR> <asp:DropDownList runat="server" id="ddPostedBy" />You are just saying DataBind(), to use databind you need to specify an object. object.DataBind(), etc. What you are you trying to accomplish? Usercontrols should normally be used much like includes were used in asp.Hi James,<BR><BR>You're right, the code snippet:<BR><BR> If not Page.IsPostBack <BR> DataBind() <BR> end if <BR><BR>wasn't doing anything, but deleting it doesn't solve my "Object reference not set to an instance of an object" error!<BR><BR><BR>As to what I'm trying to accomplish - the UserControl in the line:<BR><BR><PostedByDDL:UserControl id="ucPostedByDDL" runat="server" /><BR><BR>is essentially a DropDownList that is populated from a DB query. Since it takes about 5/6 lines of code to do the query, set up the DDL DataSource, DataTextField and DataValueField, and since the same DDL will appear in many pages, I set it up as a UserControl.<BR><BR>This UserControl is thus nothing more than a query, the DropDownList to be populated by the query, and a public property or two so that I can manipulate selected items in the UserControl just as you can in a normal DropDownList.<BR><BR>It's so simple, how can it be going wrong?? This error's driving me nuts, so any help much appreciated!<BR><BR>Thanks,<BR><BR>JONI see you dim the database as businessobject.database, is this a namespace you need to import?Yes, that just allows the line:<BR><BR>objDatabase.CreateDataTable("tblPostedBy", "SELECT * FROM tblPeople;") <BR><BR>which does nothing more than save a line or two of code when you create a DataTable (the connection and the adapter are in the business object).<BR><BR>I'm sure that's not causing my error!<BR><BR>JONJust in case, here's the business object code: <BR><BR>public function CreateDataTable(DataTableName as String, strQuery as string) as DataTable<BR> try<BR> objConn = new OleDbConnection(ConnectionString)<BR> objAdapter = New OleDbDataAdapter(strQuery, objConn)<BR> objAdapter.Fill(DataSet, DataTableName)<BR> return DataSet.Tables(DataTableName)<BR> catch ex as OleDbException<BR> return nothing<BR> end try<BR> end function<BR><BR>It seems to work fine everywhere else, so it really is unlikely (though I guess not impossible) to be the source of the error.<BR><BR>JON<BR>Is it on the same page? If not you need to import the namespace to use it. Does the error not give you a line number?Hi James,<BR><BR>The error does indeed give a line number - it points to this line as the culprit:<BR><BR>Dim iExampleInteger as Integer = ucPostedByDDL.PostedBy <BR><BR>The weird thing is that this EXACT SAME line works perfectly in the subroutine "Cheese" (the "onclick" handler), whereas in Page_Load it produces the error message:<BR><BR>Object reference not set to an instance of an object.<BR> at ASP.PostedBy.get_PostedBy()<BR><BR>The fact that the same line works fine when it is put on another part of the page suggests to me that the error probably isn't anything to do with the code in the business object (but I stand to be corrected!).<BR><BR>I'm guessing that in some way the UserControl isn't loaded into the page yet when it is being referenced from Page_Load - does that make any kind of sense? I only have a hazy idea of the order these events happen in...<BR><BR>JON<BR>Hi All,<BR><BR>I have the answer, but don't know why it works! In MAINPAGE.aspx, if I put the code:<BR><BR> Dim iExampleInteger as Integer = ucPostedByDDL.PostedBy<BR> Response.Write("iExampleInteger is :" & iExampleInteger)<BR><BR>in Page_PreRender instead of Page_Load, it works as planned. Putting it in Page_Init, which I also tried, does NOT work.<BR><BR>Does anybody out there have an explanation for this?<BR><BR>JONThe page events go in this order:<BR>init, load, and prerender<BR><BR>So the user control must not be proccesed until the load stage, thus why you cannot access it until the prerender stage.Aha, the light dawns... <BR><BR>Do you happen to have any reference to tutorials etc about the order of events in a page? I'd like to learn about this a bit more systematically, rather than finding out AFTER wrestling with an error for a whole day!<BR><BR>Thanks for the help,<BR><BR>JONTry this one out:<BR>http://www.15seconds.com/issue/020102.htm
 
Back
Top