Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: Why can't I find the control?


Message #1 by "Jon Maz" <jonmaz@s...> on Wed, 27 Mar 2002 13:28:49
Hi,

I am building a DataGrid with dynamic columns added to the grid in 
Page_Init.  As part of this, the EditItemTemplate is loaded into the 
DataGrid from a separate .ascx file via the Page.LoadTemplate method.

My problem is that I am having difficulties finding the server controls 
within this dynamically-added EditItemTemplate, (which I need to do to 
change their properties and add events dynamically).

On a non-dynamic TemplateColumn, I would simply use the following code to 
find a server control in an EditItemTemplate:

    sub dg_ItemDataBound(sender as object, e as DataGridItemEventArgs)
    If (e.Item.ItemType = ListItemType.EditItem) then
    Dim lbtExampleLinkButton as LinkButton = e.Item.FindControl
("lbtExampleLinkButton")
    lbtExampleLinkButton.Text="Text property dynamically changed in 
dg_ItemDataBound"
    end if
    end sub

But when I try to do this for my dynamic TemplateColumn, it doesn't work 
(neither in dg_ItemDataBound, nor in dg_ItemCreated), and I get the 
following error message:

	System.NullReferenceException: Object reference not set to an 
instance of an object

Can anyone tell me how to locate these server controls in a dynamic 
Template Column?

Thanks,

JON



++++++++++++++++++++++++++++++++++++++++++++++++++++++
MAIN DATAGRID PAGE 
(the EditItemTemplate is in a separate ascx file, see below):
++++++++++++++++++++++++++++++++++++++++++++++++++++++



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


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

