Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: Problem creating Template Column programmatically


Message #1 by "Jon Maz" <jonmaz@s...> on Sun, 10 Mar 2002 19:16:45
Hi,



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. 



This is how I'm trying to do it: 



1  under the textbox tbItemText in Edit Mode there is a button "Proposed 

Item Text".



2  when the user clicks on this button, a Command Event is raised



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.



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.



5  lbProposedItemText.Text is set to equal tbItemText.Text, thus showing 

the user the results of his proposed changes to ItemText.





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.



Can anyone tell me why?



Thanks!



JON











************************************************************************

THE CODE

************************************************************************











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:







sub dg_command(sender as object, e as DataGridCommandEventArgs)



If e.CommandSource.Text = "View Proposed Changes"



Dim tc as new TemplateColumn()

tc.HeaderText="Proposed Item Text"

tc.ItemTemplate = Page.LoadTemplate("Template.ascx")



dg.Columns(0).visible = False 

dg.Columns(1).visible = False

dg.Columns(2).visible = False

dg.Columns(3).visible = False

dg.Columns(4).visible = False

dg.Columns.Add(tc)



If (e.Item.ItemType = ListItemType.Item) or (e.Item.ItemType = 

ListItemType.AlternatingItem) then

Dim lbProposedItemText As Label = e.Item.FindControl("lbProposedItemText")

Dim tbItemText As TextBox = e.Item.FindControl("tbItemText")

lbProposedItemText.Text = tbItemText.Text

'THIS IS NOT WORKING FOR SOME REASON

end if





dg.DataBind()



end if



end sub



















HERE ARE THE FULL LISTINGS OF THE ASCX PAGE AND THE MAIN DATAGRID PAGE:









+++++++++++++++++++++++++++++

TEMPLATE.ASCX

+++++++++++++++++++++++++++++



<%@ Control Language="VB" %>



<asp:Label id="lbProposedItemText" runat="server" />









+++++++++++++++++++++++++++++

MAIN PAGE

+++++++++++++++++++++++++++++



<%@ Page Language="VB" Debug="true" %>

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.Oledb" %>



<%@ Reference Control="Template.ascx" %>





<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;", 

objConn )





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





function EscapeString(str)

'Used to prevent HTML from being entered and to escape any quotation 

'marks, necessary so the values can be specified in an input field

str = replace(str, "'", "''")

escapeString = str

end function







Sub myDataGrid_ItemDataBound(sender As Object, e As DataGridItemEventArgs)



If (e.Item.ItemType = ListItemType.Item) or (e.Item.ItemType = 

ListItemType.AlternatingItem) then



Dim hlViewItemText as new Hyperlink

Dim lbItemId As Label = e.Item.FindControl("lbItemID")

hlViewItemText.NavigateURL="ViewItemText.aspx?ItemID=" & lbItemID.Text

hlViewItemText.Text="ViewItemText"

e.Item.FindControl("HyperLinkPlaceHolder").Controls.Add(hlViewItemText)	

	







End if





'WHEN IN EDIT MODE, Set the SelectedIndex of the ItemType DropDownList to 

current ItemType

If e.Item.ItemType = ListItemType.EditItem then

Dim ddItemType as DropDownList = e.Item.FindControl("ddItemType")

Dim lbItemType as Label = e.Item.FindControl("lbItemType")

'lbItemType is a non-visible Label, only in the EditItemTemplate so I can 

grab the value here!

ddItemType.Items.FindByText(lbItemType.Text).Selected = true

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)	



Dim tbItemTitle As TextBox = e.Item.FindControl("tbItemTitle")

Dim tbItemDescription As TextBox = e.Item.FindControl("tbItemDescription")

Dim tbItemText As TextBox = e.Item.FindControl("tbItemText")

Dim lbItemId As Label = e.Item.FindControl("lbItemID")



Dim strSQL As String = "UPDATE tblItems SET ItemTitle = '" & 

tbItemTitle.Text & "', ItemDescription = '" & tbItemDescription.Text 

& "' , ItemText = '" & EscapeString(tbItemText.Text) & "' WHERE ItemID = " 

& lbItemID.Text

'This throws an error if ItemText is empty - but not ALL items WILL have a 

text!

Dim objCommand as New OleDbCommand(strSQL, objConn)

objCommand.ExecuteNonQuery()



ds.Tables.Clear

objAdapter.Fill(ds, "tblItems")



dg.edititemindex = -1

dg.DataBind()



end sub





sub dg_command(sender as object, e as DataGridCommandEventArgs)



'**************************************************************************

***

'PROBLEM HERE - WANT TO DYNAMICALLY ADD A TEMPLATE COLUMN WITH A 

LABEL "PROPOSEDITEMTEXT"

'THEN SET THE TEXT PROPERTY OF THIS LABEL TO EQUAL THE TEXT PROPERTY

