Click here to Skip to main content
13,143,774 members (28,668 online)
Rate this:
Please Sign up or sign in to vote.
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:
Dim MyTable As New DataTable

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:
Public Class TestServerControl
    Inherits WebControl
    Protected MyGridView As GridView
    Protected Overrides Sub CreateChildControls()
        'Create GridView
        MyGridView = New GridView
        MyGridView.ID = "MyGridView"
        MyGridView.AutoGenerateColumns = False
        MyGridView.EnableViewState = True
        'First columns is bound to data table
        Dim Field1 As New BoundField
        Field1.HeaderText = "Name"
        Field1.DataField = "Name"
        'Second column is my custom template
        Dim Field2 As New TemplateField()
        Field2.ItemTemplate = New MyItemTemplate
        Field2.HeaderText = "Delete"
    End Sub
    Private Sub TestServerControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim MyTable As DataTable = CreateDataTable()
            MyGridView.DataSource = MyTable
        End If
    End Sub
    Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
    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:
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"
    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:
<%@ 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" "">
<html xmlns="" >
<head runat="server">
    &lt;form id="form1" runat="server">
        <cc1:TestServerControl ID="TestServerControl1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Button" />
Posted 19-Dec-12 13:05pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 19 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100