我们要来修改上面的代码,修改后的代码如下:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- namespace CustomControls
- {
- [ToolboxData("<{0}:VideoPlayer runat=server></{0}:VideoPlayer>")]
- public class VideoPlayer : WebControl
- {
-
- }
- }
注意的是,默认的代码中,为控件生成了Text属性,然而这里并不需要,因此我们把
[DefaultProperty("Text")]
这行删除掉。
步骤4 继续为控件增加属性
根据之前的介绍,我们开始为控件增加一些属性,要增加的属性如下:
VideoUrl:指定视频播放的地址。
PosterUrl: 这个是当没有视频时,显示的替代图片的地址。
AutoPlay:指示视频是否自动装载播放。
DisplayControlButtons: 指示是否显示或者隐藏播放的相关按钮。
Loop: 指示视频是否自动播放。
增加属性后的代码如下:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace CustomControls
- {
- [ToolboxData("<{0}:VideoPlayer runat=server></{0}:VideoPlayer>")]
- public class VideoPlayer : WebControl
- {
- private string _Mp4Url;
- public string Mp4Url
- {
- get { return _Mp4Url; }
- set { _Mp4Url = value; }
- }
-
- private string _OggUrl = null;
- public string OggUrl
- {
- get { return _OggUrl; }
- set { _OggUrl = value; }
- }
-
- private string _Poster = null;
- public string PosterUrl
- {
- get { return _Poster; }
- set { _Poster = value; }
- }
-
- private bool _AutoPlay = false;
- public bool AutoPlay
- {
- get { return _AutoPlay; }
- set { _AutoPlay = value; }
- }
-
- private bool _Controls = true;
- public bool DisplayControlButtons
- {
- get { return _Controls; }
- set { _Controls = value; }
- }
-
- private bool _Loop = false;
- public bool Loop
- {
- get { return _Loop; }
- set { _Loop = value; }
- }
- }
- }
步骤5 修改RenderContents方法
服务端控件的主要目的就是向浏览器输出内容。因此,作为开发者,我们就必须设定好我们的控件要向客户端浏览器输出什么样的内容。因此,我们可以重写RenderContents方法即可,如下代码:
- protected override void RenderContents(HtmlTextWriter output)
- {
- }
(责任编辑:admin) |