Problem wth dynamically adding UpdatePanel trigger

Last post 03-18-2008 6:13 AM by nickfoster. 3 replies.

Sort Posts:

  • Problem wth dynamically adding UpdatePanel trigger

    03-17-2008, 1:02 PM
    • Loading...
    • nickfoster
    • Joined on 10-28-2004, 5:58 AM
    • Posts 36

    I am creating a navigation menu by adding LinkButtons to a page at runtime.  I need these buttons to cause an UpdatePanel to refresh rather than posting the whole page back.

    I have added the buttons to an asp:panel control, created a event handler for the buttons and added an AsyncPostBackTrigger for each.

    The problem is the UpdatePanel refreshes ok the first time a button is clicked, and then the second time the page performs a normal postback.  This pattern continues, performing a full page postback with every other click of a link.

    Obviously the UpdatePanel appears to be losing it's triggers after the first refresh and then the whole page load recreates them.  How do I get the triggers to persist with each UpdatePanel refresh?

     
    Any help is appreciated. The code is as follows:

     Master Page:

    <%@ Master Language="VB" CodeFile="Manual.master.vb" Inherits="Manual" %>
    
    <!DOCTYPE html public "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/tr/html4/strict.dtd">
    <html>
    <head id="Head1" runat="server">
    <title>Operations Manual</title>
    <link href="Styles/Hammertime.css" rel="stylesheet" type="text/css" />
    <link href="styles/Manual.css" rel="Stylesheet" type="text/css" />
    </head>
    <body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <div id="wrap">
    <div id="header">
    <asp:HyperLink ID="hypRoot" runat="server" NavigateUrl="~/default.aspx" ImageUrl="~/Images/logo.gif" ToolTip="Home" Text="Home" />
    <asp:ContentPlaceHolder ID="cphHeader" runat="server">
    </asp:ContentPlaceHolder>
    </div>
    <div id="navigation">
    <asp:ContentPlaceHolder ID="cphNavigation" runat="server">
    </asp:ContentPlaceHolder>
    </div>
    <div id="content-wrap">
    <div id="content">
    <asp:ContentPlaceHolder ID="cphMain" runat="server">
    </asp:ContentPlaceHolder>
    </div>
    </div>
    </div>
    </form>
    </body>
    </html>

     Sub page:

    <%@ Page Language="VB" MasterPageFile="~/Manual.master" AutoEventWireup="false" CodeFile="Manual.aspx.vb" Inherits="Manual" Title="Untitled Page" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="cphHeader" runat="Server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="cphNavigation" runat="Server">
    <asp:Label ID="lblClientName" runat="server"></asp:Label>
    <asp:Panel ID="pnlNavigation" runat="server">
    </asp:Panel>
    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="cphMain" runat="Server">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
    <asp:Label ID="lblTest" runat="server" Font-Bold="true" Font-Size="3em" />
    </ContentTemplate>
    </asp:UpdatePanel>
    </asp:Content>
     Code beside:
    1    Partial Class Manual
    2 Inherits System.Web.UI.Page
    3 4 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    5 ' Get the document. 6 Dim intDocument As Integer = Convert.ToInt32(Request("mid"))
    7 If intDocument > 0 Then
    8 Dim
    manual As New OperationsManual(intDocument)
    9 Dim intSection As Integer = 0
    10 11 ' Create navigation. 12 For Each section As OperationsManualSection In manual.Sections
    13 ' Add linkbutton to navigation panel. 14 Dim hyp As New LinkButton
    15 hyp.ID = "btnSection_" & intSection
    16 hyp.Text = section.Title
    17 hyp.CssClass = "sectionLink" 18 hyp.CommandArgument = section.OrderId
    19 pnlNavigation.Controls.Add(hyp)
    20 21 ' Add handler for the click event. 22 AddHandler hyp.Click, AddressOf sectionLink_click
    23 24 ' Add new trigger to update panel 25 Dim sectionTrigger As New AsyncPostBackTrigger
    26 sectionTrigger.ControlID = hyp.UniqueID
    27 sectionTrigger.EventName = "Click" 28 UpdatePanel1.Triggers.Add(sectionTrigger)
    29 sectionTrigger = Nothing 30 31 hyp = Nothing 32 intSection += 1
    33 Next
    34 35 If Not
    IsPostBack Then 36 lblClientName.Text = manual.ClientName
    37 End If
    38 39 End If
    40 End Sub
    41 42 Private Sub
    sectionLink_click(ByVal sender As Object, ByVal e As EventArgs)
    43 lblTest.Text = CType(sender, LinkButton).Text & " : " & Now.ToLongTimeString
    44 UpdatePanel1.Update()
    45 End Sub
    46 47 End Class
     
  • Re: Problem wth dynamically adding UpdatePanel trigger

    03-17-2008, 7:32 PM
    Answer
    • Loading...
    • vicpal25
    • Joined on 03-08-2006, 11:33 PM
    • Posts 376

    That is because you need to add your dynamic trigger not on Page_Load but rather on Page_Init


      Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            Dim AjaxMainTrigger As AjaxUtilies = New AjaxUtilies
            AjaxMainTrigger.AsyncTriggerControlID = "btnget"
            AjaxMainTrigger.AsyncTriggerEventName = "Click"
            AjaxMainTrigger.AddTrigger(UpdatePanelColorGrid)
        End Sub
      
    Victor A. Palma
  • Re: Problem wth dynamically adding UpdatePanel trigger

    03-17-2008, 11:17 PM

    AJAX live documentation says that

    Programmatically adding AsyncPostBackTrigger controls is not supported. Use the RegisterAsyncPostBackControl(Control) method of the ScriptManager control to programmatically register a postback control, and then call the Update() method of the UpdatePanel when the control posts back.

    http://www.asp.net/AJAX/Documentation/Live/mref/T_System_Web_UI_AsyncPostBackTrigger.aspx

    http://forums.asp.net/t/1209167.aspx

    http://forums.asp.net/t/1211861.aspx

    Chetan Sarode
    Software Engineer,
    Approva Systems Pvt Ltd,
    Pune, India.
  • Re: Problem wth dynamically adding UpdatePanel trigger

    03-18-2008, 6:13 AM
    • Loading...
    • nickfoster
    • Joined on 10-28-2004, 5:58 AM
    • Posts 36
    vicpal25:

    That is because you need to add your dynamic trigger not on Page_Load but rather on Page_Init


      Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    Dim AjaxMainTrigger As AjaxUtilies = New AjaxUtilies
    AjaxMainTrigger.AsyncTriggerControlID = "btnget" AjaxMainTrigger.AsyncTriggerEventName = "Click" AjaxMainTrigger.AddTrigger(UpdatePanelColorGrid) End Sub

      

     

    Ah, thanks Vic.  I have moved the LinkButton creation to the Page_Init() and it seems to be working correctly now.  It's a shame this has to run every time the UpdatePanel is updated but them's the breaks I guess.

    Working code:

     

    1    
    2    Partial Class Manual
    3 Inherits System.Web.UI.Page
    4 5 Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    6 ' Get the document. 7 Dim intDocument As Integer = Convert.ToInt32(Request("mid"))
    8 If intDocument > 0 Then
    9 Dim
    manual As New OperationsManual(intDocument)
    10 Dim intSection As Integer = 0
    11 12 ' Create navigation. 13 For Each section As OperationsManualSection In manual.Sections
    14 ' Add linkbutton to navigation panel. 15 Dim hyp As New LinkButton
    16 hyp.ID = "btnSection_" & intSection
    17 hyp.Text = section.Title
    18 hyp.CssClass = "sectionLink" 19 hyp.CommandArgument = section.OrderId
    20 pnlNavigation.Controls.Add(hyp)
    21 22 ' Add handler for the click event. 23 AddHandler hyp.Click, AddressOf sectionLink_click
    24 25 ' Add new trigger to update panel 26 Dim sectionTrigger As New AsyncPostBackTrigger
    27 sectionTrigger.ControlID = hyp.UniqueID
    28 sectionTrigger.EventName = "Click" 29 UpdatePanel1.Triggers.Add(sectionTrigger)
    30 sectionTrigger = Nothing 31 32 hyp = Nothing 33 intSection += 1
    34 Next
    35 End If
    36 End Sub
    37 38 Protected Sub
    Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    39 If Not IsPostBack Then
    40 Dim
    intDocument As Integer = Convert.ToInt32(Request("mid"))
    41 If intDocument > 0 Then
    42 Dim
    manual As New OperationsManual(intDocument)
    43 lblClientName.Text = manual.ClientName
    44 End If
    45 End If
    46 End Sub
    47 48 Private Sub
    sectionLink_click(ByVal sender As Object, ByVal e As EventArgs)
    49 lblTest.Text = CType(sender, LinkButton).Text & " : " & Now.ToLongTimeString
    50 'UpdatePanel1.Update() 51 End Sub
    52 53 End Class
    54
     
     
Page 1 of 1 (4 items)
Microsoft Communities
Page view counter