jQuery and ASP.NET

February 16, 2009

How to Create an ASP.NET AJAX ModalPopUp Extender Dynamically




You may need to create a ModalPopUpExtender dynamically. Here's some code on how to do so:

Add the following markup in your page:


<body>


<form id="form1" runat="server">


<asp:ScriptManager ID="ScriptManager1" runat="server">


</asp:ScriptManager>


<div>


<asp:Panel ID="Panel1" runat="server">


    <asp:Button ID="Button1" runat="server"


    Text="CreateModal" OnClick="Button1_Click" />


</asp:Panel>


<asp:Panel ID="ModalPanel" runat="server"


    Style="display: none"


    BackColor="Gray">


    Dynamic ModalPopup!


    <asp:Button ID="btnCancel" runat="server" Text="Close Me" />


</asp:Panel>


</div>


</form>


</body>




Then in the code behind, add the following code. On clicking the Button, we have added code to create another button dynamically which contains functionality to show and hide a ModalPopUp

C#


protected void Button1_Click(object sender, EventArgs e)


{


    Button btnNew = new Button();


    btnNew.ID = "Button2";


    btnNew.Text = "Click Me to Show PopUp";


 


    AjaxControlToolkit.ModalPopupExtender modalPop =


        new AjaxControlToolkit.ModalPopupExtender();


    modalPop.ID = "popUp";


    modalPop.PopupControlID = "ModalPanel";


    modalPop.TargetControlID = "Button2";


    modalPop.DropShadow = true;


    modalPop.CancelControlID = "btnCancel";


 


    this.Panel1.Controls.Add(modalPop);


    this.Panel1.Controls.Add(btnNew);


 


}




VB.NET


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)


        Dim btnNew As New Button()


        btnNew.ID = "Button2"


        btnNew.Text = "Click Me to Show PopUp"


 


        Dim modalPop As New AjaxControlToolkit.ModalPopupExtender()


        modalPop.ID = "popUp"


        modalPop.PopupControlID = "ModalPanel"


        modalPop.TargetControlID = "Button2"


        modalPop.DropShadow = True


        modalPop.CancelControlID = "btnCancel"


 


        Me.Panel1.Controls.Add(modalPop)


        Me.Panel1.Controls.Add(btnNew)


 


    End Sub



Bookmark this link on del.icio.us (saved by 0 users)

Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter
Others Also Read..

comments

2 Responses to "How to Create an ASP.NET AJAX ModalPopUp Extender Dynamically"
  1. Anonymous said...
    October 22, 2009 5:53 AM

    This code is not wirking man rather it shows error "Control 'popUp' of type 'ModalPopupExtender' must be placed inside a form tag with runat=server.
    So how can I assign runat=server to dynamically created modalpopup extender.

  2. Suprotim Agarwal said...
    October 23, 2009 7:05 AM

    You could alternatively add a PlaceHolder control to the page and then add these controls to it. Most of the times, it solves the issue.

 

Copyright 2010 All Rights Reserved DevCurry.com by Suprotim Agarwal