using System;
using System.ComponentModel;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using NewLife.Exceptions;
namespace XControl
{
/// <summary>
/// Êý×ÖÊäÈë¿Ø¼þ¡£Ö»ÄÜÊäÈëÊý×Ö£¬²¢¿ÉÒԹ涨·¶Î§¡¢¼ä¸ô¡£
/// <remarks>×î´ó×îСֵֻ¶ÔÕýÕûÊýÓÐЧ</remarks>
/// </summary>
[Description("Êý×ÖÊäÈë¿Ø¼þ")]
[ToolboxData("<{0}:NumberBox runat=server></{0}:NumberBox>")]
[ToolboxBitmap(typeof(TextBox))]
[ControlValueProperty("Value")]
public class NumberBox : TextBox
{
/// <summary>³õʼ»¯Êý×ÖÊäÈë¿Ø¼þµÄÑùʽ¡£</summary>
public NumberBox()
: base()
{
this.ToolTip = "Ö»ÄÜÊäÈëÊý×Ö£¡";
BorderWidth = Unit.Pixel(0);
BorderColor = Color.Black;
BorderStyle = BorderStyle.Solid;
Font.Size = FontUnit.Point(10);
Width = Unit.Pixel(70);
if (String.IsNullOrEmpty(Attributes["style"])) this.Attributes.Add("style", "border-bottom-width:1px;text-align : right ");
}
/// <summary>ÒÑÖØÔØ¡£</summary>
/// <param name="e"></param>
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (Min > Max) ShowError("Êý×ÖÊäÈë¿Ø¼þÖУ¬ÉèÖõÄ×îСֵ±È×î´óÖµ´ó£¬ÇëÖØÐÂÉèÖá£");
if (Min > -1 && Max > -1)
{
this.ToolTip = "Ö»ÄÜÊäÈë " + Min + " µ½ " + Max + " Ö®¼äÊý×Ö£¡";
}
else if (Min > -1 && Max < 0)
{
this.ToolTip = "Ö»ÄÜÊäÈë´óÓÚ»òµÈÓÚ " + Min + " Êý×Ö£¡";
}
else if (Min < 0 && Max > -1)
{
this.ToolTip = "Ö»ÄÜÊäÈëСÓÚ»òµÈÓÚ " + Max + " Êý×Ö£¡";
}
// УÑé½Å±¾
Helper.HTMLPropertyEscape(this, "onkeypress", "return ValidNumber({0});", AllowMinus ? 1 : 0);
Helper.HTMLPropertyEscape(this, "onblur", "return ValidNumber2({0},{1});", Min ?? -1, Max ?? -1);
Helper.HTMLPropertyEscape(this, "onkeyup", "FilterNumber(this,{0});", Helper.JsObjectString(
"allowFloat", 0,
"allowMinus", AllowMinus ? 1 : 0
));
this.Page.ClientScript.RegisterClientScriptResource(typeof(NumberBox), "XControl.TextBox.Validator.js");
//Èç¹ûûÓÐÖµ£¬ÔòĬÈÏÏÔʾ0
if (String.IsNullOrEmpty(Text)) Text = "0";
}
/// <summary>´¦Àí´íÎó¡£</summary>
/// <param name="err">´íÎóÐÅÏ¢</param>
private void ShowError(String err)
{
if (this.DesignMode)
{
System.Windows.Forms.MessageBox.Show(err, "XControl¿Ø¼þÉè¼Æʱ´íÎó");
}
else
{
throw new XException(err);
}
}
/// <summary>×îСֵ</summary>
[Category(" רÓÃÊôÐÔ"), DefaultValue(null), Description("×îСֵ")]
public Int32? Min
{
get
{
String str = (String)ViewState["Min"];
if (String.IsNullOrEmpty(str)) return null;
Int32 k = 0;
if (!int.TryParse(str, out k)) return null;
return k;
}
set
{
//if (value < -1) ShowError("·Ç·¨×îСֵMin¡£×îСֵ±ØÐë´óÓÚ0»òΪ-1(±íʾ²»ÏÞÖÆ)¡£");
if (value == null)
{
ViewState.Remove("Min");
return;
}
if (Max != null && value > Max)
{
ShowError("Êý×ÖÊäÈë¿Ø¼þÖУ¬ÉèÖõÄ×îСֵ±È×î´óÖµ´ó£¬ÇëÖØÐÂÉèÖá£");
return;
}
if (Max != null)
ToolTip = "Ö»ÄÜÊäÈë " + Min + " µ½ " + Max + " Ö®¼äÊý×Ö£¡";
else
ToolTip = "Ö»ÄÜÊäÈë´óÓÚ»òµÈÓÚ " + Min + " Êý×Ö£¡";
if (Value < value.Value) Value = value.Value;
ViewState["Min"] = value.ToString();
}
}
/// <summary>×î´óÖµ</summary>
[Category(" רÓÃÊôÐÔ"), DefaultValue(null), Description("×î´óÖµ")]
public Int32? Max
{
get
{
String str = (String)ViewState["Max"];
if (String.IsNullOrEmpty(str)) return null;
Int32 k = 0;
if (!int.TryParse(str, out k)) return null;
return k;
}
set
{
if (value == null)
{
//ShowError("·Ç·¨×î´óÖµMax¡£×î´óÖµ±ØÐë´óÓÚ0»òΪ-1(±íʾ²»ÏÞÖÆ)¡£");
ViewState.Remove("Max");
return;
}
if (Min != null && value < Min)
{
ShowError("Êý×ÖÊäÈë¿Ø¼þÖУ¬ÉèÖõÄ×îСֵ±È×î´óÖµ´ó£¬ÇëÖØÐÂÉèÖá£");
return;
}
Width = Unit.Empty;
Columns = value.ToString().Length;
if (Columns > 3) Columns -= 3;
if (Min != null)
ToolTip = "Ö»ÄÜÊäÈë " + Min + " µ½ " + Max + " Ö®¼äÊý×Ö£¡";
else
ToolTip = "Ö»ÄÜÊäÈëСÓÚ»òµÈÓÚ " + Max + " Êý×Ö£¡";
if (Value > value.Value) Value = value.Value;
ViewState["Max"] = value.ToString();
}
}
/// <summary>µ±Ç°Öµ</summary>
[Category(" רÓÃÊôÐÔ"), DefaultValue(0), Description("µ±Ç°Öµ")]
public Int32 Value
{
get
{
if (String.IsNullOrEmpty(Text)) return 0;
Int32 k = 0;
if (!Int32.TryParse(Text, out k)) return 0;
return k;
}
set
{
Text = value.ToString();
Check();
}
}
/// <summary>ÊÇ·ñÔÊÐí¸ºÊý</summary>
[Category(" רÓÃÊôÐÔ"), DefaultValue(true), Description("ÊÇ·ñÔÊÐí¸ºÊý,ĬÈÏtrue")]
public bool AllowMinus
{
get
{
object o = ViewState["AllowMinus"];
if (o == null) o = true;
bool r;
if (bool.TryParse(o.ToString(), out r)) return r;
return true;
}
set
{
ViewState["AllowMinus"] = value;
}
}
/// <summary>ÒÑÖØÔØ¡£Ð£ÑéÊäÈëÊý¾ÝÊÇ·ñÔÚÖ¸¶¨·¶Î§ÄÚ</summary>
protected override void RaisePostDataChangedEvent()
{
try
{
Check();
}
catch (Exception ex)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "err", String.Format("alert('{0}');", ex.Message), true);
this.Focus();
}
base.RaisePostDataChangedEvent();
}
private void Check()
{
if (Min != null && Value < Min) throw new ArgumentOutOfRangeException("Min", "Ö»ÄÜÊäÈë´óÓÚ»òµÈÓÚ " + Min + " µÄÊý×Ö£¡");
if (Max != null && Value > Max) throw new ArgumentOutOfRangeException("Min", "Ö»ÄÜÊäÈëСÓÚ»òµÈÓÚ " + Max + " µÄÊý×Ö£¡");
}
}
}
|