开发者

Dynamic Controls Example. Script controls may not be registered after PreRender

When creating dynamic ajax controls you may experience a pre-render issue on postbacks. You are supposed to re-create the controls on postback, however if there are very many of them performance gets very slow between each postback. i.e. clicking on a combobox, it may take several seconds. So what I did was group the controls in panels, store the panels in a collection, then re-call the panels on Postback. This actually works great if the controls inside the panel are standard html controls (textbox, dropdowlist, etc.). But...doesn't work well with ajax controls...yet.

I have included a sample below. Uncomment/Comment the code to test it. If anyone has a good idea how to make this work with ajax controls that would be great.

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="PreRenderAjax.WebForm1" %> 

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %> 
<!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"> 
  <title></title> 
  </head> 
<开发者_如何学JAVAbody> 
  <form id="form1" runat="server"> 
  <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> 
  </asp:ToolkitScriptManager> 
  <div> 
    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder> 
  </div> 
  </form> 
</body> 
</html>


Public Class WebForm1 
Inherits System.Web.UI.Page 

Shared panellist As New List(Of Panel) 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles   Me.Load 
    If IsPostBack Then 
        'This is for reloading panels. Works with dropdownlist, but not ajax combobox 
        For Each pn As Panel In panellist 
            PlaceHolder1.Controls.Add(pn) 
        Next 

        'This for re-creating all the controls again. Not very efficient for ajax controls though. 
        'CreateInterface() 
    Else 
        CreateInterface() 
    End If 
End Sub 

Protected Overrides Sub CreateChildControls() 
    MyBase.CreateChildControls() 
End Sub 

Protected Overrides Sub OnInit(ByVal e As System.EventArgs) 
    MyBase.OnInit(e) 
    EnsureChildControls() 
End Sub 

Private Sub CreateInterface() 
    Dim pn As Panel 

    pn = CreatePanel("panel1") 
    CreateControls(pn, 5) 
    pn = CreatePanel("panel2") 
    CreateControls(pn, 5) 
    pn = CreatePanel("panel3") 
    CreateControls(pn, 5) 
    pn = CreatePanel("panel4") 
    CreateControls(pn, 5) 

End Sub 

Private Function CreatePanel(ByVal name As String) As Panel 
    Dim pn As New Panel 
    pn.ID = name 
    pn.Width = 250 
    pn.BorderStyle = BorderStyle.Solid 
    pn.BorderColor = Drawing.Color.Blue 
    pn.Style.Add("margin", "5px") 
    pn.Style.Add("padding", "5px") 
    PlaceHolder1.Controls.Add(pn) 
    panellist.Add(pn) 
    CreatePanel = pn 
End Function 

Private Sub CreateControls(ByVal pn As Panel, ByVal howmany As Integer) 
    Dim cbo As AjaxControlToolkit.ComboBox 
    'Dim cbo As DropDownList 
    For i As Integer = 0 To howmany - 1 
        cbo = New AjaxControlToolkit.ComboBox 
        'cbo = New DropDownList 
        cbo.ID = pn.ID & "_cbo" & i 
        cbo.Width = 200 
        cbo.Items.Add("Item 1") 
        cbo.Items.Add("Item 2") 
        cbo.Items.Add("Item 3") 
        cbo.Items.Add("Item 4") 
        cbo.Items.Add("Item 5") 
        cbo.Style.Add("margin", "3px") 
        cbo.AutoPostBack = True 
        pn.Controls.Add(cbo) 
    Next 
  End Sub 

End Class


I just did a test to see if postback slowness was from the ajax controls themselves or recreation. Well come to find out its tha actual ajax controls themselves. I created a blank page, put 40 ajax combobox's on (adding a couple items to each), turn on postcack. When I started the page and clicked an item it took about 3 or 4 seconds to complete the postback.

Did the same thing with dropdownlist and it works great. too bad you cant use it like a combobox.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