.NETGURU
What's the problem with Page_Load?
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngreuse' list.


Jon Maz
-- Moved from [aspngfreeforall] to [aspngreuse] by Marcie Jones <Click here to reveal e-mail address> --

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" />

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.361 / Virus Database: 199 - Release Date: 07/05/2002

Reply to this message...
 
    
Jon Maz
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

PS Thanks to Marcie Jones, whose idea this actually was!

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.361 / Virus Database: 199 - Release Date: 07/05/2002

Reply to this message...
 
    
Chris Kinsman
I suspect the issue is that in Page_Init the user control hasn't had a
chance to load it's information out of ViewState or out of the postback
data. Depending on how the control loads it's information Page_Load
could also be to early. I think however this last one is dependent upon
the implementation of the user control.

-----Original Message-----
From: Jon Maz [mailto:Click here to reveal e-mail address]=20
Sent: Monday, May 13, 2002 2:08 PM
To: aspngreuse
Subject: [aspngreuse] Re: What's the problem with Page_Load?

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 =3D 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

PS Thanks to Marcie Jones, whose idea this actually was!

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.361 / Virus Database: 199 - Release Date: 07/05/2002

| [aspngreuse] member Click here to reveal e-mail address =3D YOUR ID
| http://www.aspfriends.com/aspfriends/aspngreuse.asp =3D JOIN/QUIT

Reply to this message...
 
 
System.Configuration.ConfigurationSettings
System.Data.DataSet
System.EventArgs
System.Web.UI.Page
System.Web.UI.UserControl
System.Web.UI.WebControls.DropDownList
System.Web.UI.WebControls.LinkButton
System.Windows.Forms.UserControl




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification