Wednesday, June 4, 2008

Ajax - Response.Write with UpdatePanel

Usually, the Response.Write() method inside an UpdatePanel doesn't work.
To solve this problem, a workaround is to insert a LinkButton out of the UpdatePanel and to associate this control with the postback generated from the object inside the panel.


HTML
<asp:UpdatePanel ID="upd" runat="server">
    …
    <asp:Button runat="server" ID="BUTTON" Text="Click" />
    …
    </asp:UpdatePanel>
<asp:LinkButton ID="ALinkButton" runat="server"></asp:LinkButton>



CODE
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    …
    AddHandler Me.ALinkButton.Click, AddressOf FUNCTION
    Me.BUTTON.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(Me.ALinkButton, ""))

    If Not Page.IsPostBack Then
        …
    End If
    …
End Sub

Protected Sub FUNCTION(ByVal sender As Object, ByVal e As System.EventArgs)
    …
    Response.Write("Text")
    …
End Sub

No comments: