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

Discussion in 'ASP .Net Datagrid Control' started by Steve Mauldin, Aug 10, 2004.

  1. 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
    Steve Mauldin, Aug 10, 2004
    #1
    1. Advertising

  2. RE: How do I dynamically bind a textbox into a grid field in ASP.NET u

    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/d...atingwebservercontroltemplatesdynamically.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/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
    >
    >
    >
    Bharat Biyani, Aug 11, 2004
    #2
    1. Advertising

  3. Re: How do I dynamically bind a textbox into a grid field in ASP.NET u

    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" <> wrote in message
    news:...
    > 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/default.asp?url=/library/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/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
    > >
    > >
    > >
    Steve Mauldin, Aug 11, 2004
    #3
  4. Re: How do I dynamically bind a textbox into a grid field in ASP.NET u

    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:...
    > 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/default.asp?url=/library/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/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
    > >
    > >
    > >
    Steve Mauldin, Aug 11, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    568
    Alphonse Giambrone
    Jun 28, 2003
  2. DevilZAngel
    Replies:
    3
    Views:
    7,709
    tatygonq
    Sep 20, 2006
  3. Luqman
    Replies:
    4
    Views:
    6,669
    Luqman
    Jan 25, 2006
  4. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    465
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
  5. Tatyana
    Replies:
    0
    Views:
    186
    Tatyana
    Jun 26, 2006
Loading...

Share This Page