如何将Youtube视频添加到Web表单 [英] How to add Youtube video to a web form

查看:116
本文介绍了如何将Youtube视频添加到Web表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用文字控件在我的网络表单中添加YouTube视频。如何将此添加到我的webform

Am trying to add a youtube video in my webform using a literal control. How can i add this to my webform

推荐答案

亲爱的朋友使用此概念



Dear Friend Use this Concept

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;
  }







protected void btnSubmit_Click(object sender,EventArgs e)

{



SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [sqlCon]。ToString());

string url = txtLink .Text;

if(url.Contains(youtube.com))

{

string ytFormattedUrl = GetYouTubeID(url );



if(!CheckDuplicate(ytFormattedUrl))

{

DataSet ds = new DataSet() ;

SqlDataAdapter ad = new SqlDataAdapter(select * from YouTubeVideos,con);

ad.Fill(ds);

if(b) ds.Tables [0] .Rows.Count == 0)

{

SqlCommand cmd = new SqlCommand(INSERT INTO YouTubeVideos([url])VALUES('' + ytFormattedUrl +''),con);

使用(con)

{

con.Open();

int result = cmd.ExecuteNonQuery();

if(result!= -1)

{

Repeater1.DataBind();

}

否则

{Response.Write(插入新网址时出错!); }

con.Close();



}

}

else

{

Response.Write(你已经上传了视频);

}

}

else {Response.Write(此视频已存在于我们的数据库中!); }

}

其他

{

Response.Write(此网址不是有效的YOUTUBE视频链接因为它不包含youtube.com);

}

}





//已经退出网址

public bool CheckDuplicate(string youTubeUrl)

{

bool exists = false;



SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [sqlCon]。ToString());

SqlCommand cmd = new SqlCommand(String。格式化(选择*来自YouTube视频,其中url =''{0}'',youTubeUrl),con);



使用(con)

{

con.Open();

SqlDataReader dr = cmd.ExecuteReader();

dr.Read();

存在=(dr.HasRows)? true:false;

}



返回存在;

}

<设计代码中你应该使用转发器控制的









protected void btnSubmit_Click(object sender, EventArgs e)
{

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlCon"].ToString());
string url = txtLink .Text;
if (url.Contains("youtube.com"))
{
string ytFormattedUrl = GetYouTubeID(url);

if (!CheckDuplicate(ytFormattedUrl))
{
DataSet ds = new DataSet();
SqlDataAdapter ad = new SqlDataAdapter("select * from YouTubeVideos", con);
ad.Fill(ds);
if (ds.Tables[0].Rows.Count == 0)
{
SqlCommand cmd = new SqlCommand("INSERT INTO YouTubeVideos ([url]) VALUES (''" + ytFormattedUrl + "'')", con);
using (con)
{
con.Open();
int result = cmd.ExecuteNonQuery();
if (result != -1)
{
Repeater1.DataBind();
}
else
{ Response.Write("Error inserting new url!"); }
con.Close();

}
}
else
{
Response.Write("already you uploaded the video");
}
}
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");
}
}


//For already Exit Url
public bool CheckDuplicate(string youTubeUrl)
{
bool exists = false;

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlCon"].ToString());
SqlCommand cmd = new SqlCommand(String.Format("select * from YouTubeVideos where url=''{0}''", youTubeUrl), con);

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

return exists;
}

in design code you should use repeater control


<asp:Repeater ID="Repeater1" runat="server" DataSourceID ="SqlDataSource1">
    <ItemTemplate>
    <object width="480" height="385"><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, "Items") %>' type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385">
    </embed>
    </object>
    <br />
    </ItemTemplate>
    </asp:Repeater>









你理解并尝试这个概念......





You understand and try this concept...


这篇关于如何将Youtube视频添加到Web表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