Hi,
I'm developing a module that displays some data in a repeater control. I have a setting that allows the end user (admins) specify (using tokens) a template on how the data should appear (e.g., they might specify the following <h1>[FIELD1]</h1><b>[FIELD2]</b><br>[BUTTON]<br> etc.). One of my tokens ([BUTTON]) indicates that a button should appear at the specified location. When the button is clicked I want to perform some action based on the selected item.
I've got it to work, however, because the controls are created dynamically I have to databind after each postback (see below).
Is there a way to maintain viewstate between postbacks so that I only have to databind the repeater the first time the page loads and still be able to respond to the button clicks? (I know I could store the data in cache and bind using the cached version but I'd like to avoid rebinding at all).
To date the various elements of my code are as follows:
My view control (i.e. view.ascx) comprises of my repeater control:
<asp:Repeater ID="rpt" runat="server" EnableViewState="true">
<HeaderTemplate />
<ItemTemplate />
<FooterTemplate />
</asp:Repeater>
In the codebehind (i.e. view.ascx.vb) I retrieve the template setting, set the repeaters ItemTemplate property and data bind the control.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'If Not Page.IsPostBack Then
If Settings.ContainsKey("ItemTemplate") Then
itemTemplate = CStr(Settings.Item("ItemTemplate"))
End If
rpt.ItemTemplate = New MyListItemTemplate(itemTemplate)
BindRepeater()
'End If
End Sub
Private Sub BindRepeater()
' get data from database and bind repeater
Dim objController As New MyController
Dim al As ArrayList = objController.GetData
rpt.DataSource = al
rpt.DataBind()
End Sub
Protected Sub rpt_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rpt.ItemCommand
' determine which button was clicked
If e.CommandName = "DoSomething" Then
Dim Id as Integer = CInt(e.CommandArgument)
' do something
End If
End Sub
The MyListItemTemplate class creates the various controls according to the specified template layout and is as follows:
Public Class MyListItemTemplate
Implements ITemplate
Private _placeHolders As String = String.Empty
Public Sub New(ByVal placeHolders As String)
_placeHolders = placeHolders
End Sub
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
' various methods have been omitted from this class for brevity
' iterate template and replace tokens with controls to be databound
' anything not to be replaced with controls will be emitted as text
Dim placeHolders As String = _placeHolders
Do While placeHolders.Length > 0
Dim alTokens As New TokenList
alTokens.Add(GetToken(placeHolders, "[FIELD1]"))
alTokens.Add(GetToken(placeHolders, "[FIELD2]"))
alTokens.Add(GetToken(placeHolders, "[BUTTON]"))
alTokens.Sort()
Dim tokenToReplace As Token = alTokens.FindNextToReplace
If Not tokenToReplace Is Nothing Then
container.Controls.Add(New LiteralControl(placeHolders.Substring(0, tokenToReplace.Position)))
Select Case tokenToReplace.Name
Case "[FIELD1]" ' create control to be databound with data from Field1 column
Dim field1 As LiteralControl = New LiteralControl()
AddHandler field1.DataBinding, AddressOf Field1_DataBinding
container.Controls.Add(field1)
Case "[FIELD2]" ' create control to be databound with data from Field2 column
Dim field2 As LiteralControl = New LiteralControl()
AddHandler field2.DataBinding, AddressOf Field2_DataBinding
container.Controls.Add(field2)
Case "[BUTTON]" ' create button
Dim btn As Button = New Button()
AddHandler btn.DataBinding, AddressOf Btn_DataBinding
container.Controls.Add(btn)
End Select
placeHolders = placeHolders.Remove(0, tokenToReplace.Position + tokenToReplace.Name.Length)
Else
container.Controls.Add(New LiteralControl(placeHolders))
placeHolders = String.Empty
End If
Loop
End Sub
Private Sub Field1_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
Dim field1 As LiteralControl = CType(sender, LiteralControl)
Dim container As RepeaterItem = CType(field1.NamingContainer, RepeaterItem)
field1.Text = DataBinder.GetPropertyValue(container.DataItem, "Field1").ToString()
End Sub
Private Sub Field2_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
Dim field2 As LiteralControl = CType(sender, LiteralControl)
Dim container As RepeaterItem = CType(field2.NamingContainer, RepeaterItem)
field2.Text = DataBinder.GetPropertyValue(container.DataItem, "Field2").ToString()
End Sub
Private Sub Btn_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
Dim btn As Button = CType(sender, Button)
Dim container As RepeaterItem = CType(btn.NamingContainer, RepeaterItem)
btn.Text = "Click Me"
btn.CausesValidation = False
btn.CommandName = "DoSomething"
btn.CommandArgument = DataBinder.GetPropertyValue(container.DataItem, "ID").ToString()
End Sub
End Class