2009/08/10

Update ModalPopup before it appears

I've been working on how to update an AJAX ModalPopup control before it appears and I think I've got it nailed. Here's the code.

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>Untitled Pagetitle>
head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        asp:ScriptManager>
        <asp:Button ID="Button1" runat="server" Text="Secret Button" />
        <cc1:ModalPopupExtender ID="Button1_ModalPopupExtender" runat="server" 
            DynamicServicePath="" Enabled="True" PopupControlID="Panel1" 
            TargetControlID="Button1">
        cc1:ModalPopupExtender>
        <asp:Button ID="Button2" runat="server" Text="Real Button" 
            onclick="Button2_Click" />
        <br />
        <br />
        <asp:Panel ID="Panel1" runat="server">
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
        <asp:Label runat="server" Text="Label" ID="Label1">asp:Label>
        ContentTemplate>
        asp:UpdatePanel>
        <asp:Button ID="Button3" runat="server" Text="Button" />
        asp:Panel>
        <br />
    div>
    form>
body>
html>

C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
 
namespace WebTest1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = "Cat's in the cradle with the silver spoon";
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            Label1.Text = "Yes we have no bananas";
            UpdatePanel1.Update();
            Button1_ModalPopupExtender.Show();
        }
    }
}
With this code you'll have two buttons, one will show as "Secret Button" (this would normally be hidden) and the other is "Real Button", which executes the ModalPopup that's attached to the secret button after a postback event.

How to format code before you paste it into a blog post

http://www.manoli.net has a great tool at http://www.manoli.net/csharpformat/