Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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>
Posted

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



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