'OF THE TEXTBOX ITEMTEXT (FROM ANOTHER COLUMN)

'**************************************************************************

***



If e.CommandSource.Text = "View Proposed Changes"



Dim tc as new TemplateColumn()

tc.HeaderText="Proposed ItemText"

tc.ItemTemplate = Page.LoadTemplate("Template.ascx")



dg.Columns(0).visible = False 

dg.Columns(1).visible = False

dg.Columns(2).visible = False

dg.Columns(3).visible = False

dg.Columns(4).visible = False

dg.Columns.Add(tc)



If (e.Item.ItemType = ListItemType.Item) or (e.Item.ItemType = 

ListItemType.AlternatingItem) then

Dim lbProposedItemText As Label = e.Item.FindControl("lbProposedItemText")

Dim tbItemText As TextBox = e.Item.FindControl("tbItemText")

lbProposedItemText.Text = tbItemText.Text

'THIS IS NOT WORKING FOR SOME REASON

end if





dg.DataBind()



end if



end sub





</script> 



<html>

<head>

<link rel="STYLESHEET" type="text/css" href="Forum.css">

</head>

<body>

<form runat="server">

<asp:DataGrid id="dg" runat="server"

Bordercolor="black" 

gridlines="vertical"

font-names="Arial" 	

font-size="10pt"

cellpadding="4"

cellspacing="0"

width="100%"

ShowFooter="True"

HeaderStyle-BackColor="#cccc99"

FooterStyle-BackColor="#cccc99"

ItemStyle-BackColor="#ffffff"

AlternatingItemStyle-Backcolor="#cccccc"

AutoGenerateColumns="False"

OnItemDataBound="myDataGrid_ItemDataBound"

OnEditCommand="dg_edit"

OnCancelCommand="dg_cancel"

OnUpdateCommand="dg_update"

OnItemCommand="dg_command"

>









<Columns>



<asp:editcommandcolumn HeaderText="EDIT" edittext="Edit" 

CancelText="Cancel" UpdateText="Save" HeaderText="" />





<%-- ITEMID; ITEMTYPEID; ITEMTYPE; ITEMTITLE --%>

<asp:TemplateColumn ItemStyle-VerticalAlign="top" HeaderText="Various">

<ItemTemplate>

<asp:Table CSSClass="TableStyle1" runat="server">

<asp:TableRow>

<asp:TableCell><b>ItemID: </b></asp:TableCell><asp:TableCell><asp:Label 

id="lbItemID" Text ='<%# DataBinder.Eval(Container.DataItem, "ItemID") %>' 

runat="server" /></asp:TableCell>

<%-- 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 --%>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell><b>ItemTypeID: 

</b></asp:TableCell><asp:TableCell><asp:Label runat="server" Text='<%# 

Container.DataItem("ItemTypeID") %>' /></asp:TableCell>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell><b>ItemType: </b></asp:TableCell><asp:TableCell><asp:Label 

runat="server" Text='<%# Container.DataItem("ItemType") %

>' /></asp:TableCell>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell><b>ItemTitle: </b></asp:TableCell><asp:TableCell><asp:Label 

runat="server" Text='<%# Container.DataItem("ItemTitle") %

>' /></asp:TableCell>

</asp:TableRow>

</asp:Table>

</ItemTemplate>

<EditItemTemplate>

<asp:Table CSSClass="TableStyle1" runat="server">

<asp:TableRow>

<asp:TableCell><b>ItemID: </b></asp:TableCell><asp:TableCell><asp:Label 

id="lbItemID" Text ='<%# DataBinder.Eval(Container.DataItem, "ItemID") %>' 

runat="server" /></asp:TableCell>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell><b>ItemTypeID: 

</b></asp:TableCell><asp:TableCell><asp:Textbox id="tbItemTypeID" 

runat="server" Text='<%# Container.DataItem("ItemTypeID") %

>' /></asp:TableCell>

</asp:TableRow>

<asp:TableRow>

<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>

<%-- 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 --%>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell><b>ItemTitle: 

</b></asp:TableCell><asp:TableCell><asp:Textbox id="tbItemTitle" 

runat="server" Text='<%# Container.DataItem("ItemTitle") %

>' /></asp:TableCell>

</asp:TableRow>

</asp:Table>

</EditItemTemplate>

</asp:TemplateColumn>





<%-- ITEM DESCRIPTION --%>

<asp:TemplateColumn HeaderText="ItemDescription">

<ItemTemplate>

<asp:Label runat="server" Text='<%# Container.DataItem("ItemDescription")%

>' />

</ItemTemplate>

<EditItemTemplate>

<asp:Textbox id="tbItemDescription" runat="server" Textmode="MultiLine" 

Columns="30" Rows="8" Text='<%# Container.DataItem("ItemDescription") %

>' />

