Pages

Friday, September 3, 2010

Create a Modal Popup using the Ajax Modal Popup Extender

Hello All,
Here is the sample application for creating a MODAL POPUP for this I am going to use the Ajax ModalPopupExtender control. Before using this you must ensure that the Ajax control tool kit in install in the machine Or make a reference for the AjaxControlToolkit.dll in the application.

Make use of a ScriptManager control in the page is mandatory , so make sure you have used the Script manager control inside the page, If you are using a MASTERPAGE place this Script Manger tag inside the master page. it will be like..

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

Now place the controls inside a PANEL or a DIV which you want to display like the modal pop up and you can also set the background of the panel or the DIV. for example I am using the Login controls as a PopUp

<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Enter userName:-" ForeColor="White">asp:Label>
td>
<td>
<asp:TextBox ID="txtname" runat="server" Text="">asp:TextBox>
td>
tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Enter Password:-" ForeColor="White" >asp:Label>
td>
<td>
<asp:TextBox ID="txtPass" runat="server" Text="">asp:TextBox>
td>
tr>

In order to make use of the Ajax control what you have to do is make reference of the Ajax DLL in the application. And the page in which you are going to use the control REGISTER the control like this, to provide a control prefix name.

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxtoolkit" %>

After that you need to make use of the ModalPopupExtender control like this...

<ajaxtoolkit:modalpopupextender backgroundcssclass="modalBackground" dropshadow="False"
okcontrolid
="btnOk" cancelcontrolid="btnClose" runat="server" popupcontrolid="Panel1"
id
="ModalPopupExtender1" targetcontrolid="btnPOPUP" />

Here Every Property has Its Mening..

1. backgroundcssclass -- Defines the CSS used for the popup.
2. dropshadow -- Used to set the Shadow Effect, the popup area will become like a Shadow
3. okcontrolid -- Used to set the Ok control or submit control for popup
4. cancelcontrolid -- Used to set the Cancel control for popup
5. popupcontrolid -- Defines the Panle or DIV which will be display like popup.
6. id -- Define the Id of the extender control
7. targetcontrolid -- This defines the control by whihc the pop up will be invoked.


I hope you understood the property and how to use the control, Below are the actual file for the sample application..

Master Page File.

lt;%@ Master Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeFile=&quot;Site.master.cs&quot; Inherits=&quot;SiteMaster&quot; %&gt;

&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml" xml:lang="en">
&lt;head runat=&quot;server&quot;&gt;

&lt;style type=&quot;text/css&quot;&gt;
.
modalBackground {
background-color
:#414141;
filter
:alpha(opacity=70);
opacity
:0.7;
}
&lt;/style&gt;
&lt;title&gt;&lt;/title&gt;
&lt;link href=&quot;~/Styles/Site.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
&lt;asp:ContentPlaceHolder ID=&quot;HeadContent&quot; runat=&quot;server&quot;&gt;
&lt;/asp:ContentPlaceHolder&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form runat=&quot;server&quot;&gt;
&lt;asp:ScriptManager ID=&quot;ScriptManager1&quot; runat=&quot;server&quot;&gt;
&lt;/asp:ScriptManager&gt;
&lt;div class=&quot;page&quot;&gt;
&lt;div class=&quot;header&quot;&gt;
&lt;div class=&quot;title&quot;&gt;
&lt;h1&gt;
My ASP.
NET Application
&lt;/h1&gt;
&lt;/div&gt;
&lt;div class=&quot;loginDisplay&quot;&gt;
&lt;asp:LoginView ID=&quot;HeadLoginView&quot; runat=&quot;server&quot; EnableViewState=&quot;false&quot;&gt;
&lt;AnonymousTemplate&gt;
[ &lt;a href=&quot;~/Account/Login.aspx&quot; ID=&quot;HeadLoginStatus&quot; runat=&quot;server&quot;&gt;Log In&lt;/a&gt; ]
&lt;/AnonymousTemplate&gt;
&lt;LoggedInTemplate&gt;
Welcome
&lt;span class=&quot;bold&quot;&gt;&lt;asp:LoginName ID=&quot;HeadLoginName&quot; runat=&quot;server&quot; /&gt;&lt;/span&gt;!
[ &lt;asp:LoginStatus ID=&quot;HeadLoginStatus&quot; runat=&quot;server&quot; LogoutAction=&quot;Redirect&quot; LogoutText=&quot;Log Out&quot; LogoutPageUrl=&quot;~/&quot;/&gt; ]
&lt;/LoggedInTemplate&gt;
&lt;/asp:LoginView&gt;
&lt;/div&gt;
&lt;div class=&quot;clear hideSkiplink&quot;&gt;
&lt;asp:Menu ID=&quot;NavigationMenu&quot; runat=&quot;server&quot; CssClass=&quot;menu&quot; EnableViewState=&quot;false&quot; IncludeStyleBlock=&quot;false&quot; Orientation=&quot;Horizontal&quot;&gt;
&lt;Items&gt;
&lt;asp:MenuItem NavigateUrl=&quot;~/Default.aspx&quot; Text=&quot;Home&quot;/&gt;
&lt;asp:MenuItem NavigateUrl=&quot;~/About.aspx&quot; Text=&quot;About&quot;/&gt;
&lt;/Items&gt;
&lt;/asp:Menu&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;main&quot;&gt;
&lt;asp:ContentPlaceHolder ID=&quot;MainContent&quot; runat=&quot;server&quot;/&gt;
&lt;/div&gt;
&lt;div class=&quot;clear&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;footer&quot;&gt;

