2017-02-16 84 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

相關問題