Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: What's the problem with Page_Load?


Message #1 by "Jon Maz" <jonmaz@s...> on Mon, 13 May 2002 12:19:30
Hi,

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).

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:

Object reference not set to an instance of an object.

My code seems fine to me, I can't work it out!  Hope someone can tell me 
what's going on!

Thanks in advance,

JON




*****************************************
MAINPAGE.aspx - displays the User Control
*****************************************



<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Oledb" %>

<%@ Register TagPrefix="PostedByDDL" TagName="UserControl" 
src="POSTEDBY.ascx" %>


<script language="VB" runat="server">


Sub Page_Load(obj as object, e as EventArgs)

'***********************************
'* THIS DOES NOT WORK IN PAGE_LOAD *
'***********************************
Dim iExampleInteger as Integer = ucPostedByDDL.PostedBy
Response.Write("iExampleInteger is :" & iExampleInteger)


   If not Page.IsPostBack
   	DataBind()		
   end if


end sub


Sub Cheese(obj as object, e as eventargs)
'****************************************
'* BUT IT WORKS FINE IN THIS SUBROUTINE *
'****************************************
   Dim iExampleInteger as Integer = ucPostedByDDL.PostedBy
   Response.Write("iExampleInteger is :" & iExampleInteger)
End sub


</script>


<html>
<form runat="server">

   PostedBy: <PostedByDDL:UserControl id="ucPostedByDDL" runat="server" />
   <br>
   <asp:LinkButton runat="server" OnClick="Cheese" Text="Cheese" />

</form>
</html>





*****************************************
POSTEDBY.ascx - a simple User Control
*****************************************


<%@ Control Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Oledb" %>

<script language="VB" runat="server">

Dim objDatabase as new BusinessObjects.Database
Dim ds as new DataSet()


Sub Page_Load(obj as object, e as eventargs)

   objDatabase.ConnectionString = ConfigurationSettings.AppSettings
("ConnectionString")
   objDatabase.DataSet = ds
   
   objDatabase.CreateDataTable("tblPostedBy", "SELECT * FROM tblPeople;")
   ddPostedBy.DataSource = ds.Tables("tblPostedBy")
   ddPostedBy.DataTextField = "People"
   ddPostedBy.DataValueField = "PersonID"
   
   If not Page.IsPostBack then
   	ddPostedBy.DataBind()
   	ddPostedBy.Items.FindByText("Smith, Mike").Selected = true
   end if

end sub



Public Property PostedBy As Integer
   Get
   	Return ddPostedBy.SelectedItem.Value			
   End Get
   Set
   	ddPostedBy.SelectedItem.Selected = false
   	ddPostedBy.Items.FindByValue(value).Selected = true
   End Set
End Property


</script>


<asp:DropDownList runat="server" id="ddPostedBy" />
Message #2 by "Minh T. Nguyen" <nguyentriminh@y...> on Mon, 13 May 2002 10:17:10 -0700
Hi Jon,

	I usually set up code in a different code-behind file, but I
have noticed that you have put your code in line. I am not quite sure,
but can it be that when the page is loaded for the first time, ASP.NET
doesn't know what the ucPostedByDDL control is, because the script is
loaded/read in chronologically first before the control comes? This is
just a wild guess, though.
	Try putting your code in a code-behind file or move the script
part to the bottom.

Hope this works,
Minh.

-----Original Message-----
From: Jon Maz [mailto:jonmaz@s...] 
Sent: Monday, May 13, 2002 12:20 PM
To: aspx_beginners
Subject: [aspx_beginners] What's the problem with Page_Load?


Hi,

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).

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:

Object reference not set to an instance of an object.

My code seems fine to me, I can't work it out!  Hope someone can tell me

what's going on!

Thanks in advance,

JON




*****************************************
MAINPAGE.aspx - displays the User Control
*****************************************



<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Oledb" %>

<%@ Register TagPrefix="PostedByDDL" TagName="UserControl" 
src="POSTEDBY.ascx" %>


<script language="VB" runat="server">


Sub Page_Load(obj as object, e as EventArgs)

'***********************************
'* THIS DOES NOT WORK IN PAGE_LOAD *
'***********************************
Dim iExampleInteger as Integer = ucPostedByDDL.PostedBy
Response.Write("iExampleInteger is :" & iExampleInteger)


   If not Page.IsPostBack
   	DataBind()		
   end if


end sub


Sub Cheese(obj as object, e as eventargs)
'****************************************
'* BUT IT WORKS FINE IN THIS SUBROUTINE *
'****************************************
   Dim iExampleInteger as Integer = ucPostedByDDL.PostedBy
   Response.Write("iExampleInteger is :" & iExampleInteger)
End sub


</script>


<html>
<form runat="server">

   PostedBy: <PostedByDDL:UserControl id="ucPostedByDDL" runat="server"
/>
   <br>
   <asp:LinkButton runat="server" OnClick="Cheese" Text="Cheese" />

</form>
</html>





*****************************************
POSTEDBY.ascx - a simple User Control
*****************************************


<%@ Control Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Oledb" %>

<script language="VB" runat="server">

Dim objDatabase as new BusinessObjects.Database
Dim ds as new DataSet()


Sub Page_Load(obj as object, e as eventargs)

   objDatabase.ConnectionString = ConfigurationSettings.AppSettings
("ConnectionString")
   objDatabase.DataSet = ds
   
   objDatabase.CreateDataTable("tblPostedBy", "SELECT * FROM
tblPeople;")
   ddPostedBy.DataSource = ds.Tables("tblPostedBy")
   ddPostedBy.DataTextField = "People"
   ddPostedBy.DataValueField = "PersonID"
   
   If not Page.IsPostBack then
   	ddPostedBy.DataBind()
   	ddPostedBy.Items.FindByText("Smith, Mike").Selected = true
   end if

end sub



Public Property PostedBy As Integer
   Get
   	Return ddPostedBy.SelectedItem.Value			
   End Get
   Set
   	ddPostedBy.SelectedItem.Selected = false
   	ddPostedBy.Items.FindByValue(value).Selected = true
   End Set
End Property


</script>


<asp:DropDownList runat="server" id="ddPostedBy" />

Message #3 by "Jon Maz" <jonmaz@s...> on Mon, 13 May 2002 20:02:38
Hi Minh,

Thanks for your reply.  Swapping the order of Page_Load and the Cheese 
subroutine (I'm starting to regret choosing a silly name for it now!) 
makes no difference, I get the same error...

I haven't tried putting it in a code-behind file yet, but I am not 
hopeful!  After all, the same line of code works perfectly in the Cheese 
subroutine, so the code itself is OK, it's just something about being in 
Page_Load that is messing things up.

My suspicions chime with yours, that in some way (which I don't 
understand) the UserControl hasn't been loaded into the page yet when it 
is referenced from Page_Load.

Thanks for the help so far,

JON
Message #4 by "Jon Maz" <jonmaz@s...> on Mon, 13 May 2002 22:05:40
Hi All,

I have the answer, but don't know why it works!  In MAINPAGE.aspx, if I 
put the code:

	Dim iExampleInteger as Integer = ucPostedByDDL.PostedBy
	Response.Write("iExampleInteger is :" & iExampleInteger)

in Page_PreRender instead of Page_Load, it works as planned.  Putting it 
in Page_Init, which I also tried, does NOT work.

Does anybody out there have an explanation for this?

JON

  Return to Index