943,937 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 12565
  • ASP.NET RSS
Jun 16th, 2007
0

? ItemTemplate in TemplateField of a GridView

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Juwar is offline Offline
2 posts
since May 2007
Jul 5th, 2007
0

Re: ? ItemTemplate in TemplateField of a GridView

In a word - State

Remember asp.net is stateless. When you click the button click the page reloads first and so you lose the previously added column.

You need to turn on their viewstate
Reputation Points: 26
Solved Threads: 11
Posting Whiz in Training
f1 fan is offline Offline
275 posts
since Jan 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Whats going on?
Next Thread in ASP.NET Forum Timeline: Help with a cast error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC