uczxnvdrlf
New Member
Hi,<BR><BR>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.<BR><BR>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).<BR><BR>On a non-dynamic TemplateColumn, I would simply use the following code to find a server control in an EditItemTemplate:<BR><BR> sub dg_ItemDataBound(sender as object, e as DataGridItemEventArgs)<BR> If (e.Item.ItemType = ListItemType.EditItem) then<BR> Dim lbtExampleLinkButton as LinkButton = e.Item.FindControl("lbtExampleLinkButton")<BR> lbtExampleLinkButton.Text="Text property dynamically changed in dg_ItemDataBound"<BR> end if<BR> end sub<BR><BR>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:<BR><BR> System.NullReferenceException: Object reference not set to an instance of an object<BR><BR>Can anyone tell me how to locate these server controls in a dynamic Template Column?<BR><BR>Thanks,<BR><BR>JON<BR><BR><BR><BR>++++++++++++++++++++++++++++++++++++++++++++++++++ ++++<BR>MAIN DATAGRID PAGE <BR>(the EditItemTemplate is in a separate ascx file, see below):<BR>++++++++++++++++++++++++++++++++++++++++++++++++++ ++++<BR><BR><BR><BR><%@ Page Language="VB" Debug="true" %><BR><%@ Import Namespace="System.Data" %><BR><%@ Import Namespace="System.Data.Oledb" %><BR><BR><BR><script language="VB" runat="server"><BR><BR>Dim objConn as New OleDbConnection ( ConfigurationSettings.AppSettings("ConnectionString") )<BR>Dim ds as Dataset = New DataSet()<BR>Dim objAdapter as New OleDbDataAdapter ( "SELECT * FROM tblItems WHERE ItemID = 1;", objConn )<BR><BR><BR>Sub Page_Init(sender as object, e as eventargs)<BR> Dim tc1 As New TemplateColumn()<BR> tc1.HeaderTemplate = New DataGridTemplate(ListItemType.Header, "Column1")<BR> tc1.ItemTemplate = New DataGridTemplate(ListItemType.Item, "Column1")<BR> tc1.EditItemTemplate = Page.LoadTemplate("Example_EditItemTemplate.ascx")<BR> tc1.FooterTemplate = New DataGridTemplate(ListItemType.Footer, "Column1")<BR> dg.Columns.Add(tc1)<BR>End sub<BR><BR><BR>Sub Page_Load(sender as object, e as eventargs)<BR> objConn.Open()<BR> objAdapter.Fill(ds, "tblItems")<BR> dg.DataSource = ds<BR> dg.DataMember = "tblItems" <BR> <BR> If Not Page.IsPostBack Then<BR> dg.Databind()<BR> End If<BR>End Sub<BR><BR>sub dg_edit(sender as object, e as DataGridCommandEventArgs)<BR> dg.edititemindex = e.item.itemindex<BR> dg.databind()<BR>end sub<BR><BR>sub dg_cancel(sender as object, e as DataGridCommandEventArgs)<BR> dg.edititemindex = -1<BR> dg.databind()<BR>end sub<BR><BR>sub dg_update(sender as object, e as DataGridCommandEventArgs) <BR> 'Put an update function here<BR>end sub<BR><BR>sub dg_command(sender as object, e as DataGridCommandEventArgs)<BR> 'Do something later here<BR>end sub<BR><BR>sub dg_ItemCreated(sender as object, e as DataGridItemEventArgs)<BR> 'can't find lbtExampleLinkButton in dg_ItemDataBound or here either<BR>end sub<BR><BR>sub dg_ItemDataBound(sender as object, e as DataGridItemEventArgs)<BR> If (e.Item.ItemType = ListItemType.EditItem) then<BR> Dim lbtExampleLinkButton as LinkButton = e.Item.FindControl("lbtExampleLinkButton")<BR> lbtExampleLinkButton.Text="Text property dynamically changed in dg_ItemDataBound"<BR> '************************************************* *************************<BR> 'Above line gets following error:<BR> 'System.NullReferenceException: Object reference not set to an instance of an object<BR> '************************************************* *************************<BR> AddHandler lbtExampleLinkButton.Click, AddressOf DoSomething<BR> end if<BR>end sub<BR><BR>sub DoSomething(sender as object, e as EventArgs)<BR> Response.Write("written in Sub DoSomething")<BR>end sub<BR><BR>Private Class DataGridTemplate<BR> Implements ITemplate<BR> Dim templateType As ListItemType<BR> Dim columnName As String<BR> Sub New(ByVal type As ListItemType, ByVal ColName As String)<BR> templateType = type<BR> columnName = ColName<BR> End Sub<BR> Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn<BR> Dim lc As New Literal()<BR> Select Case templateType<BR> Case ListItemType.Header<BR> lc.Text = "<I><b>Header</b></I>"<BR> container.Controls.Add(lc)<BR> Case ListItemType.Item<BR> lc.Text = "Item " & columnName<BR> container.Controls.Add(lc)<BR> Case ListItemType.EditItem<BR> Dim tb As New TextBox()<BR> tb.Text = ""<BR> container.Controls.Add(tb)<BR> Case ListItemType.Footer<BR> lc.Text = "<I><b>Footer</b></I>"<BR> container.Controls.Add(lc)<BR> End Select<BR> End Sub<BR>End Class<BR><BR><BR><BR></script> <BR><BR><html><body><BR><BR><form runat="server" name="form1" id="form1"><BR><BR><asp
ataGrid id="dg" runat="server"<BR>HeaderStyle-BackColor="#cccc99"<BR>FooterStyle-BackColor="#cccc99"<BR>ItemStyle-BackColor="#ffffff"<BR>AlternatingItemStyle-Backcolor="#cccccc"<BR>AutoGenerateColumns="False"<BR>ShowFooter="True"<BR>OnEditCommand="dg_edit"<BR>OnCancelCommand="dg_cancel"<BR>OnUpdateCommand="dg_update"<BR>OnItemCommand="dg_command"<BR>OnItemCreated="dg_ItemCreated"<BR>OnItemDataBound="dg_ItemDataBound"<BR>><BR><BR><Columns> <BR><asp:editcommandcolumn HeaderText="EDIT" edittext="Edit" CancelText="Cancel" UpdateText="Save" /><BR></Columns><BR><BR></asp:dataGrid><BR><BR></form> <BR></body></html><BR><BR>__________________________________________________ ____________________________<BR><BR><BR>CODE FOR "Example_EditItemTemplate.ascx":<BR><BR><BR><%@ Control Language="VB" %><BR><p><BR>ItemTitle: <asp:Textbox id="tbItemTitle" runat="server" /><BR></p><BR><p><BR>ItemRating: <asp:TextBox id="tbItemRating" runat="server" /><BR></p><BR><p><BR><asp:LinkButton id="lbtExampleLinkButton" runat="server" Text="ExampleLinkButton" /><BR></p><BR><p><BR><asp:Button id="btExampleButton" runat="server" Text="ExampleButton" /><BR></p><BR>
