Bharat,
I was able to get my code to work. The problem I was having was with
ListItemType.EditItem. It does not fire off it's InstantiateIn(). I
changed my code using the example code you referenced to use
ListItemType.Item instead of ListItemType.EditItem and it started working
great. Thanks for your help.
Steve Mauldin
P.S. For anyone else interested the code is below.
' Visual Basic
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim DataGridText As New TemplateColumn
dim datagrid1 as new DataGrid
DataGridText.ItemTemplate = New DataGridTemplate(ListItemType.Item,
"Months")
datagrid1.Columns.Add(DataGridText)
End Sub
Private Class DataGridTemplate
Implements ITemplate
Dim templateType As ListItemType
Dim columnName As String
Shared itemcount As Integer = 0
Sub New(ByVal type As ListItemType, ByVal ColName As String)
templateType = type
columnName = ColName
End Sub
Sub InstantiateIn(ByVal container As Control) _
Implements ITemplate.InstantiateIn
Dim lc As New Literal
Dim x As New TableCell
Select Case templateType
Case ListItemType.Header
lc.Text = "<B>" & columnName & "</B>"
container.Controls.Add(lc)
Case ListItemType.Item
Dim tb As New TextBox
tb.Width = Unit.Pixel(40)
container.Controls.Add(tb)
AddHandler tb.DataBinding, AddressOf TemplateControl_DataBinding
Case ListItemType.EditItem
Dim tb As New TextBox
tb.Width = Unit.Pixel(40)
container.Controls.Add(tb)
AddHandler tb.DataBinding, AddressOf TemplateControl_DataBinding
Case ListItemType.Footer
lc.Text = "<I>Footer</I>"
container.Controls.Add(lc)
End Select
itemcount += 1
End Sub
Private Sub TemplateControl_DataBinding(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim TB As TextBox
TB = CType(sender, TextBox)
Dim container As DataGridItem
container = CType(TB.NamingContainer, DataGridItem)
TB.Text = DataBinder.Eval(container.DataItem, "Months")
End Sub
End Class
"Bharat Biyani" <> wrote in message
news:A765DFEA-EED3-42BC-A9C0-...
> Hi Steve,
>
> You need to add Databinding code to the templates u have created. There is
> another link on that same article which discusses howto data bind the
> controls.
>
> URL
>
is:
http://msdn.microsoft.com/library/de...en-us/vbcon/ht
ml/vbtskcreatingwebservercontroltemplatesdynamically. asp
>
> ---
> Bharat Biyani ()
> http://www.orcim.com
>
>
> "Steve Mauldin" wrote:
>
> > I am trying to take dynamically generated datagrid that is bound to a
data
> > source and make one of the fields on the grid into an editable textbox
that
> > is bound to a field from the data source. Using a microsoft example
from
> >
(
http://msdn.microsoft.com/library/de...-us/vbcon/html
> > /vbtskcreatingtemplatesprogrammaticallyindatagridco ntrol.asp) I have
been
> > able to get a textbox on to the grid using the datagrid.itemtemplate but
it
> > is not bound to the data source field. The datagrid.edititemtemplate
doe
> > not display the textbox. The example just leaves you hanging with a
> > textbox not linked to anything and what use is that? I am including my
code
> > below. If anyone can tell me how to do the binding or what I am missing
I
> > would greatly appreciate it. Thanks in advance.
> >
> > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> > System.EventArgs) Handles MyBase.Load
> > datagrid1 = New DataGrid
> > datagrid1.DataSource = AutoOrderPerson
> > datagrid1.ShowHeader = False
> > datagrid1.ShowFooter = False
> > datagrid1.GridLines = GridLines.None
> > datagrid1.BackColor = Color.White
> > datagrid1.Width = Unit.Percentage(100)
> > datagrid1.AutoGenerateColumns = False
> > Dim DataGridText As New TemplateColumn
> >
> > 'This Display nothing but is the code in the microsoft example
> > DataGridText.EditItemTemplate = New
DataGridTemplate(ListItemType.EditItem,
> > "Quantity")
> >
> > 'This Displays unbound textbox
> > 'DataGridText.ItemTemplate = New DataGridTemplate(ListItemType.EditItem,
> > "Quantity")
> >
> >
> > datagrid1.Columns.Add(DataGridText)
> >
> > End 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 ITemplate.InstantiateIn
> > Select Case templateType
> > Case ListItemType.EditItem
> > Dim tb As New TextBox()
> > tb.Text = ""
> > container.Controls.Add(tb)
> > End Select
> > End Sub
> > End Class
> >
> >
> >