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.
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
Protected MyGridView As GridView
Protected Overrides Sub CreateChildControls()
MyGridView = New GridView
MyGridView.ID = "MyGridView"
MyGridView.AutoGenerateColumns = False
MyGridView.EnableViewState = True
Dim Field1 As New BoundField
Field1.HeaderText = "Name"
Field1.DataField = "Name"
Dim Field2 As New TemplateField()
Field2.ItemTemplate = New MyItemTemplate
Field2.HeaderText = "Delete"
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
Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
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:
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"
So when I first load the page, I get something like (where 'X' represents my image):
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >