disappearing controls from gridview templatecolumn

G

Guest

Hi,

I have the following problem in my ASP.NET project.

On my form I have a gridview with column that can be dynamically chosen by
the user. The user has some way to enter which columns should be visible,
and it is impossible to know at design time which columns will exist at
runtime. Only the first 3 columns are fixed, the first column is a
templatecolumn with only an unbound checkbox control.

So I have written code that add and/or removes columns to the gridview as
requested by the user of the application, that works very well, only after 2
postbacks, the control in the templatecolumn disappears. I guess this looks
like a bug. It only happens if I remove columns from the gridview, not if I
add columns.

I have created a small application to demonstrate the problem, using a
standard new ASP.NET project. I'll add the code at the bottom of this post.

Thanks in advance to anyone who gives me a clue how this can be solved.

Greetings,

Joris

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Naam" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" Text="Add Column" />
<asp:Button ID="Button2" runat="server" Text="Remove Column" /></div>
</form>
</body>
</html>


Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

If Not Page.IsPostBack Then
RefreshData()
End If

End Sub

Private Class Item

Public Sub New(ByVal name As String)
Me.Name = name
End Sub

Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property

Public ReadOnly Property FirstChar() As String
Get
Return _name.Substring(0, 1)
End Get
End Property

End Class

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim firstCharColumn As New BoundField
firstCharColumn.DataField = "FirstChar"
GridView1.Columns.Add(firstCharColumn)

RefreshData()

End Sub

Private Sub RefreshData()
Dim items As New Generic.List(Of Item)
items.Add(New Item("aaaaa"))
items.Add(New Item("bbbbb"))
items.Add(New Item("ccccc"))
items.Add(New Item("ddddd"))
items.Add(New Item("eeeee"))

GridView1.DataSource = items
GridView1.DataBind()
End Sub

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button2.Click

GridView1.Columns.RemoveAt(GridView1.Columns.Count - 1)

RefreshData()

End Sub

End Class
 
G

Guest

Hi Joris,

I believe you and I were banging our heads on this issue at the same time.
I figured out a way to resolve the issue. Add this code to the "RowCreated"
handler for your gridview:

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
Dim AddCB As Web.UI.WebControls.CheckBox
AddCB = e.Row.FindControl("CheckBox1")
If AddCB Is Nothing Then
AddCB = New CheckBox
AddCB.ID = "CheckBox1"
e.Row.Cells(0).Controls.Add(AddCB)
End If
End If
End Sub

This worked with all of my "helper" functions for GridView checkboxes
(selecting/deselecting all, getting IDs for selected rows, etc.)

Hope that helps,

Kevin
 

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,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top