先上图:
设计思路:
先看JS的Slider怎么用:
$("#slider").slider({
value:100,
min: 0,
max: 500,
step: 50,
slide: function(event, ui) {
$("#amount").val('$' + ui.value);
}
});
因此设计一个从Panel派生的子类,然后再加上一个隐藏域即可以完成此功能,
控件代码:
- public class JQSlider:Panel
- {
- private int min = 0;
- public event EventHandler ValueChanged;//当值改变时事件
- public int Min//最小值
- {
- get { return min; }
- set { min = value; }
- }
- private int max = 100;
- public int Max//最大值
- {
- get { return max; }
- set { max = value; }
- }
- private int value = 0;
- public int Value//当前值
- {
- get { return this.value; }
- set { this.value = value; }
- }
- private int step = 1;
- public int Step//步长
- {
- get { return step; }
- set { step = value; }
- }
- private HiddenField txt;//隐藏域
- public JQSlider()
- {
- txt = new HiddenField();
- this.Controls.Add(txt);
- }
- private bool autoPostBack = false;//自动回调
- public bool AutoPostBack
- {
- get { return autoPostBack; }
- set { autoPostBack = value; }
- }
- protected override void OnLoad(EventArgs e)//重写Load事件
- {
- base.OnLoad(e);
- string val = this.Page.Request[txt.ClientID];//获取隐藏域中的值
- if (!string.IsNullOrEmpty(val))
- {
- value = int.Parse(val);
- }
- txt.Value = "" + value;
- if (ValueChanged != null&&autoPostBack)//判断是否有事件且自动事件回调
- ValueChanged(this, e);
- }
- public override void RenderEndTag(System.Web.UI.HtmlTextWriter writer)
- {
- string postback = "";
- if (autoPostBack)
- {
- postback = "$('form').submit();";
- }
- base.RenderEndTag(writer);
- //输出JS代码
- writer.Write(string.Format("<script>$(function()|$('#{0}').slider(|value:{1},min:{2},max:{3},step:{4},slide:function(e,u)|$('#{0} :hidden').val(u.value);{5}%%);%);</script>",
- this.ClientID,this.Value,this.Min,this.Max,this.Step,postback).Replace("|","{").Replace("%","}"));
- }
- }
使用非常简单,在页面中拖放一个JQueryManager和一个JQSlider,可以设置期Min,Max,Value,Step,AutoPostBack属性和ValueChanged事件.
服务端代码如下:
- <cc1:JQueryManager ID="JQueryManager1" runat="server" Skins="sunny">
- </cc1:JQueryManager>
- <cc1:JQSlider ID="JQSlider1" runat="server" Width="200px" AutoPostBack="True"
- onvaluechanged="JQSlider1_ValueChanged" Step="5">
- </cc1:JQSlider>
客户生成端代码如下:
- <div id="JQSlider1" style="width:200px;">
- <input type="hidden" name="ctl02" id="ctl02" value="25" />
- </div><script>$(function(){$('#JQSlider1').slider({value:25,min:0,max:100,step:5,slide:function(e,u){$('#JQSlider1 :hidden').val(u.value);$('form').submit();}});});</script>
- </div>
哈哈完工!待续中....