</EditItemTemplate>

</asp:TemplateColumn>





<%-- ITEM TEXT --%>

<asp:TemplateColumn HeaderText="ItemText">

<ItemTemplate>

<asp:TextBox runat="server" ReadOnly="True" Textmode="MultiLine" 

Columns="30" Rows="8" Text='<%# Container.DataItem("ItemText") %>' />

<br/>

<asp:Placeholder runat="server" id="HyperLinkPlaceholder" />

</ItemTemplate>

<EditItemTemplate>

<asp:TextBox id="tbItemText" runat="server" Textmode="MultiLine" 

Columns="30" Rows="8" BackColor="red" Text='<%# Container.DataItem

("ItemText") %>' />

<br/>

<asp:Button runat="server" id="btViewItemText" Text="View Proposed 

Changes"/>

</EditItemTemplate>

</asp:TemplateColumn>





<%-- POSTEDBY; POSTEDDATE; ITEMPARENT; SORTORDER; 

ITEMRATING; MODERATED; JPTOREPLY --%>

<asp:TemplateColumn ItemStyle-VerticalAlign="top" HeaderText="Various">

<ItemTemplate>

<asp:Table CSSClass="TableStyle1" runat="server">

<asp:TableRow>

<asp:TableCell><b>PostedBy: </b></asp:TableCell><asp:TableCell><asp:Label 

runat="server" Text='<%# Container.DataItem("PostedBy") %

>' /></asp:TableCell>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell><b>PostedDate: 

</b></asp:TableCell><asp:TableCell><asp:Label runat="server" Text='<%# 

Container.DataItem("PostedDate") %>' /></asp:TableCell>

<%-- COULD do this dynamically, finding the values directly from the 

DataSet - see above --%>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell><b>ItemParent: 

</b></asp:TableCell><asp:TableCell><asp:Label runat="server" Text='<%# 

Container.DataItem("ItemParent") %>' /></asp:TableCell>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell><b>SortOrder: </b></asp:TableCell><asp:TableCell><asp:Label 

runat="server" Text='<%# Container.DataItem("SortOrder") %

>' /></asp:TableCell>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell><b>ItemRating: 

</b></asp:TableCell><asp:TableCell><asp:Label runat="server" Text='<%# 

Container.DataItem("ItemRating") %>' /></asp:TableCell>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell><b>Moderated?: 

</b></asp:TableCell><asp:TableCell><asp:CheckBox runat="server" 

Enabled="false" Checked='<%# Container.DataItem("ModeratedYN") %

>' /></asp:TableCell>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell><b>JPToReply?: 

</b></asp:TableCell><asp:TableCell><asp:CheckBox runat="server" 

Enabled="false" Checked='<%# Container.DataItem("JPStillToReplyYN") %

>' /></asp:TableCell>

</asp:TableRow>

</asp:Table>

</ItemTemplate>

<EditItemTemplate>

<asp:Table CSSClass="TableStyle1" runat="server">

<asp:TableRow>

<asp:TableCell><b>PostedBy: 

</b></asp:TableCell><asp:TableCell><asp:Textbox runat="server" 

width="100px" Text='<%# Container.DataItem("PostedBy") %

>' /></asp:TableCell>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell><b>PostedDate: 

</b></asp:TableCell><asp:TableCell><asp:Textbox runat="server" 

width="100px" Text='<%# Container.DataItem("PostedDate") %

>' /></asp:TableCell>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell><b>ItemParent: 

</b></asp:TableCell><asp:TableCell><asp:Textbox runat="server" 

width="100px" Text='<%# Container.DataItem("ItemParent") %

>' /></asp:TableCell>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell><b>SortOrder: 

</b></asp:TableCell><asp:TableCell><asp:Textbox runat="server" 

width="100px" Text='<%# Container.DataItem("SortOrder") %

>' /></asp:TableCell>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell><b>ItemRating: 

</b></asp:TableCell><asp:TableCell><asp:Textbox runat="server" 

width="100px" Text='<%# Container.DataItem("ItemRating") %

>' /></asp:TableCell>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell><b>Moderated?: 

</b></asp:TableCell><asp:TableCell></strong><asp:CheckBox runat="server" 

Enabled="true" Checked='<%# Container.DataItem("ModeratedYN") %

>' /></asp:TableCell>

</asp:TableRow>

<asp:TableRow>

<asp:TableCell><b>JPToReply?: 

</b></asp:TableCell><asp:TableCell></strong><asp:CheckBox runat="server" 

Enabled="true" Checked='<%# Container.DataItem("JPStillToReplyYN") %

>' /></asp:TableCell>

</asp:TableRow>

</asp:Table>

</EditItemTemplate>

</asp:TemplateColumn>







</Columns>



</asp:dataGrid>









</form> 

</body></html>

  Return to Index