Saturday, July 31, 2010

YouTube videos on ASP.NET page with AJAX

For embedding YouTube videos on ASP.NET web pages we need Literal control, DropDownList (its optional)  and of course code, here the code is in C#.


see Screen Shot:

Front End: File name : Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <br />
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <div style="width: 649px; position: relative; top: -1px; left: 177px">
                    <br />
                    <br />
                    <asp:DropDownList ID="lstSelectVideo" runat="server" AutoPostBack="True" Height="16px"
                        Width="220px" OnSelectedIndexChanged="lstSelectVideo_SelectedIndexChanged">
                        <asp:ListItem Selected="True" Value="66TuSJo4dZM">Inception Movie Trailer</asp:ListItem>
                        <asp:ListItem Value="C6RU5y2fU6s">The Expandables Trailer </asp:ListItem>
                        <asp:ListItem Value="RXZY_XRjABs">Despicable Me Trailer </asp:ListItem>
                    </asp:DropDownList>
                    <br />
                    <!--This control is to display video -->
                    <asp:Literal ID="litVideo" runat="server"></asp:Literal>
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>


 


Back end file name : Default.aspx.cs
//****************************************************************************
// Module           :   Default.aspx.cs
// Type             :   ASP.NET web page code behind
// Developer        :   Kirtisagar Malshet (Suraj)
// DateCreated      :   07/31/2010
// LastModified     :   07/31/2010


//****************************************************************************
// TERMS OF USE     :   ALL YouTube CONTENT IS SHOWN AS DEMO SAMPLE ONLY
//                  :   You can use it at your sole risk
//****************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    VideoExtractor ve = new VideoExtractor(); 
    protected void Page_Load(object sender, EventArgs e)
    {
        litVideo.Text = ve.getVideo(lstSelectVideo.SelectedValue);
    }

      
    protected void lstSelectVideo_SelectedIndexChanged(object sender, EventArgs e)
    {
               
       litVideo.Text= ve.getVideo(lstSelectVideo.SelectedValue);

     }
}


Code file: VideoExtractor.cs
//****************************************************************************
// Module           :   VideoExtractor.cs
// Type             :   ASP.NET code 
// Developer        :   Kirtisagar Malshet (Suraj)
// DateCreated      :   07/31/2010
// LastModified     :   07/31/2010


//****************************************************************************
// TERMS OF USE     :   ALL YouTube CONTENT IS SHOWN AS DEMO SAMPLE ONLY
//                  :   You can use it at your sole risk
//****************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

/// 
/// Summary description for VideoExtractor
/// 
public class VideoExtractor
{
  public string getVideo(string strVideoId)
    {
       
        StringBuilder strb = new StringBuilder();
        strb.Append(@"<embed src='http://www.youtube.com/v/");

        // add video id
        strb.Append(strVideoId);

        strb.Append("&rel=0");

        // allow full screen
        strb.Append("&fs=1");

        // closing single quote after parameter list
        strb.Append("' ");
        
        strb.Append("type='application/x-shockwave-flash' ");

        // add id
        strb.Append("id='youTubePlayer" + DateTime.Now.Millisecond.ToString() + "' ");

        strb.Append("allowscriptaccess='always' enableJavascript ='false' ");

        // set parameters: allowfullscreen
        strb.Append("allowfullscreen='true' ");

        // set width 16:9 Ratio
        strb.Append("width='" + "640" + "' ");

        // set height
        strb.Append("height='" + "360" + "' ");
        
        strb.Append(@"></embed>");

        // get final script
       string vid = strb.ToString();

       return vid;
       

    }
}



Download the complete source code here