如何添加<音频>标签在code页面的内容后面? [英] How to add <audio> tag to Content of Page in Code behind?

查看:184
本文介绍了如何添加<音频>标签在code页面的内容后面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这段音频HTML标记在添加到自code后面的页面Client.RegisterClientScriptBlock()。这可能吗?

 <音频ID =A>< /音频>
<脚本类型=文/ JavaScript的>
    VAR播放器=的document.getElementById(A);
    函数alert(){
        播放器preLOAD =自动。
        player.src =/Sounds/tada.wav;
        player.play();
    }
< / SCRIPT>

的BaseClass

 公共类的BasePage:System.Web.UI.Page
{保护覆盖无效在preRender(EventArgs的发送)
{
   base.On preRender(E);  //添加音频标签}}

Page.aspx

 < ASP:内容ID =内容1ContentPlaceHolderID =日程地址搜索Maincontent=服务器>
 ....
 .....
 ....
< / ASP:内容>

Page.aspx.cs

 公共部分类页:的BasePage
{
   保护无效的Page_Load(对象发件人,EventArgs的发送)
    {}}


解决方案

<音频> 标记?或< HTML和GT; 标签

<音频> 标签,你应该能够与添加

  VAR CTRL =新HtmlGenericControl(音频);
myContainer.Add(CTRL);

其中myContainer中是模板的容器(如DIV)

有关的JavaScript的一部分,你就需要引用音频CTRL的客户端ID在你的JavaScript。你可以在后台建立你的JavaScript,或者只是建立一个JavaScript属性在后台,并把它放入模板。

  VAR scriptText =的String.Format(VAR AUDIOID = {0};ctrl.ClientId);
ClientScript.RegisterClientScriptBlock(this.GetType(),的MyScript,scriptText,真);

然后在模板你的JavaScript

  VAR播放器=的document.getElementById(AUDIOID);

I want to add this audio html tag to the page from code behind in Client.RegisterClientScriptBlock(). Is it possible?

<audio id="A"></audio>
<script type="text/javascript">
    var player = document.getElementById("A");
    function Alert() {
        player.preload = 'auto';
        player.src = "/Sounds/tada.wav";
        player.play();
    }
</script>

BaseClass

public class BasePage : System.Web.UI.Page
{

protected override void OnPreRender(EventArgs e)
{
   base.OnPreRender(e);

  //add audio tag 

}

}

Page.aspx

 <asp:Content ID="Content1" ContentPlaceHolderID="MainContent"  runat="server">
 ....
 .....
 ....
</asp:Content>

Page.aspx.cs

public partial class Page: BasePage
{
   protected void Page_Load(object sender, EventArgs e)
    {  }

}

解决方案

<audio> tag? or <html> tag?

<audio> tag you should be able to add with

var ctrl = new HtmlGenericControl("audio");
myContainer.Add(ctrl);

where myContainer is a container in your template (eg a div)

For the javascript part, you would need to reference the client id of the audio ctrl in your javascript. You could build up your javascript in the backend, or just build up a javascript property in the backend and put it into the template.

var scriptText = String.Format("var audioId = {0};", ctrl.ClientId);
ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", scriptText, true);

and then in your javascript in the template

var player = document.getElementById(audioId); 

这篇关于如何添加&LT;音频&GT;标签在code页面的内容后面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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