2013-05-01 63 views
0

我需要一个代码为youtube缩略图与播放按钮,它从URL获取视频ID后点击播放视频将播放弹出...任何人都可以建议...?需要代码为youtube缩略图与播放按钮后,我点击播放视频将弹出

的.aspx代码:

<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代码

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> 

从ablove代码我得到的数据列表YouTube视频,但是当我点击它,它起着因为它是在200像素* 200像素。

+0

你会举个例子吗? – Gaurav 2013-05-01 08:45:39

回答

0

我会做不同的。

个人而言,我不会嵌入视频,因为这是问题!相反,嵌入视频或类似图片,onclick然后加载适当的视频在一个新的窗口。

更新您的模板太

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

您可能还需要更新您的Insert命令,因为它可以允许SQL注入。