How To Play YouTube Video In ASP.NET Web Application Using AJAX

Introduction

In this article, I am going to explain how to play a YouTube video in ASP.NET web application using AJAX Modal Popup. While working with any web application in any technology, be it ASP.NET, or PHP, or Java, or any other language, sometimes it’s necessary to play a media file or any media online on your web page based on the client’s requirement. Recently, a few days ago, I got a new requirement from my client to play a YouTube video on the webpage.

Actually, they wanted one textbox and one button with the name Play Video on the webpage. When a user enters the URL of the YouTube video in the given textbox and clicks on the button, one Modal Popup window will open and load the video from YouTube to play that video in a popup window.

Implementation

First, we need to register the AJAX Window Toolkit with our web application.
  1. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>  
 Now, we will write the HTML code with one textbox and one button, as following.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="CS" %>  
  2.   
  3. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8.     <style type="text/css">  
  9.         body  
  10.         {  
  11.             font-family: Arial;  
  12.             font-size: 10pt;  
  13.         }  
  14.         .modalBackground  
  15.         {  
  16.             background-color: Black;  
  17.             filter: alpha(opacity=60);  
  18.             opacity: 0.6;  
  19.         }  
  20.         .modalPopup  
  21.         {  
  22.             background-color: #FFFFFF;  
  23.             width: 500px;  
  24.             border: 3px solid #0DA9D0;  
  25.             padding: 0;  
  26.         }  
  27.         .modalPopup .header  
  28.         {  
  29.             background-color: #2FBDF1;  
  30.             height: 30px;  
  31.             color: White;  
  32.             line-height: 30px;  
  33.             text-align: center;  
  34.             font-weight: bold;  
  35.         }  
  36.         .modalPopup .body  
  37.         {  
  38.             min-height: 50px;  
  39.             padding:5px;  
  40.             line-height: 30px;  
  41.             text-align: center;  
  42.             font-weight: bold;  
  43.         }  
  44.         .Input {  
  45.     background-color: #2f8cf1; /* Green */  
  46.     border: none;  
  47.     color: white;  
  48.     text-align: center;  
  49.     text-decoration: none;  
  50.     display: inline-block;  
  51.     font-size: 16px;  
  52.     margin: 4px 2px;  
  53.     cursor: pointer;  
  54. }  
  55.   .button {  
  56.     background-color: #ff6a00; /* Green */  
  57.     border: none;  
  58.     color: white;  
  59.     text-align: center;  
  60.     text-decoration: none;  
  61.     display: inline-block;  
  62.     font-size: 16px;  
  63.     margin: 4px 2px;  
  64.     cursor: pointer;  
  65. }  
  66.   
  67. .button1 {padding: 10px 24px;}  
  68.     </style>  
  69. </head>  
  70. <body>  
  71.     <form id="form1" runat="server">  
  72. <cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">  
  73. </cc1:ToolkitScriptManager>  
  74. <asp:TextBox ID="txtUrl" runat="server" CssClass="button1 Input" Width="370" Text = "https://www.youtube.com/watch?v=d4TKdE8bmSI" />  
  75. <asp:Button ID="btnShow" runat="server" CssClass="button1 button" Text="Play Video" OnClientClick="return ShowModalPopup()" />  
  76. <asp:LinkButton ID="lnkDummy" runat="server"></asp:LinkButton>  
  77. <cc1:ModalPopupExtender ID="ModalPopupExtender1" BehaviorID="mpe" runat="server"  
  78.     PopupControlID="pnlPopup" TargetControlID="lnkDummy" BackgroundCssClass="modalBackground" CancelControlID = "btnClose">  
  79. </cc1:ModalPopupExtender>  
  80. <asp:Panel ID="pnlPopup" runat="server" CssClass="modalPopup" Style="display: none">  
  81.     <div class="header">  
  82.         Youtube Video  
  83.     </div>  
  84.     <div class="body">  
  85.         <iframe id = "video" width="420" height="315" frameborder="0" allowfullscreen></iframe>  
  86.         <br />  
  87.         <br />  
  88.         <asp:Button ID="btnClose" runat="server" Text="Close" />  
  89.     </div>  
  90. </asp:Panel>  
  91.     <script type="text/javascript">  
  92.         function ShowModalPopup() {  
  93.             var url = $get("<%=txtUrl.ClientID %>").value;  
  94.             url = url.split('v=')[1];  
  95.             $get("video").src = "//www.youtube.com/embed/" + url  
  96.             $find("mpe").show();  
  97.             return false;  
  98.         }  
  99.         function HideModalPopup() {  
  100.             $get("video").src = "";  
  101.             $find("mpe").hide();  
  102.             return false;  
  103.         }  
  104.     </script>  
  105.     </form>  
  106. </body>  
  107. </html>  

For, opening the popup window and playing the video, you need to write the following JavaScript code.

  1. <script type="text/javascript">  
  2.         function ShowModalPopup() {  
  3.             var url = $get("<%=txtUrl.ClientID %>").value;  
  4.             url = url.split('v=')[1];  
  5.             $get("video").src = "//www.youtube.com/embed/" + url  
  6.             $find("mpe").show();  
  7.             return false;  
  8.         }  
  9.         function HideModalPopup() {  
  10.             $get("video").src = "";  
  11.             $find("mpe").hide();  
  12.             return false;  
  13.         }  
  14.     </script>  

Actually, when a user clicks on Play Video button, this JavaScript function will be called and will open a popup window that fetches the video from YouTube based on the entered video URL. Now, when the user clicks on the close button in popup function of JavaScript, the “HideModelPopup” function will be called and the Modal Popup will close.

Output Screen


How to play YouTube video in ASP.Net 
After you click the Play Video button, the Modal Popup will open and show/play the YouTube video.

Play YouTube Video 

Conclusion

In this write-up, I explained how to play YouTube videos on the web page using AJAX Control Toolkit. I hope this article helps the beginner developers to build a better application. 

Codingvila
Codingvila is an educational website, developed to help tech specialists/beginners.