How do I dynamically bind a textbox into a grid field in ASP.NET using only the code behind?

S

Steve Mauldin

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/default.asp?url=/library/en-us/vbcon/html
/vbtskcreatingtemplatesprogrammaticallyindatagridcontrol.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
 
S

Steve Mauldin

Bharat,

Please excuse me for being a bit more dense than a rock but the article you
reference says "Note There are a few differences when creating template
columns for the DataGrid control. For details, see Creating Templates
Programmatically in the DataGrid Control." which is the Page I had
referenced. The article you referenced does not work on a DataGrid and it
excluded the EditItem field or textboxes that I am really interested in.
If you know a place where there is a good example I could go by I would
greatly appreciate it. Thank you in advance for your response.

Steve Mauldin
 
S

Steve Mauldin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top