Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0ITemplate and ViewStateITemplate and ViewState
Previous
 
Next
New Post
9/22/2010 9:52 AM
 

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





 
Previous
 
Next
HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0ITemplate and ViewStateITemplate and ViewState


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out