Click here to Skip to main content
15,885,904 members

Help Incorporating GridView with Custom Item Template Into Server Control

bob39204 asked:

Open original thread
First, I really need this to be a server control. I feel that I am close, but although my bound fields retain their values across postbacks, my custom template (ITemplate) fields do not. It would be much appreciated if someone could tell me what I am doing wrong.

The objective...

First, as stated I am building a server control. For the purposes of demonstrating my problem, I created a simple data table to use as a data source as follows:
VB
Dim MyTable As New DataTable
MyTable.Columns.Add("Name")

MyTable.Rows.Add("Bob")
MyTable.Rows.Add("Laura")
MyTable.Rows.Add("Jack")


My server control implements WebControl and instantiates all of its child controls within the overridden CreateChildControls() method. It then databinds within its Load event. Here's the complete code:
VB
Public Class TestServerControl
    Inherits WebControl

    Protected MyGridView As GridView

    Protected Overrides Sub CreateChildControls()
        MyBase.CreateChildControls()

        'Create GridView
        MyGridView = New GridView
        MyGridView.ID = "MyGridView"
        MyGridView.AutoGenerateColumns = False
        MyGridView.EnableViewState = True
        Me.Controls.Add(MyGridView)

        'First columns is bound to data table
        Dim Field1 As New BoundField
        Field1.HeaderText = "Name"
        Field1.DataField = "Name"
        MyGridView.Columns.Add(Field1)

        'Second column is my custom template
        Dim Field2 As New TemplateField()
        Field2.ItemTemplate = New MyItemTemplate
        Field2.HeaderText = "Delete"
        MyGridView.Columns.Add(Field2)
    End Sub

    Private Sub TestServerControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            EnsureChildControls()

            Dim MyTable As DataTable = CreateDataTable()

            MyGridView.DataSource = MyTable
            MyGridView.DataBind()
        End If
    End Sub

    Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
        MyGridView.RenderControl(writer)
    End Sub
End Class


Next, I created MyItemTemplate as follows. All it does in this case is display an image, which I realize I could have done in easier ways, but in the real version of my control it would be much more complicated. That said, if I can just get this working I think I could figure the rest out. Anyway, here's my very simplified custom template class:
VB
Class MyItemTemplate
    Implements ITemplate

    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
        Dim MyImageButton As New ImageButton
        MyImageButton.ID = "DeleteImage"
        MyImageButton.ImageUrl = "/hef/hers/img/delete.gif"

        container.Controls.Add(MyImageButton)
    End Sub
End Class


So when I first load the page, I get something like (where 'X' represents my image):
Bob X
Laura X
Jack X

which is exactly what I expect. However, when I click click a button the containing webform, the images (Xs) go away.

For reference, here is the web form that I included the server control within:
ASP.NET
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="TestProject.WebForm1" %>
<%@ Register Assembly="Hef.Controls" Namespace="TestProject" TagPrefix="cc1" %>
<!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">
    &lt;title>&lt;/title>
</head>
<body>
    &lt;form id="form1" runat="server">
    <div>
        <cc1:TestServerControl ID="TestServerControl1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>
Tags: Visual Basic, .NET, ASP.NET, Gridview, Server control

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900