Dim objConn as New OleDbConnection ( ConfigurationSettings.AppSettings
("ConnectionString") )
Dim ds as Dataset = New DataSet()
Dim objAdapter as New OleDbDataAdapter ( "SELECT * FROM tblItems WHERE 
ItemID = 1;", objConn )


Sub Page_Init(sender as object, e as eventargs)
	Dim tc1 As New TemplateColumn()
	tc1.HeaderTemplate = New DataGridTemplate
(ListItemType.Header, "Column1")
	tc1.ItemTemplate = New DataGridTemplate
(ListItemType.Item, "Column1")
	tc1.EditItemTemplate = Page.LoadTemplate
("Example_EditItemTemplate.ascx")
	tc1.FooterTemplate = New DataGridTemplate
(ListItemType.Footer, "Column1")
	dg.Columns.Add(tc1)
End sub


Sub Page_Load(sender as object, e as eventargs)
	objConn.Open()
	objAdapter.Fill(ds, "tblItems")
	dg.DataSource = ds
	dg.DataMember = "tblItems"	
	
	If Not Page.IsPostBack Then
	dg.Databind()
	End If
End Sub

sub dg_edit(sender as object, e as DataGridCommandEventArgs)
	dg.edititemindex = e.item.itemindex
	dg.databind()
end sub

sub dg_cancel(sender as object, e as DataGridCommandEventArgs)
	dg.edititemindex = -1
	dg.databind()
end sub

sub dg_update(sender as object, e as DataGridCommandEventArgs)	
	'Put an update function here
end sub

sub dg_command(sender as object, e as DataGridCommandEventArgs)
	'Do something later here
end sub

sub dg_ItemCreated(sender as object, e as DataGridItemEventArgs)
	'can't find lbtExampleLinkButton in dg_ItemDataBound or here either
end sub

sub dg_ItemDataBound(sender as object, e as DataGridItemEventArgs)
	If (e.Item.ItemType = ListItemType.EditItem) then
		Dim lbtExampleLinkButton as LinkButton = e.Item.FindControl
("lbtExampleLinkButton")
		lbtExampleLinkButton.Text="Text property dynamically 
changed in dg_ItemDataBound"
		'*******************************************************
		'Above line gets following error:
		'System.NullReferenceException: Object reference not set 
to an instance of an object
		'*******************************************************
		AddHandler lbtExampleLinkButton.Click, AddressOf 
DoSomething
	end if
end sub

sub DoSomething(sender as object, e as EventArgs)
	Response.Write("written in Sub DoSomething")
end sub

Private Class DataGridTemplate
	Implements ITemplate
	Dim templateType As ListItemType
	Dim columnName As String
	Sub New(ByVal type As ListItemType, ByVal ColName As String)
		templateType = type
		columnName = ColName
		End Sub
		Sub InstantiateIn(ByVal container As Control) Implements 
ITemplate.InstantiateIn
		Dim lc As New Literal()
		Select Case templateType
		Case ListItemType.Header
		lc.Text = "<I><b>Header</b></I>"
		container.Controls.Add(lc)
		Case ListItemType.Item
		lc.Text = "Item " & columnName
		container.Controls.Add(lc)
		Case ListItemType.EditItem
		Dim tb As New TextBox()
		tb.Text = ""
		container.Controls.Add(tb)
		Case ListItemType.Footer
		lc.Text = "<I><b>Footer</b></I>"
		container.Controls.Add(lc)
		End Select
	End Sub
End Class



</script> 

<html><body>

<form runat="server" name="form1" id="form1">

<asp:DataGrid id="dg" runat="server"
HeaderStyle-BackColor="#cccc99"
FooterStyle-BackColor="#cccc99"
ItemStyle-BackColor="#ffffff"
AlternatingItemStyle-Backcolor="#cccccc"
AutoGenerateColumns="False"
ShowFooter="True"
OnEditCommand="dg_edit"
OnCancelCommand="dg_cancel"
OnUpdateCommand="dg_update"
OnItemCommand="dg_command"
OnItemCreated="dg_ItemCreated"
OnItemDataBound="dg_ItemDataBound"
>

<Columns>	
<asp:editcommandcolumn HeaderText="EDIT" edittext="Edit" 
CancelText="Cancel" UpdateText="Save" />
</Columns>

</asp:dataGrid>

</form> 
</body></html>

__________________________________________________________________


CODE FOR "Example_EditItemTemplate.ascx":


<%@ Control Language="VB" %>
<p>
ItemTitle: <asp:Textbox id="tbItemTitle" runat="server" />
</p>
<p>
ItemRating: <asp:TextBox id="tbItemRating" runat="server" />
</p>
<p>
<asp:LinkButton id="lbtExampleLinkButton" runat="server" 
Text="ExampleLinkButton" />
</p>
<p>
<asp:Button id="btExampleButton" runat="server" Text="ExampleButton" />
</p>

Message #2 by Scott Watermasysk <swatermasysk@C...> on Wed, 27 Mar 2002 08:55:58 -0500
Jon,

I have been working on the same problem on and off for a while now...

I am starting to think this is a bug. If you just try to look at the control
heirchary using Trace, you will see that for some reason, after you do a
postback from the edit template your edittemplate controls seem to
disappear.

I would try to cross post this at:
http://www.aspfriends.com/aspfriends/aspngdatagridrepeaterdatalist.asp and
see if you can't get more information there.

HTH,
scott

-----Original Message-----
From: Jon Maz [mailto:jonmaz@s...] 
Sent: Wednesday, March 27, 2002 8:29 AM
To: aspx_beginners
Subject: [aspx_beginners] Why can't I find the control?


Hi,

I am building a DataGrid with dynamic columns added to the grid in 
Page_Init.  As part of this, the EditItemTemplate is loaded into the 
DataGrid from a separate .ascx file via the Page.LoadTemplate method.

My problem is that I am having difficulties finding the server controls 
within this dynamically-added EditItemTemplate, (which I need to do to 
change their properties and add events dynamically).

On a non-dynamic TemplateColumn, I would simply use the following code to 
find a server control in an EditItemTemplate:

    sub dg_ItemDataBound(sender as object, e as DataGridItemEventArgs)
    If (e.Item.ItemType = ListItemType.EditItem) then
    Dim lbtExampleLinkButton as LinkButton = e.Item.FindControl
("lbtExampleLinkButton")
    lbtExampleLinkButton.Text="Text property dynamically changed in 
dg_ItemDataBound"
    end if
    end sub

But when I try to do this for my dynamic TemplateColumn, it doesn't work 
(neither in dg_ItemDataBound, nor in dg_ItemCreated), and I get the 
following error message:

	System.NullReferenceException: Object reference not set to an 
instance of an object

Can anyone tell me how to locate these server controls in a dynamic 
Template Column?

Thanks,

JON



++++++++++++++++++++++++++++++++++++++++++++++++++++++
MAIN DATAGRID PAGE 
(the EditItemTemplate is in a separate ascx file, see below):
++++++++++++++++++++++++++++++++++++++++++++++++++++++



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


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

Dim objConn as New OleDbConnection ( ConfigurationSettings.AppSettings
("ConnectionString") )
Dim ds as Dataset = New DataSet()
Dim objAdapter as New OleDbDataAdapter ( "SELECT * FROM tblItems WHERE 
ItemID = 1;", objConn )


Sub Page_Init(sender as object, e as eventargs)
	Dim tc1 As New TemplateColumn()
	tc1.HeaderTemplate = New DataGridTemplate (ListItemType.Header,
"Column1")
	tc1.ItemTemplate = New DataGridTemplate
(ListItemType.Item, "Column1")
	tc1.EditItemTemplate = Page.LoadTemplate
("Example_EditItemTemplate.ascx")
	tc1.FooterTemplate = New DataGridTemplate (ListItemType.Footer,
"Column1")
	dg.Columns.Add(tc1)
End sub


Sub Page_Load(sender as object, e as eventargs)
	objConn.Open()
	objAdapter.Fill(ds, "tblItems")
	dg.DataSource = ds
	dg.DataMember = "tblItems"	
	
	If Not Page.IsPostBack Then
	dg.Databind()
	End If
End Sub

sub dg_edit(sender as object, e as DataGridCommandEventArgs)
	dg.edititemindex = e.item.itemindex
	dg.databind()
end sub

sub dg_cancel(sender as object, e as DataGridCommandEventArgs)
	dg.edititemindex = -1
	dg.databind()
end sub

sub dg_update(sender as object, e as DataGridCommandEventArgs)	
	'Put an update function here
end sub

sub dg_command(sender as object, e as DataGridCommandEventArgs)
	'Do something later here
end sub

sub dg_ItemCreated(sender as object, e as DataGridItemEventArgs)
	'can't find lbtExampleLinkButton in dg_ItemDataBound or here either
end sub

sub dg_ItemDataBound(sender as object, e as DataGridItemEventArgs)
	If (e.Item.ItemType = ListItemType.EditItem) then
		Dim lbtExampleLinkButton as LinkButton = e.Item.FindControl
("lbtExampleLinkButton")
		lbtExampleLinkButton.Text="Text property dynamically 
changed in dg_ItemDataBound"
		'*******************************************************
		'Above line gets following error:
		'System.NullReferenceException: Object reference not set 
to an instance of an object
		'*******************************************************
		AddHandler lbtExampleLinkButton.Click, AddressOf 
DoSomething
	end if
end sub

sub DoSomething(sender as object, e as EventArgs)
	Response.Write("written in Sub DoSomething")
end sub

Private Class DataGridTemplate
	Implements ITemplate
	Dim templateType As ListItemType
	Dim columnName As String
	Sub New(ByVal type As ListItemType, ByVal ColName As String)
		templateType = type
		columnName = ColName
		End Sub
		Sub InstantiateIn(ByVal container As Control) Implements 
ITemplate.InstantiateIn
		Dim lc As New Literal()
		Select Case templateType
		Case ListItemType.Header
		lc.Text = "<I><b>Header</b></I>"
		container.Controls.Add(lc)
		Case ListItemType.Item
		lc.Text = "Item " & columnName
		container.Controls.Add(lc)
		Case ListItemType.EditItem
		Dim tb As New TextBox()
		tb.Text = ""
		container.Controls.Add(tb)
		Case ListItemType.Footer
		lc.Text = "<I><b>Footer</b></I>"
		container.Controls.Add(lc)
		End Select
	End Sub
End Class



</script> 

<html><body>

<form runat="server" name="form1" id="form1">

<asp:DataGrid id="dg" runat="server" HeaderStyle-BackColor="#cccc99"
FooterStyle-BackColor="#cccc99" ItemStyle-BackColor="#ffffff"
AlternatingItemStyle-Backcolor="#cccccc"
AutoGenerateColumns="False"
ShowFooter="True"
OnEditCommand="dg_edit"
OnCancelCommand="dg_cancel"
OnUpdateCommand="dg_update"
OnItemCommand="dg_command"
OnItemCreated="dg_ItemCreated" OnItemDataBound="dg_ItemDataBound"
>

<Columns>	
<asp:editcommandcolumn HeaderText="EDIT" edittext="Edit" 
CancelText="Cancel" UpdateText="Save" />
</Columns>

</asp:dataGrid>

</form> 
</body></html>

__________________________________________________________________


CODE FOR "Example_EditItemTemplate.ascx":


<%@ Control Language="VB" %>
<p>
ItemTitle: <asp:Textbox id="tbItemTitle" runat="server" />
</p>
<p>
ItemRating: <asp:TextBox id="tbItemRating" runat="server" /> </p> <p>
<asp:LinkButton id="lbtExampleLinkButton" runat="server" 
Text="ExampleLinkButton" />
</p>
<p>
<asp:Button id="btExampleButton" runat="server" Text="ExampleButton" /> </p>

Message #3 by "Jon Maz" <jonmaz@s...> on Wed, 27 Mar 2002 16:52:14
Hi Scott,

Well, at least I'm no longer alone with this problem!  Have posted to 
aspngdatagridrepeaterdatalist as suggested, let's hope someone there comes 
to our rescue.  What do you do if it IS a .net bug?  Is there somewhere at 
Microsoft you can report it to?

JON

  Return to Index