Mark Cooney
Mark Cooney

Reputation: 121

Understanding UpdatePanels

I am trying to understand UpdatePanels and best practise for using them.

I am using .Net4.0 with VB.Net.

The idea is to create a conversation app for a clients website and so I have control called Convo.ascx. Code added below.

<asp:UpdatePanel runat="server">
<ContentTemplate>

    <h2>Conversation</h2>
    <p><asp:Literal ID="lit1" runat="server" /></p>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" />

</ContentTemplate>
</asp:UpdatePanel>

Convo.ascx.vb

Partial Class Convo
Inherits System.Web.UI.UserControl

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    lit1.Text = lit1.Text & "<p>" & TextBox1.Text & "</p>"
End Sub

End Class

On a load page (Default.aspx) I have:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"     Inherits="_Default" %>

<%@ Reference Control="~/Convo.ascx" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
    <asp:scriptmanager ID="Scriptmanager1" runat="server"></asp:scriptmanager>

<div>
    <asp:UpdatePanel runat="server">
        <ContentTemplate>
            <asp:Button ID="Button1" runat="server" Text="Add Conversation" />
            <asp:PlaceHolder ID="phConversation" runat="server">
            </asp:PlaceHolder>
        </ContentTemplate>
    </asp:UpdatePanel>
 </div>
 </form>
 </body>
 </html>

With Codebehind Default.aspx.vb as

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    AddConvo()
End Sub

Private Sub AddConvo()
    Dim getPh As New PlaceHolder
    getPh = CType(Me.FindControl("phConversation"), PlaceHolder)
    Dim ucConvo As New Convo
    ucConvo = CType(LoadControl("~/Convo.ascx"), Convo)
    getPh.Controls.Add(ucConvo)
End Sub

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    AddConvo()
End Sub
End Class

So the Convo I add OnLoad remains on the page after extra as been added been any convo added after load is gone once the button on Convo is hit.

So my question is, how can I have these add and remain? Eventually they will be added to database but right now I am trying to understand UpdatePanels as they will become the foundation for this app.

Is there a very good explanation of multi-use UpdatePanels anywhere?

Thanks in advance

PS, im a hobbiest so only VB responses please

Upvotes: 0

Views: 232

Answers (1)

Brian Mains
Brian Mains

Reputation: 50728

The issue actually isn't with the UpdatePanel, but with ASP.NET. ASP.NET web forms uses a control hierarchy for the entire page, and you are adding the controls to the hierarchy "dynamically". Since you are doing it that way, ASP.NET requires you add the control back into the control hierarchy on every postback to the server. The UpdatePanel is a way to post back to the server, and therefore you must re-add the old user controls and new ones to that hierarchy.

Essentially the UpdatePanel was added to make AJAX easy, but you still have to work within the rules of ASP.NET.

Upvotes: 1

Related Questions