Problem creating DataGrid Template Column programm

fantozunea

New Member
Hi,<BR><BR>I am building a DataGrid, which includes a Template Column "ItemText". When the DataGrid goes into Edit Mode, the datafield in this column is displayed in a textbox "tbItemText". The text in this field is quite long (a few thousand words), and contains html tags. I want the user to be able to make changes in the textbox, and then see what his changes will look like when displayed in a browser, BEFORE the database itself is updated. <BR><BR>This is how I'm trying to do it: <BR><BR>1 under the textbox tbItemText in Edit Mode there is a button "Proposed Item Text".<BR><BR>2 when the user clicks on this button, a Command Event is raised<BR><BR>3 in the subroutine dg_Command, the existing columns of the DataGrid are made invisible, and a new Template Column (Header: "Proposed Item Text") is dynamically created.<BR><BR>4 This new dynamic Template Column contains an asp:Label Control, id="lbProposedItemText". This label is loaded from a separate file "TEMPLATE.ASCX" via thePage.LoadTemplate("Template.ascx") method.<BR><BR>5 lbProposedItemText.Text is set to equal tbItemText.Text, thus showing the user the results of his proposed changes to ItemText.<BR><BR><BR>I am having a problem with step 5 - when the dynamically created Template Column is shown, the Label lbProposedItemText has NO Text attached to it.<BR><BR>Can anyone tell me why?<BR><BR>Thanks!<BR><BR>JON<BR><BR><BR><BR><BR><BR>************************************************** **********************<BR>THE CODE<BR>************************************************** **********************<BR><BR><BR><BR><BR><BR>THE WHOLE LISTING IS QUITE LONG - HERE'S JUST THE SUBROUTINE TO HANDLE THE COMMAND EVENT RAISED WHEN THE "PROPOSED ITEM TEXT" BUTTON IS CLICKED:<BR><BR><BR><BR>sub dg_command(sender as object, e as DataGridCommandEventArgs)<BR><BR>If e.CommandSource.Text = "View Proposed Changes"<BR><BR>Dim tc as new TemplateColumn()<BR>tc.HeaderText="Proposed Item Text"<BR>tc.ItemTemplate = Page.LoadTemplate("Template.ascx")<BR><BR>dg.Columns(0).visible = False <BR>dg.Columns(1).visible = False<BR>dg.Columns(2).visible = False<BR>dg.Columns(3).visible = False<BR>dg.Columns(4).visible = False<BR>dg.Columns.Add(tc)<BR><BR>If (e.Item.ItemType = ListItemType.Item) or (e.Item.ItemType = ListItemType.AlternatingItem) then<BR>Dim lbProposedItemText As Label = e.Item.FindControl("lbProposedItemText")<BR>Dim tbItemText As TextBox = e.Item.FindControl("tbItemText")<BR>lbProposedItemText.Text = tbItemText.Text<BR>'THIS IS NOT WORKING FOR SOME REASON<BR>end if<BR><BR><BR>dg.DataBind()<BR><BR>end if<BR><BR>end sub<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>HERE ARE THE FULL LISTINGS OF THE ASCX PAGE AND THE MAIN DATAGRID PAGE:<BR><BR><BR><BR><BR>+++++++++++++++++++++++++++++<BR>TEMPLATE.ASCX<BR>+++++++++++++++++++++++++++++<BR><BR><%@ Control Language="VB" %><BR><BR><asp:Label id="lbProposedItemText" runat="server" /><BR><BR><BR><BR><BR>+++++++++++++++++++++++++++++<BR>MAIN PAGE<BR>+++++++++++++++++++++++++++++<BR><BR><%@ Page Language="VB" Debug="true" %><BR><%@ Import Namespace="System.Data" %><BR><%@ Import Namespace="System.Data.Oledb" %><BR><BR><%@ Reference Control="Template.ascx" %><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;", objConn )<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><BR>If Not Page.IsPostBack Then<BR>dg.Databind()<BR>End If<BR>End Sub<BR><BR><BR>function EscapeString(str)<BR>'Used to prevent HTML from being entered and to escape any quotation <BR>'marks, necessary so the values can be specified in an input field<BR>str = replace(str, "'", "''")<BR>escapeString = str<BR>end function<BR><BR><BR><BR>Sub myDataGrid_ItemDataBound(sender As Object, e As DataGridItemEventArgs)<BR><BR>If (e.Item.ItemType = ListItemType.Item) or (e.Item.ItemType = ListItemType.AlternatingItem) then<BR><BR>Dim hlViewItemText as new Hyperlink<BR>Dim lbItemId As Label = e.Item.FindControl("lbItemID")<BR>hlViewItemText.NavigateURL="ViewItemText.aspx?ItemID=" & lbItemID.Text<BR>hlViewItemText.Text="ViewItemText"<BR>e.Item.FindControl("HyperLinkPlaceHolder").Controls.Add(hlViewItemText) <BR><BR><BR><BR>End if<BR><BR><BR>'WHEN IN EDIT MODE, Set the SelectedIndex of the ItemType DropDownList to current ItemType<BR>If e.Item.ItemType = ListItemType.EditItem then<BR>Dim ddItemType as DropDownList = e.Item.FindControl("ddItemType")<BR>Dim lbItemType as Label = e.Item.FindControl("lbItemType")<BR>'lbItemType is a non-visible Label, only in the EditItemTemplate so I can grab the value here!<BR>ddItemType.Items.FindByText(lbItemType.Text).Selec ted = true<BR>End if<BR><BR>End sub<BR><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><BR>Dim tbItemTitle As TextBox = e.Item.FindControl("tbItemTitle")<BR>Dim tbItemDescription As TextBox = e.Item.FindControl("tbItemDescription")<BR>Dim tbItemText As TextBox = e.Item.FindControl("tbItemText")<BR>Dim lbItemId As Label = e.Item.FindControl("lbItemID")<BR><BR>Dim strSQL As String = "UPDATE tblItems SET ItemTitle = '" & tbItemTitle.Text & "', ItemDescription = '" & tbItemDescription.Text & "' , ItemText = '" & EscapeString(tbItemText.Text) & "' WHERE ItemID = " & lbItemID.Text<BR>'This throws an error if ItemText is empty - but not ALL items WILL have a text!<BR>Dim objCommand as New OleDbCommand(strSQL, objConn)<BR>objCommand.ExecuteNonQuery()<BR><BR>ds.Tables.Clear<BR>objAdapter.Fill(ds, "tblItems")<BR><BR>dg.edititemindex = -1<BR>dg.DataBind()<BR><BR>end sub<BR><BR><BR>sub dg_command(sender as object, e as DataGridCommandEventArgs)<BR><BR>'************************************************* ****************************<BR>'PROBLEM HERE - WANT TO DYNAMICALLY ADD A TEMPLATE COLUMN WITH A LABEL "PROPOSEDITEMTEXT"<BR>'THEN SET THE TEXT PROPERTY OF THIS LABEL TO EQUAL THE TEXT PROPERTY<BR>'OF THE TEXTBOX ITEMTEXT (FROM ANOTHER COLUMN)<BR>'************************************************* ****************************<BR><BR>If e.CommandSource.Text = "View Proposed Changes"<BR><BR>Dim tc as new TemplateColumn()<BR>tc.HeaderText="Proposed ItemText"<BR>tc.ItemTemplate = Page.LoadTemplate("Template.ascx")<BR><BR>dg.Columns(0).visible = False <BR>dg.Columns(1).visible = False<BR>dg.Columns(2).visible = False<BR>dg.Columns(3).visible = False<BR>dg.Columns(4).visible = False<BR>dg.Columns.Add(tc)<BR><BR>If (e.Item.ItemType = ListItemType.Item) or (e.Item.ItemType = ListItemType.AlternatingItem) then<BR>Dim lbProposedItemText As Label = e.Item.FindControl("lbProposedItemText")<BR>Dim tbItemText As TextBox = e.Item.FindControl("tbItemText")<BR>lbProposedItemText.Text = tbItemText.Text<BR>'THIS IS NOT WORKING FOR SOME REASON<BR>end if<BR><BR><BR>dg.DataBind()<BR><BR>end if<BR><BR>end sub<BR><BR><BR></script> <BR><BR><html><BR><head><BR><link rel="STYLESHEET" type="text/css" href=http://aspmessageboard.com/archive/index.php/"Forum.css"><BR></head><BR><body><BR><form runat="server"><BR><asp:DataGrid id="dg" runat="server"<BR>Bordercolor="black" <BR>gridlines="vertical"<BR>font-names="Arial" <BR>font-size="10pt"<BR>cellpadding="4"<BR>cellspacing="0"<BR>width="100%"<BR>ShowFooter="True"<BR>HeaderStyle-BackColor="#cccc99"<BR>FooterStyle-BackColor="#cccc99"<BR>ItemStyle-BackColor="#ffffff"<BR>AlternatingItemStyle-Backcolor="#cccccc"<BR>AutoGenerateColumns="False"<BR>OnItemDataBound="myDataGrid_ItemDataBound"<BR>OnEditCommand="dg_edit"<BR>OnCancelCommand="dg_cancel"<BR>OnUpdateCommand="dg_update"<BR>OnItemCommand="dg_command"<BR>><BR><BR><BR><BR><BR><Columns><BR><BR><asp:editcommandcolumn HeaderText="EDIT" edittext="Edit" CancelText="Cancel" UpdateText="Save" HeaderText="" /><BR><BR><BR><%-- ITEMID; ITEMTYPEID; ITEMTYPE; ITEMTITLE --%><BR><asp:TemplateColumn ItemStyle-VerticalAlign="top" HeaderText="Various"><BR><ItemTemplate><BR><asp:Table CSSClass="TableStyle1" runat="server"><BR><asp:TableRow><BR><asp:TableCell><b>ItemID: </b></asp:TableCell><asp:TableCell><asp:Label id="lbItemID" Text ='<%# DataBinder.Eval(Container.DataItem, "ItemID") %>' runat="server" /></asp:TableCell><BR><%-- this was originally a bound column, and I was tracking down the ItemID value with e.Item.Cells(1).Text - problem was, if I changed the columns around, I had to manually change the column number, so am now doing it dynamically --%><BR></asp:TableRow><BR><asp:TableRow><BR><asp:TableCell><b>ItemTypeID: </b></asp:TableCell><asp:TableCell><asp:Label runat="server" Text='<%# Container.DataItem("ItemTypeID") %>' /></asp:TableCell><BR></asp:TableRow><BR><asp:TableRow><BR><asp:TableCell><b>ItemType: </b></asp:TableCell><asp:TableCell><asp:Label runat="server" Text='<%# Container.DataItem("ItemType") %>' /></asp:TableCell><BR></asp:TableRow><BR><asp:TableRow><BR><asp:TableCell><b>ItemTitle: </b></asp:TableCell><asp:TableCell><asp:Label runat="server" Text='<%# Container.DataItem("ItemTitle") %>' /></asp:TableCell><BR></asp:TableRow><BR></asp:Table><BR></ItemTemplate><BR><EditItemTemplate><BR><asp:Table CSSClass="TableStyle1" runat="server"><BR><asp:TableRow><BR><asp:TableCell><b>ItemID: </b></asp:TableCell><asp:TableCell><asp:Label id="lbItemID" Text ='<%# DataBinder.Eval(Container.DataItem, "ItemID") %>' runat="server" /></asp:TableCell><BR></asp:TableRow><BR><asp:TableRow><BR><asp:TableCell><b>ItemTypeID: </b></asp:TableCell><asp:TableCell><asp:Textbox id="tbItemTypeID" runat="server" Text='<%# Container.DataItem("ItemTypeID") %>' /></asp:TableCell><BR></asp:TableRow><BR><asp:TableRow><BR><asp:TableCell><b>ItemType: </b></asp:TableCell><asp:TableCell><asp:Label runat="server" id="lbItemType" Visible="false" Text='<%# Container.DataItem("ItemType") %>' /><asp:DropDownList id="ddItemType" runat="server" DataSource='<%# ds.Tables("tblItems").DefaultView %>' DataTextField="ItemType" /></asp:TableCell><BR><%-- This row contains a non-visible Label, simply there so I can grab the "current" ItemType from it and set the Selected Item of the Drop Down to this value --%><BR></asp:TableRow><BR><asp:TableRow><BR><asp:TableCell><b>ItemTitle: </b></asp:TableCell><asp:TableCell><asp:Textbox id="tbItemTitle" runat="server" Text='<%# Container.DataItem("ItemTitle") %>' /></asp:TableCell><BR></asp:TableRow><BR></asp:Table><BR></EditItemTemplate><BR></asp:TemplateColumn><BR><BR><BR><%-- ITEM DESCRIPTION --%><BR><asp:TemplateColumn HeaderText="ItemDescription"><BR><ItemTemplate><BR><asp:Label runat="server" Text='<%# Container.DataItem("ItemDescription")%>' /><BR></ItemTemplate><BR><EditItemTemplate><BR><asp:Textbox id="tbItemDescription" runat="server" Textmode="MultiLine" Columns="30" Rows="8" Text='<%# Container.DataItem("ItemDescription") %>' /><BR></EditItemTemplate><BR></asp:TemplateColumn><BR><BR><BR><%-- ITEM TEXT --%><BR><asp:TemplateColumn HeaderText="ItemText"><BR><ItemTemplate><BR><asp:TextBox runat="server" ReadOnly="True" Textmode="MultiLine" Columns="30" Rows="8" Text='<%# Container.DataItem("ItemText") %>' /><BR><br/><BR><asp:Placeholder runat="server" id="HyperLinkPlaceholder" /><BR></ItemTemplate><BR><EditItemTemplate><BR><asp:TextBox id="tbItemText" runat="server" Textmode="MultiLine" Columns="30" Rows="8" BackColor="red" Text='<%# Container.DataItem("ItemText") %>' /><BR><br/><BR><asp:Button runat="server" id="btViewItemText" Text="View Proposed Changes"/><BR></EditItemTemplate><BR></asp:TemplateColumn><BR><BR><BR><%-- POSTEDBY; POSTEDDATE; ITEMPARENT; SORTORDER; <BR>ITEMRATING; MODERATED; JPTOREPLY --%><BR><asp:TemplateColumn ItemStyle-VerticalAlign="top" HeaderText="Various"><BR><ItemTemplate><BR><asp:Table CSSClass="TableStyle1" runat="server"><BR><asp:TableRow><BR><asp:TableCell><b>PostedBy: </b></asp:TableCell><asp:TableCell><asp:Label runat="server" Text='<%# Container.DataItem("PostedBy") %>' /></asp:TableCell><BR></asp:TableRow><BR><asp:TableRow><BR><asp:TableCell><b>PostedDate: </b></asp:TableCell><asp:TableCell><asp:Label runat="server" Text='<%# Container.DataItem("PostedDate") %>' /></asp:TableCell><BR><%-- COULD do this dynamically, finding the values directly from the DataSet - see above --%><BR></asp:TableRow><BR><asp:TableRow><BR><asp:TableCell><b>ItemParent: </b></asp:TableCell><asp:TableCell><asp:Label runat="server" Text='<%# Container.DataItem("ItemParent") %>' /></asp:TableCell><BR></asp:TableRow><BR><asp:TableRow><BR><asp:TableCell><b>SortOrder: </b></asp:TableCell><asp:TableCell><asp:Label runat="server" Text='<%# Container.DataItem("SortOrder") %>' /></asp:TableCell><BR></asp:TableRow><BR><asp:TableRow><BR><asp:TableCell><b>ItemRating: </b></asp:TableCell><asp:TableCell><asp:Label runat="server" Text='<%# Container.DataItem("ItemRating") %>' /></asp:TableCell><BR></asp:TableRow><BR><asp:TableRow><BR><asp:TableCell><b>Moderated?: </b></asp:TableCell><asp:TableCell><asp:CheckBox runat="server" Enabled="false" Checked='<%# Container.DataItem("ModeratedYN") %>' /></asp:TableCell><BR></asp:TableRow><BR><asp:TableRow><BR><asp:TableCell><b>JPToReply?: </b></asp:TableCell><asp:TableCell><asp:CheckBox runat="server" Enabled="false" Checked='<%# Container.DataItem("JPStillToReplyYN") %>' /></asp:TableCell><BR></asp:TableRow><BR></asp:Table><BR></ItemTemplate><BR><EditItemTemplate><BR><asp:Table CSSClass="TableStyle1" runat="server"><BR><asp:TableRow><BR><asp:TableCell><b>PostedBy: </b></asp:TableCell><asp:TableCell><asp:Textbox runat="server" width="100px" Text=&#03
 
Back
Top