need a code for youtube thumbnail with play button after i click to play the video will play in popup

别说谁变了你拦得住时间么 提交于 2019-12-08 11:28:20

问题


i need a code for youtube thumbnail with play button which fetch the video id from url after i click to play the video will play in popup... can anyone suggest...?

.aspx code:

<asp:DataList ID="DataList3" runat="server" RepeatDirection="Horizontal" 
            RepeatColumns="7" Width="600px" DataSourceID="SqlDataSource1">
        <ItemTemplate>
             Description:
             <asp:Label ID="DescriptionLabel" runat="server" 
                 Text='<%# Eval("Description") %>' />
             <br />
              <object width="200" height="200"><param name="movie" value='<%#DataBinder.Eval(Container.DataItem, "url") %>'></param>
<param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param>
<embed src='<%#DataBinder.Eval(Container.DataItem, "url") %>' type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="200" height="200">
</embed>
</object>
             <br />
             <br />
        </ItemTemplate>
        </asp:DataList>

.aspx.cs code

    string url = txturl.Text;
    if (url.Contains("youtube.com"))
    {
        string ytFormattedUrl = GetYouTubeID(url);

        if (!CheckDuplicate(ytFormattedUrl))
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO YoutubeVideo VALUES ('" + ytFormattedUrl + "','" + lbluser.Text + "','" + title.Text + "','" + DateTime.Today.Date + "','" + DateTime.Now.TimeOfDay + "')", con);
            using (con)
            {
                con.Open();
                int result = cmd.ExecuteNonQuery();
                if (result != -1)
                {
                    DataList1.DataBind();
                }
                else { Response.Write("Error inserting new url!"); }
            }
        }
        else { Response.Write("This video already exists in our database!"); }
    }
    else
    {
        Response.Write("This URL is not a valid YOUTUBE video link because it does not contain youtube.com in it");
    }

}

private string GetYouTubeID(string youTubeUrl)
{
    //RegEx to Find YouTube ID
    Match regexMatch = Regex.Match(youTubeUrl, "^[^v]+v=(.{11}).*",
                       RegexOptions.IgnoreCase);
    if (regexMatch.Success)
    {
        return "http://www.youtube.com/v/" + regexMatch.Groups[1].Value +
               "&hl=en&fs=1";
    }
    return youTubeUrl;
}

public bool CheckDuplicate(string youTubeUrl)
{
    bool exists = false;

    SqlCommand cmd = new SqlCommand(String.Format("select * from YoutubeVideo where url='{0}'", youTubeUrl), con);

    using (con)
    {
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        dr.Read();
        exists = (dr.HasRows) ? true : false;
    }

    return exists;
}</code>

from ablove code i get youtube video in datalist but when i click to it it plays as it is in 200px*200px.


回答1:


I would do this differently.

Personally, I'd not embed the video as this is the problem! Instead, embed a picture of the video or similar, the onclick then loads the appropriate video in a new window.

Update your template too

<ItemTemplate>
             Description:
             <asp:Label ID="DescriptionLabel" runat="server" 
                 Text='<%# Eval("Description") %>' />
             <br />
         <a href="wherever" javacriptCommandToOpenPopUpHere><img src="picture.jpg"></a>
<br /><br />
</ItemTemplate>

You may also need to update your Insert command as it may allow for SQL injection.



来源:https://stackoverflow.com/questions/16315110/need-a-code-for-youtube-thumbnail-with-play-button-after-i-click-to-play-the-vid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!