Hi,<BR><BR>I am building a DataGrid with Template Columns which are dynamically added to a DataGrid in the Page_Load subroutine. <BR><BR>The dynamic columns work fine when the page is first loaded, and also when the DataGrid goes into Edit Mode, but once in EditMode, as soon as I click on either the "Save" button, or the Button (or LinkButton) in my EditItemTemplate, then entire DYNAMIC Template Column simply disappears - although the DataGrid as a whole is STILL in Edit Mode.<BR><BR>Now clicking on any of these buttons is firing the Page_Load subroutine (I even put a PageLoadCounter into the code to check!), but for some reason, the code within Page_Load that creates the template column STOPS working - and I have no idea why.<BR><BR>Would really appreciate anyone telling me what's going on!<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><%@ 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>'This is modified code from the section<BR>' "Creating Templates Programmatically in the DataGrid Control", from URL:<BR>'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/<BR>'dv_vstechart/html/vbtchcreatingwebservercontroltemplatesprogrammatic ally.asp<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> Dim tc1 As New TemplateColumn()<BR> tc1.HeaderTemplate = New DataGridTemplate(ListItemType.Header, "Column1")<BR> tc1.ItemTemplate = New DataGridTemplate(ListItemType.Item, "Column1")<BR> 'ADD THE ASCX EDIT_ITEM_TEMPLATE FILE<BR> tc1.EditItemTemplate = Page.LoadTemplate("Example_EditItemTemplate.ascx")<BR> tc1.FooterTemplate = New DataGridTemplate(ListItemType.Footer, "Column1")<BR> dg.Columns.Add(tc1)<BR> <BR> '*** just to check that Page_Load is really firing on the button clicks<BR> lbPageLoadCounter.Text = ViewState("iPageLoadCounter")<BR> ViewState("iPageLoadCounter") = ViewState("iPageLoadCounter") + 1<BR> <BR> If Not Page.IsPostBack Then<BR> ViewState("iPageLoadCounter") = 1<BR> lbPageLoadCounter.Text = ViewState("iPageLoadCounter")<BR> dg.Databind()<BR> End If<BR>End Sub<BR><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><BR>sub dg_cancel(sender as object, e as DataGridCommandEventArgs)<BR> dg.edititemindex = -1<BR> dg.databind()<BR>end sub<BR><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> 'The buttons will do something here<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><BR><body><BR><BR>Page_Load has fired this often: <asp:Label id="lbPageLoadCounter" runat="server" /><BR><BR><BR><form runat="server" name="form1" id="form1"><BR><BR><asp
ataGrid id="dg" runat="server"<BR>AutoGenerateColumns="False"<BR>OnEditCommand="dg_edit"<BR>OnCancelCommand="dg_cancel"<BR>OnUpdateCommand="dg_update"<BR>OnItemCommand="dg_command"<BR>><BR><BR><Columns> <BR><asp:editcommandcolumn HeaderText="EDIT" edittext="Edit" CancelText="Cancel" UpdateText="Save" /><BR><asp:boundcolumn HeaderText="Test Non-Dynamic BoundColumn" DataField="ItemID" /><BR></Columns><BR><BR></asp:dataGrid><BR><BR></form> <BR></body></html><BR><BR><BR><BR>__________________________________________________ ____________________________<BR><BR><BR>CODE FOR "Example_EditItemTemplate.ascx":<BR><BR><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>
