2017-02-16 85 views
0

我在ASPX文件中使用此代码:如何在后面的代码中设置视频标签的来源?

<video width="320" height="240" autoplay="autoplay"> 
    <source id="videoSrc" runat="server" type="video/mp4"/> 
    Your browser does not support the video tag. 
</video> 

,但是当我在后面的代码使用此代码:

protected void Page_Load(object sender, EventArgs e) 
{ 
    videoSrc.Src= "UploadMovies/"+Request.QueryString["id"]+"/high.mp4"; 
} 

,并叫我的页面myPage.aspx?id=1我得到<source>此错误:

The base class includes the field 'videoSrc', but its type (System.Web.UI.HtmlControls.HtmlSource) is not compatible with the type of control (System.Web.UI.HtmlControls.HtmlGenericControl).

回答

1

你可以在这里做几件事。

首先是完全摆脱<source>并使用src属性。你需要让video服务器端控制,但不会导致错误:

<video width="320" height="240" autoplay="autoplay" id="video" runat="server"> 
</video> 

video.Attributes["src"] = "UploadMovies/"+Request.QueryString["id"]+"/high.mp4"; 

的另一件事是背后有功能的代码,会给你一个视频链接:

<video width="320" height="240" autoplay="autoplay"> 
    <source type="video/mp4" src='<%= GetVideoLink() %>'/> 
</video> 

protected string GetVideoLink() 
{ 
    return "UploadMovies/"+Request.QueryString["id"]+"/high.mp4"; 
} 

在这里您还可以使用参数,并有几个<source>标签来支持回退。

至于你所看到的错误,为什么会发生这种情况并不明显。 HtmlSource是source标记的正确控制类型,但不清楚为什么ASP.NET决定将其视为通用html控件。但你可以试试this workaround

相关问题