Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > How do I dynamically bind a textbox into a grid field in ASP.NET using only the code behind?

Reply
Thread Tools

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

 
 
Steve Mauldin
Guest
Posts: n/a
 
      08-10-2004
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


 
Reply With Quote
 
 
 
 
Bharat Biyani
Guest
Posts: n/a
 
      08-11-2004
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...ynamically.asp

---
Bharat Biyani ((E-Mail Removed))
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
>
>
>

 
Reply With Quote
 
 
 
 
Steve Mauldin
Guest
Posts: n/a
 
      08-11-2004
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


"Bharat Biyani" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 ((E-Mail Removed))
> 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
> >
> >
> >



 
Reply With Quote
 
Steve Mauldin
Guest
Posts: n/a
 
      08-11-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 ((E-Mail Removed))
> 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
> >
> >
> >



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Bind selected columns to grid using dataset. arun.11 ASP .Net 0 07-05-2011 12:53 PM
Catch TextBox value entered by user into the GridView Templated Field (both created dynamically) on PostBack Tatyana ASP .Net Datagrid Control 0 06-26-2006 03:03 PM
Using a data-bind dropdownlist to populate another data-bind dropdownlist mr2_93 ASP .Net 1 10-02-2005 05:07 PM
OnTextChanged only works for first textbox in grid? notTold ASP .Net Datagrid Control 0 05-20-2004 03:44 PM



Advertisments