Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET .NET Gridview VB.NET , +
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
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:
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:
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:
<%@ 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 19-Dec-12 14:05pm

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

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 Zoltán Zörgő 99
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 19 Dec 2012
Copyright © CodeProject, 1999-2014
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