Tuesday, January 17, 2012

ModalPopUp from serverside in Asp.Net (C#)

 The ModalPopup control in the AJAX Control Toolkit offers a simple way to create a modal popup using client-side popup. However some scenarios require that the opening of the modal popup is triggered on the server-side.


Steps :
Take asp.net button as shown below
<asp:Button ID="btnModalPopUp" runat="server" Text="Show 
Modal Popup" OnClick = "btnModalPopUp_Click" />

Then, you need a panel to display popup.


Code for panel

<asp:Panel ID="pnlPoppUp" runat="server" Width="500px">

 ASP.NET AJAX is a free framework for quickly creating a
new generation of more efficient, more interactive and 
highly-personalized Web experiences that work across all the 
most popular browsers.<br />


<asp:Button ID="OKButton" runat="server" Text="Close" />
</asp:Panel>

Next add the ModalPopup control from the ASP.NET AJAX Toolkit to the page. Set properties for the button which loads the control, the button which makes it disappear, and the ID of the actual popup as below.

<ajaxToolkit:ModalPopupExtender ID="myPopup" runat="server" 
 TargetControlId = "btnModalPopUp" PopupControlID = "pnlPoppUp" 
 OkControlID="OKButton" />
 
As with all web pages based on ASP.NET AJAX; the Script Manager is
required to load the necessary JavaScript libraries for the 
different target browsers as below:

<asp:ScriptManager ID="scripmanager1" runat="server" />

As you can see, a click on the button generates a postback and executes  btnModalPopUp_Click() method on the server. In this method, a JavaScript function called ShowPopup() is executed to be exact, the JavaScript function will be executed once the page has been loaded:
Code for buttonclick
 
 protected void btnModalPopUp_Click(object sender, EventArgs e)
 {
      ClientScript.RegisterStartupScript(this.GetType(), 
 "key", "ShowPopup();", true);
 }

Then  the job of ShowPopup() is to display the ModalPopup. The ShowPopup() function is executed once the complete HTML page has been loaded. At that moment, however, the ASP.NET AJAX framework has not been fully loaded yet. Therefore, the ShowPopup() function just sets a variable that the ModalPopup control must be shown later on.  Write the below code in the header tag:

<script type="text/javascript">
 var launch = false;
 function ShowPopup() 
 {
 launch = true;
 }
function pageLoad() 
 {
 if (launch) 
 {
 $find("myPopup").show();
 }
 }
</script>
 
Full code as shown below :
In aspx page Before closing header tag
 
<script type="text/javascript">
 var launch = false;
 function ShowPopup() 
 {
 launch = true;
 }
function pageLoad() 
 {
 if (launch) 
 {
 $find("myPopup").show();
 }
 }
</script>

after header tag
<asp:Button ID="btnModalPopUp" runat="server" 
Text="Show Modal Popup" OnClick = "btnModalPopUp_Click" />

<asp:Panel ID="pnlPoppUp" runat="server" Width="500px">

 ASP.NET AJAX is a free framework for quickly creating a new generation
of more efficient, more interactive and highly-personalized Web
experiences that work across all the most popular browsers.<br />

 <asp:Button ID="OKButton" runat="server" Text="Close" />
</asp:Panel>
 
<asp:ScriptManager ID="scripmanager1" runat="server" />

Server Side :
 protected void btnModalPopUp_Click(object sender, EventArgs e)

 {
      ClientScript.RegisterStartupScript(this.GetType(), 
 "key", "ShowPopup();", true);
 }

No comments:

Post a Comment