Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: Can't set UserControl property


Message #1 by "Jon Maz" <jonmaz@s...> on Tue, 14 May 2002 01:48:59
Hi,

I am building an aspx Admin page consisting essentially of a series of 
TextBoxes and DropDownLists. This Admin page will do two different things, 
depending on what it finds in the QueryString: either (a) input new data 
into an Access DB or (b) pull existing data out of the DB and display it, 
so it can be edited and updated.  

When attempting to do (b) I am having problems setting the properties of a 
DropDownList which is part of a UserControl, and I'm hoping someone can 
help me find out why it isn't working.

The DDL in question is part of UserControl "PostedByDDL.ascx" (all code 
below).  This simple UserControl consists of a DropDownList, the DB query 
to populate it, and a Public Property allowing the DDL's selected item to 
be both read and set from the page holding the UserControl.  

This PostedByDDL.ascx UserControl is itself part of another 
UserControl, "AddMessageTemplate.ascx".  The relevant part of this second 
UserControl is the "Public Sub DisplayExistingMessageDetails", in which I 
set the Public Property of the PostedByDDL UserControl:

	ucPostedByDDL.PostedBy = Number

The AddMessageTemplate.ascx UserControl is itself displayed in 
MainPage.aspx.  In the Page_Load event of this aspx page, 
the "DisplayExistingMessageDetails" Subroutine is called.  Theoretically, 
this should pass an integer (in this case, "3") to the 
AddMessageTemplate.ascx UserControl, which then uses this integer to set 
the PostedBy property of the PostedByDLL.ascx UserControl.  Ultimately 
this integer should then select a particular item in the DDL which 
comprises the UI element of PostedByDLL.ascx.

Well, that's what's supposed to happen.  What in fact happens is that I 
get the following error message:

        Object reference not set to an instance of an object.
          at ASP.PostedByDDL.set_PostedBy(Int32 Value)


I have no idea why an object is not being found here.  The hierarchy of 
controls is clear; there is a DDL in the lowest UserControl, 
PostedByDDL.ascx.  This UserControl is properly registered in the higher 
UserControl AddMessageTemplate.ascx.  This higher UserControl is in turn 
properly registered in the holding MainPage.aspx.

So why on earth is this error message coming up????

I've spent ages trying to work this one out, and my one conclusion is that 
I don't know what's going on!  I really hope someone out there has the 
patience to look at my code (I've tried to simplify it as much as possible 
without losing shape) and help me out here!

Thanks in advance,

JON



*****************
PostedByDDL.ascx
*****************

<%@ Control Language="VB" ClassName="PostedByDDL"%>
<%@ 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("Porritt, Jonathan").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" />




************************
AddMessageTemplate.ascx
************************


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

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


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

        Public Sub DisplayExistingMessageDetails(Number as Integer)
        	ucPostedByDDL.PostedBy = Number
		'++++++++++++++++++++++++++
		' ERROR IS CAUSED HERE 
		'++++++++++++++++++++++++++
        End Sub
			
</script>


<PostedByDDL:UserControl id="ucPostedByDDL" runat="server" />





**************
MAINPAGE.aspx
**************


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

<%@ Register TagPrefix="UserControl" TagName="Message" 
src="AddMessageTemplate.ascx" %>


<script language="VB" runat="server">
	
Sub Page_Load(sender as object, e as eventargs)
	ucMessage.DisplayExistingMessageDetails(3)
End Sub


</script> 

<html>
<form runat="server" name="form1" id="form1">
	
	<UserControl:Message id="ucMessage" runat="server" />

</form> 
</html>




Message #2 by "Jon Maz" <jonmaz@s...> on Wed, 15 May 2002 14:58:13
Hi All, 

In case anyone's interested, I solved the problem eventually.  In 
MainPage.aspx, I simply replaced: 

Sub Page_Load(sender as object, e as eventargs) 
   ucMessage.DisplayExistingMessageDetails(3) 
End Sub 

with: 

Sub Page_PreRender(sender as object, e as eventargs) 
   ucMessage.DisplayExistingMessageDetails(3) 
End Sub 

and it worked!  What a relief after about two days of debugging hell! 


JON 

  Return to Index