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