&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

The Default.aspx file code.

&lt;%@ Page Title=&quot;Home Page&quot; Language=&quot;C#&quot; MasterPageFile=&quot;~/Site.master&quot; AutoEventWireup=&quot;true&quot;
CodeFile
=&quot;Default.aspx.cs&quot; Inherits=&quot;_Default&quot; %&gt;
&lt;%@ Register Assembly=&quot;AjaxControlToolkit&quot; Namespace=&quot;AjaxControlToolkit&quot; TagPrefix=&quot;ajaxtoolkit&quot; %&gt;

&lt;asp:Content ID=&quot;HeaderContent&quot; runat=&quot;server&quot; ContentPlaceHolderID=&quot;HeadContent&quot;&gt;
&lt;/asp:Content&gt;
&lt;asp:Content ID=&quot;BodyContent&quot; runat=&quot;server&quot; ContentPlaceHolderID=&quot;MainContent&quot;&gt;
&lt;asp:Button ID=&quot;btnPOPUP&quot; runat=&quot;server&quot; Text=&quot;Display PopUp&quot; /&gt;
&lt;ajaxtoolkit:modalpopupextender backgroundcssclass=&quot;modalBackground&quot; dropshadow=&quot;False&quot;
okcontrolid
=&quot;btnOk&quot; cancelcontrolid=&quot;btnClose&quot; runat=&quot;server&quot; popupcontrolid=&quot;Panel1&quot;
id
=&quot;ModalPopupExtender1&quot; targetcontrolid=&quot;btnPOPUP&quot; /&gt;
&lt;asp:Panel ID=&quot;Panel1&quot; runat=&quot;server&quot; CssClass=&quot;modalPopup&quot; Style=&quot;display: none;&quot;&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text=&quot;Enter userName:-&quot; ForeColor=&quot;White&quot;&gt;&lt;/asp:Label&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;asp:TextBox ID=&quot;txtname&quot; runat=&quot;server&quot; Text=&quot;&quot;&gt;&lt;/asp:TextBox&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;asp:Label ID=&quot;Label2&quot; runat=&quot;server&quot; Text=&quot;Enter Password:-&quot; ForeColor=&quot;White&quot; &gt;&lt;/asp:Label&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;asp:TextBox ID=&quot;txtPass&quot; runat=&quot;server&quot; Text=&quot;&quot;&gt;&lt;/asp:TextBox&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;asp:Button ID=&quot;btnOk&quot; runat=&quot;server&quot; Text=&quot;Ok&quot; /&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;asp:Button ID=&quot;btnClose&quot; runat=&quot;server&quot; Text=&quot;Close Me&quot; /&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/asp:Panel&gt;
&lt;/asp:Content&gt;

Now please take a look of the output of the application.

First Screen



Second Screen




Please Decode the files using a HTML DECODER (http://centricle.com/tools/html-entities/) to get the actual file. Hope you have like this article, Please feel free to post your ideas and the comments.

Yours,

Anil Kumar Pandey

No comments:

Post a Comment

Kontera