Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey all,
How can i embed utube video with only c# code in asp.net. which control will help me to achieve this task. currently i am using label control, which is obviously not logically correct because of its string kind of nature and is not string and it is giving me some kind of invalid reference error
yt video embed tag is
<iframe width="560" height="315" src="https://www.youtube.com/embed/9bi9eDZUJPQ" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>


What I have tried:

C#
label lbl= new label();
label.text="<iframe width='560' height='315' src='https://www.youtube.com/embed/9bi9eDZUJPQ' frameborder='0' allow='accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture' allowfullscreen></iframe>";
Posted
Updated 1-Oct-19 2:42am
v2
Comments
F-ES Sitecore 1-Oct-19 5:47am    
Use an asp:Literal rather than an asp:Label.
Maciej Los 1-Oct-19 6:58am    
Sounds like an answer...
ZurdoDev 1-Oct-19 7:12am    
I second him. ^
Aftab Iqbal Clips 1-Oct-19 8:02am    
Used the literal despite of label but still getting the same error
ZurdoDev 1-Oct-19 8:20am    
How can we possibly help you if you won't share what the error is or what the line of code is that causes the error?

1 solution

Here is a working example that I've tried with Literal control as F-ES Sitecore suggested.

Your aspx page

ASP.NET
<div>
     <asp:Panel ID="Panel1" runat="server"></asp:Panel>
</div>

Your code behind
C#
var videoFrame = new Literal();
videoFrame.Text = string.Format(@"<iframe width=""628"" height=""374"" src=""{0}"" frameborder=""0"" allowfullscreen></iframe>", "https://www.youtube.com/embed/9bi9eDZUJPQ");
Panel1.Controls.Add(videoFrame);
 
Share this answer
 
v2
Comments
Aftab Iqbal Clips 1-Oct-19 9:01am    
it means that this can not be achieved with purely using c# in asp.net.
what are your thoughts bro, i will deeply appreciate
dnxit 1-Oct-19 9:29am    
above code is purely C# just one Panel I've used to add that literal control.
Aftab Iqbal Clips 2-Oct-19 5:39am    
yes it was in c# 99.9%. i think string.format is key regarding this matter and i was missing it before can you please put some light on " keyword "@" , why are we using it
dnxit 2-Oct-19 15:06pm    
to keep multiple double quotes like width=""628"" height=""374"", which will result width="628" height="374" after rendering.
Aftab Iqbal Clips 3-Oct-19 6:34am    
GOT IT
I HAVE POSTED ANOTHER QUESTION REGARDING THIS. MY NEW TASK IS TO MAKE IT MORE DYNAMIC CAN YOU PLEASE HAVE A LOOK AT THIS
https://www.codeproject.com/Questions/5247540/How-do-I-embed-youtube-videos-to-data-bound-contro

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900