I am using a RangeValidator to validate that a year is between a static start year and a dynamic end year (the current year). I am drawing a huge blank for setting the maximum value in this fashion:
MaximumValue='<% DateTime.Now.Year %>'
Any help is appreciated as I usually don’t set max values in this fashion.
Edit:
So I have been given the following ways to incorporate the code into the codebehind:
- validator init event
- page prerender
- and i’m a newb and would just have done on page load
which is best?
By default, ASP.NET doesn’t let you do this; the
<%= ... %>syntax doesn’t work either.The easiest way is to just set the
MaximumValueproperty in the code-behind, in the validator’sInitevent. (This is better than the page’sInit,Load, orPreRenderevent, which would bloat view state.)By using the
senderparameter, multipleRangeValidatorcontrols on the page can all share this event handler.If you really wanted to set the
MaximumValuein the .ascx/.aspx, then take a look at this blog post: The CodeExpressionBuilder.UPDATE: Setting
MaximumValueinInit,Load, andPreRenderwould all work.Inithas the slight advantage that it avoids increasing the size of view state.PreRenderhas the additional disadvantage that server-side validation would break if view state were disabled for the validator.