During our work, we faced some strange problem that occured when having to use a RangeValidator within an Ajaxified Panel.
The scenario is as following
In the next example, All i was trying to do is to modify the MaximumValue property of the RangeValidator based on a values extracted from a TextBox. This means that the MaximumValue is a variant.
The ASPX Code is as following:
| <html xmlns="http://www.w3.org/1999/xhtml" > | |
| <head runat="server"> | |
| <title></title> | |
| </head> | |
| <body> | |
| <form id="form1" runat="server"> | |
| <div> | |
| <rad:RadScriptManager ID = "scr" runat = "server" /> | |
| <asp:Panel ID = "pnl" runat = "server" > | |
| <asp:TextBox ID = "txt1" runat = "server" AutoPostBack = "true" OnTextChanged = "txt1_TextChanged"/> | |
| <asp:TextBox ID = "txt2" runat = "server" /> | |
| <asp:RequiredFieldValidator ID = "rf" runat = "server" ControlToValidate = "txt2" ValidationGroup = "a" /> | |
| <asp:RangeValidator ID = "rv" runat = "server" Text = "*" ErrorMessage = "Abbas" ForeColor = "Red" ControlToValidate = "txt2" ValidationGroup = "a" Type= "Integer" MinimumValue = "0" MaximumValue = "10" /> | |
| <br /> | |
| <asp:Button ID = "btn" runat = "server" ValidationGroup = "a" OnClick = "btn_Click" Text= "Press Me" CausesValidation = "true"/> | |
| <asp:ValidationSummary ID = "sum" runat = "server" ValidationGroup = "a" ShowSummary = "true" DisplayMode = "BulletList" ShowMessageBox = "true" /> | |
| </asp:Panel> | |
| <rad:RadAjaxManager ID = "rad" runat = "server" > | |
| <AjaxSettings> | |
| <rad:AjaxSetting AjaxControlID = "pnl"> | |
| <UpdatedControls> | |
| <rad:AjaxUpdatedControl ControlID = "pnl" /> | |
| </UpdatedControls> | |
| </rad:AjaxSetting> | |
| </AjaxSettings> | |
| </rad:RadAjaxManager> | |
| </div> | |
| </form> | |
| </body> | |
| </html> | |
The Code Behind is as Following
| using System; | |
| using System.Collections.Generic; | |
| using System.Web; | |
| using System.Web.UI; | |
| using System.Web.UI.WebControls; | |
| namespace WebApplication1 | |
| { | |
| public partial class _Default : System.Web.UI.Page | |
| { | |
| protected void Page_Load(object sender, EventArgs e) | |
| { | |
| } | |
| protected void txt1_TextChanged(object sender, EventArgs e) | |
| { | |
| int x = 0; | |
| int.TryParse(txt1.Text, out x); | |
| rv.MaximumValue = x.ToString (); | |
| rad.Alert ("Maximum Value Set"); | |
| } | |
| protected void btn_Click(object sender, EventArgs e) | |
| { | |
| rad.Alert ("Button Post Back"); | |
| } | |
| } | |
| } | |
You will notice that the RangeValidator by default has the MaximumValue "10", Lets take the following Scenario :-
- Open the Page in your IE
- Type in a value of 200 in the First Text Box
- You will get an Alert that the 200 has been set as the MaximumValue from the TextChanged Event of the TextBox
- Now try to put 220 in the Second TextBox , then click the Press Me button.
- Well, You will notice that the Validation Summary Displayed that the Value is out of range.
- Now it should be obvious that trying enter a value that is below the 200 would be IN the range.
- Now Put 180, 160, 100, Try all the values and click the Press Me button, you will always get the Validation as active!.
- Finally Try to put 9 (Which is actually below the MaximumValue that was hardcoded declaratevely in the ASPX) and press the Press Me button. !! Wow the Page will post back finally and you will get the Alert from the Button "Button Post Back"
Now after some investigation, there is some problem with this validator, At first i thought it was a problem with all the Expando Attributues in the Valiadators, But it turned out to be this RangeValidator that has this specific problem.
Kindly inform us what should we do ?