Shouldn't min and max then be tied into Upper_Range and Lower_Range, both in OnServerValidate and the client side script in OnLoad? Otherwise, I don't see how they do anything, right
jd
----- \"Jeffrey Tan[MSFT]\" wrote: ----
Hi jdn
Yes, you are right. You should expose the range rate as 2 properties, then
the your control customer can set them properly and use it freely
Also, if you want to make your control more like realistic control, I think
you can add more logic into your control
1). In OnLoad method, judge the EnableClientScript property to determine if
render the client javascript code
2). Encapsulate the server side validation logic in your control, then your
control user need not add any programming logic to use
To get this done, I think you should override OnServerValidate method and
do the server side validation
3). Just as you point out, Add lower and upper range rate as properties for
user in your control
Sample code like this
public class WebCustomControl1 : System.Web.UI.WebControls.CustomValidato
protected override void OnLoad(EventArgs e
base.OnLoad (e)
if(this.EnableClientScript==true
StringBuilder sb=new StringBuilder()
sb.Append("<script language=\"javascript\">")
sb.Append("function ")
sb.Append(this.ClientValidationFunction.ToString())
sb.Append("(source, args)")
sb.Append("{")
sb.Append("var val=document.forms[0].item(\"")
sb.Append(this.Controltodependid)
sb.Append("\").value;")
sb.Append("var min=val*0.2;")
sb.Append("var max=val*0.5;")
sb.Append("if(args.Value>=min&&args.Value<=max)")
sb.Append("{")
sb.Append("args.IsValid=true;")
sb.Append("}else{")
sb.Append("args.IsValid=false;")
sb.Append("}}")
sb.Append("</script>")
this.Page.RegisterClientScriptBlock("script",sb.ToString())
string controltodependid
float lower_range
float upper_range
public float Lower_Rang
ge
return lower_range
se
lower_range=value
public float Upper_Rang
ge
return upper_range
se
upper_range=value
public string Controltodependi
ge
return controltodependid
se
controltodependid=value
protected override bool OnServerValidate(string value
bool result=base.OnServerValidate (value)
double max, min
TextBox tb=(TextBox)this.Page.FindControl(this.Controltodependid)
double val=Convert.ToSingle(tb.Text)
min=val*0.2
max=val*0.5
double newval=Convert.ToSingle (value)
this.IsValid=(newval>=min&&newval<=max)
return result&&this.IsValid
protected override void Render(HtmlTextWriter output
base.Render(output)
output.Write(Text)
=============================================================
If it does not work, please feel free to tell me
Have a nice day!
Best regards
Jeffrey Ta
Microsoft Online Partner Suppor
Get Secure! -
www.microsoft.com/securit This posting is provided "as is" with no warranties and confers no rights