Has anyone had any luck with this problem yet? Thanks!
Dave
> Hi,
> I am building a DataGrid with dynamic columns added to the grid in
P> age_Init. As part of this, the EditItemTemplate is loaded into the
D> ataGrid from a separate .ascx file via the Page.LoadTemplate method.
> My problem is that I am having difficulties finding the server controls
w> ithin this dynamically-added EditItemTemplate, (which I need to do to
c> hange their properties and add events dynamically).
> On a non-dynamic TemplateColumn, I would simply use the following code
to
f> ind 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
d> g_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
f> ollowing error message:
> System.NullReferenceException: Object reference not set to an
i> nstance of an object
> Can anyone tell me how to locate these server controls in a dynamic
T> emplate Column?
> Thanks,
> JON
>
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++
M> AIN 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") )
D> im ds as Dataset = New DataSet()
D> im objAdapter as New OleDbDataAdapter ( "SELECT * FROM tblItems WHERE
I> temID = 1;", objConn )
>
S> ub 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)
E> nd sub
>
S> ub 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
E> nd Sub
> sub dg_edit(sender as object, e as DataGridCommandEventArgs)
> dg.edititemindex = e.item.itemindex
> dg.databind()
e> nd sub
> sub dg_cancel(sender as object, e as DataGridCommandEventArgs)
> dg.edititemindex = -1
> dg.databind()
e> nd sub
> sub dg_update(sender as object, e as DataGridCommandEventArgs)
> 'Put an update function here
e> nd sub
> sub dg_command(sender as object, e as DataGridCommandEventArgs)
> 'Do something later here
e> nd sub
> sub dg_ItemCreated(sender as object, e as DataGridItemEventArgs)
> 'can't find lbtExampleLinkButton in dg_ItemDataBound or here
either
e> nd 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
c> hanged in dg_ItemDataBound"
> '*******************************************************
> 'Above line gets following error:
> 'System.NullReferenceException: Object reference not set
t> o an instance of an object
> '*******************************************************
> AddHandler lbtExampleLinkButton.Click, AddressOf
D> oSomething
> end if
e> nd sub
> sub DoSomething(sender as object, e as EventArgs)
> Response.Write("written in Sub DoSomething")
e> nd 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
I> Template.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
E> nd Class
>
> </script>
> <html><body>
> <form runat="server" name="form1" id="form1">
> <asp:DataGrid id="dg" runat="server"
H> eaderStyle-BackColor="#cccc99"
F> ooterStyle-BackColor="#cccc99"
I> temStyle-BackColor="#ffffff"
A> lternatingItemStyle-Backcolor="#cccccc"
A> utoGenerateColumns="False"
S> howFooter="True"
O> nEditCommand="dg_edit"
O> nCancelCommand="dg_cancel"
O> nUpdateCommand="dg_update"
O> nItemCommand="dg_command"
O> nItemCreated="dg_ItemCreated"
O> nItemDataBound="dg_ItemDataBound"
>>
> <Columns>
<> asp:editcommandcolumn HeaderText="EDIT" edittext="Edit"
C> ancelText="Cancel" UpdateText="Save" />
<> /Columns>
> </asp:dataGrid>
> </form>
<> /body></html>
> __________________________________________________________________
>
C> ODE FOR "Example_EditItemTemplate.ascx":
>
<> %@ Control Language="VB" %>
<> p>
I> temTitle: <asp:Textbox id="tbItemTitle" runat="server" />
<> /p>
<> p>
I> temRating: <asp:TextBox id="tbItemRating" runat="server" />
<> /p>
<> p>
<> asp:LinkButton id="lbtExampleLinkButton" runat="server"
T> ext="ExampleLinkButton" />
<> /p>
<> p>
<> asp:Button id="btExampleButton" runat="server" Text="ExampleButton" />
<> /p>