We help IT Professionals succeed at work.

How do I dynamically load min/max properties of the asp:RangeValidator?

jxbma
jxbma asked
on
Hi:

Relatively newby to the world of ASP .Net.

I'm having issues trying to set the min/max values of the RangeValidator.
I'm trying to set these values dynamically as my page/control loads.
I'd like to set these values directly within the page using embedded JavaScript to
references variables from my code behind.



My ASP .Net code looks like this:
=======================

<asp:RangeValidator ID="rngNumberOfTravelers" runat="server"  
ControlToValidate="tNumberofTravelers"
ErrorMessage="<%=RangeTravelersErrorMessage%>"
CssClass="error"
Display="Dynamic"
EnableClientScript="true"  
MinimumValue="<%=MinNumber%>"
MaximumValue="<%=MaxNumber%>"  
Type="Integer"/>


My property definition is C# looks like this:
==============================

    private int _minNumber;
    public int MinNumber
    {
        get {
                return _minNumbers;
            }
    }
    private int _maxNumber;
    public int MaxNumber
    {
        get {
                return _maxNumbers;
            }
    }


I get the following exception during the page load:
===================================

Server Error in '/Explorica.TravelerAdmin' Application.
--------------------------------------------------------------------------------

The value '<%=MaxNumberofTravelers%>' of the MaximumValue property of 'rngNumberOfTravelers' cannot be converted to type 'Integer'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The value '<%=MaxNumberofTravelers%>' of the MaximumValue property of 'rngNumberOfTravelers' cannot be converted to type 'Integer'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:

[HttpException (0x80004005): The value '<%=MaxNumberofTravelers%>' of the MaximumValue property of 'rngNumberOfTravelers' cannot be converted to type 'Integer'.]
   System.Web.UI.WebControls.RangeValidator.ValidateValues() +2564459
   System.Web.UI.WebControls.RangeValidator.ControlPropertiesValid() +18
   System.Web.UI.WebControls.BaseValidator.OnPreRender(EventArgs e) +146
   System.Web.UI.Control.PreRenderRecursiveInternal() +113
   System.Web.UI.Control.PreRenderRecursiveInternal() +222
   System.Web.UI.Control.PreRenderRecursiveInternal() +222
   System.Web.UI.Control.PreRenderRecursiveInternal() +222
   System.Web.UI.Control.PreRenderRecursiveInternal() +222
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4185



What am I doing wrong?
Can this not be set dynamically on the ASPX page?

I've also tried setting this in my code behind during Page_Load() <Binding the controls>.
But the result was that my RangeValidator did not fire correctly.


Any and all help will be much appreciated.

Thanks,
JohnB
Comment
Watch Question

Check
<asp:RangeValidator ID="rngNumberOfTravelers" runat="server"  
ControlToValidate="tNumberofTravelers"
ErrorMessage="Testing"
CssClass="error"
Display="Dynamic"
EnableClientScript="true"  
MinimumValue="<%#MinNumber%>"
MaximumValue="<%#MaxNumber%>"  
/>

--------------------------------------------
private int _minNumber = 20;
    public int MinNumber
    {
        get
        {
            return _minNumber;
        }
    }
    private int _maxNumber = 40;
    public int MaxNumber
    {
        get
        {
            return _maxNumber;
        }
    }

-----------------------------------------------

Hope this will help you
jxbmaSoftware Consultant

Author

Commented:
Thanks!
Carla RomereDirector of Information Technology

Commented:
I saw the first link yesterday before I decided to go ahead and post a request. The error that I'm getting is: CS0029: Cannot implicitly convert type 'double' to 'string on this line:
            rangePaper.MinimumValue = 0.01;