| | |
? ItemTemplate in TemplateField of a GridView
Please support our ASP.NET advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: May 2007
Posts: 2
Reputation:
Solved Threads: 0
I have created a custom template field for a gridview.When one clicks a button, the template field is supposed to be added to the gridview.
Here is the code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim assignment As String
assignment = txtAssignment.Text
Dim column As New TemplateField
column.ItemTemplate = New GridViewTemplate(DataControlRowType.DataRow, "txtbox")
column.HeaderStyle.Width = 4
column.HeaderText = assignment
column.HeaderStyle.CssClass = "verticaltext"
GridView1.Columns.Add(column)
GridView1.DataBind()
End Sub
Public Class GridViewTemplate
Implements ITemplate
Private templateType As DataControlRowType
Private columnName As String
Sub New(ByVal type As DataControlRowType, ByVal colname As String)
templateType = type
columnName = colname
End Sub
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
Select Case templateType
Case DataControlRowType.Header
' Create the controls to put in the header
' section and set their properties.
Dim lc As New Literal
lc.Text = "<b>" & columnName & "</b>"
' Add the controls to the Controls collection
' of the container.
container.Controls.Add(lc)
Case DataControlRowType.DataRow
Dim t As New TextBox
t.Width = 20
t.MaxLength = 3
container.Controls.Add(t)
End Select
End Sub
End Class
The problem is the rendering. When one clicks the button the template field is added with its textbox item templates.
However, when one clicks again, the textboxes of the previous temp field disappear completely. What is causing this?? All I want to do is to be able to add multiple temp fields with textboxes to the gridview and have the textboxes remain so that the user can put in a value.
This is so frustrating that no one can find a way to do this.
Here is the code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim assignment As String
assignment = txtAssignment.Text
Dim column As New TemplateField
column.ItemTemplate = New GridViewTemplate(DataControlRowType.DataRow, "txtbox")
column.HeaderStyle.Width = 4
column.HeaderText = assignment
column.HeaderStyle.CssClass = "verticaltext"
GridView1.Columns.Add(column)
GridView1.DataBind()
End Sub
Public Class GridViewTemplate
Implements ITemplate
Private templateType As DataControlRowType
Private columnName As String
Sub New(ByVal type As DataControlRowType, ByVal colname As String)
templateType = type
columnName = colname
End Sub
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
Select Case templateType
Case DataControlRowType.Header
' Create the controls to put in the header
' section and set their properties.
Dim lc As New Literal
lc.Text = "<b>" & columnName & "</b>"
' Add the controls to the Controls collection
' of the container.
container.Controls.Add(lc)
Case DataControlRowType.DataRow
Dim t As New TextBox
t.Width = 20
t.MaxLength = 3
container.Controls.Add(t)
End Select
End Sub
End Class
The problem is the rendering. When one clicks the button the template field is added with its textbox item templates.
However, when one clicks again, the textboxes of the previous temp field disappear completely. What is causing this?? All I want to do is to be able to add multiple temp fields with textboxes to the gridview and have the textboxes remain so that the user can put in a value.
This is so frustrating that no one can find a way to do this.
![]() |
Similar Threads
- GridView Insert Problem (ASP.NET)
- MultiLine TextBoxes in GridView edit mode (ASP.NET)
- How to split datetime format? (ASP.NET)
- select check boxes (ASP.NET)
- How to bind a value in GridView to other data? (ASP.NET)
- How to retrieve data from a dynamically generated radio button (ASP.NET)
- Special formatting of a GridView... (ASP.NET)
- GridView hiding cells dynamically (ASP.NET)
Other Threads in the ASP.NET Forum
- Previous Thread: Whats going on?
- Next Thread: Help with a cast error
Views: 8659 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for ASP.NET
.net 2.0 3.5 activexcontrol advice ajax application asp asp.net bc30451 bottomasp.net browser businesslogiclayer button c# c#gridviewcolumn chat checkbox child class click compatible confirmationcodegeneration content contenttype countryselector courier css database datagrid datagridview datagridviewcheckbox datalist deadlock development dgv dropdown dropdownmenu edit feedback flash flv folder form forms google grid gridview homeedition hosting identity iframe iis index javascript jquery list menu mono mssql multistepregistration nameisnotdeclared object objects order problem profile ratings refer rotatepage save search security serializesmo.table session silverlight smartcard software sql suse textbox tracking typeof unauthorized update validation vb vb.net video view virtualdirectory vista visual-studio visualstudio vs2008 web webarchitecture webdevelopemnt wizard xml





