How to embed YouTube video in asp.net c#

In this article, we will learn how to display /play YouTube video in browser or website. Here we pass string VideoUrl where we direct pass the YouTube channel url and if you want you can pass customize url then we you add a textbox and there we pass the values. And here we use on page load but you can also use click on button.


Source Code:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="youtubevedio.aspx.cs" Inherits="youtubevedio" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

    <div id="divVideo" runat="server">
    </div>
    </form>
</body>
</html>

Code behind:-

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

public partial class youtubevedio : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)

    {
        SetVideoForPlay();
    }
    protected void SetVideoForPlay()
    {
        string VideoUrl = "https://www.youtube.com/watch?v=z4gCpvZhRbw&index=2&list=PLib8Q64STW-vAm8qORHaBOWnNOWR2eznR";
        string youcode = VideoUrl.Substring(VideoUrl.LastIndexOf("v=") + 2);
        if (youcode.Contains("&"))
            youcode = youcode.Substring(0, youcode.LastIndexOf("&"));
        string frame = @"<object width='{0}' height='{1}' data='http://www.youtube.com/v/{2}&autoplay=1' codetype='application/x-shockwave-flash'>
  &ltparam name='movie' value='http://www.youtube.com/v/{2}&autoplay=1' ></param></object>";
        string fWidth = "600px";
        string fHeight = "500px";
        divVideo.InnerHtml = String.Format(frame, fWidth, fHeight, youcode);
    }
}
Out-Put:-



How to embed YouTube video in asp.net c# How to embed YouTube video in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 5:54:00 PM Rating: 5

No comments:

Powered by Blogger.