ControlToValidate property in CustomValidator is blank? - asp.net

I have the following code defined in my ASP.NET file:
<asp:CustomValidator ID="CustomValidator6"
ControlToValidate="Weight1" OnServerValidate="TotalWeight" runat="server" ErrorMessage="Must equal 100%"></asp:CustomValidator>
Yet when I try stepping through my code to debug, I get a Javascript error saying "ControlToValidate propery of '' cannot be blank." This is driving me nuts - I know it's this particular control causing the problem, because if I comment it out, the page runs fine.
Has anyone seen this before and know of a fix? I've tried dropping a new custom validator on the page, setting the properties manually and through the properties window, and a little of both but I still get this error.

Related

Microsoft ReportViewer control (Web) & displaying error messages

I have a Microsoft ReportViewer control on my web page. However, if someone types in an invalid input for one of the parameters then it displays a rather unfriendly error message where the report should go. For example: The value provided for the report parameter 'pToDate' is not valid for its type. (rsReportParameterTypeMismatch)
The control prompts the user for the information with "To Date" and "pToDate" is the internal name of the parameter. The users won't know this, nor will they likely react well to "rsReportParameterTypeMismatch" (what ever that means!? [while thinking like a user])
As I couldn't find somewhere in the ReportViewer control to put any error or custom message, my solution was to create a label in which to put a more friendly error message. This works insofar as the friendly error message is displayed.
My problem is that once the user has corrected their mistake and clicks "View Report" the report is displayed but the error message is still visible. I've set the label text to string.Empty, I've set the label to Visible = false. I've tried this in various places, ensured the code is hit, but to no avail.
So, is there any way to get custom messages to appear and disappear with a ReportViewer control?
Okay - I've got something that works
Previous I had this:
<asp:Label runat="server" ID="ReportErrorMessage" Visible="false" CssClass="report-error-message">
</asp:Label>
which I was updating in the code behind like this:
ReportErrorMessage.Text = GetErrorMessage(reportException);
ReportErrorMessage.Visible = true;
and then removing like this:
ReportErrorMessage.Visible = false;
ReportErrorMessage.Text = string.Empty;
The latter part didn't work.
I eventually realised that the ReportViewer control is using partial rendering and so wasn't actually changing the label at all (and consdering that, I've still not quite figured out how the initial display actually worked, but anyway...)
The solution was to wrap the label in an update panel like this:
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Label runat="server" ID="ReportErrorMessage" Visible="false" CssClass="report-error-message">
</asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
UPDATE
I've also added a full explanation onto my blog: Friendly Error Messages with Microsoft Report Viewer

RegularExpressionValidation error message not clear after value is fixed

I am using a regular expression validator to validate several fields on an asp.net form. Here is an example:
<asp:RegularExpressionValidator runat="server"
ID=RegularExpressionValidatortxtEMAIL ControlToValidate="txtEmployerEmail"
ErrorMessage="EMail Address - must be valid"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic" ></asp:RegularExpressionValidator>
The problem is that after the user enter a invalid value and then corrects it with a valid value, the error message persists until a postback is generated.
Is there any fix for this?
There's nothing wrong with that validator tag - I've just used it in a page and it works fine. You understand that the validator only gets fired when the control loses focus... It could be something else in your page that's causing the problem, perhaps a JavaScript error?

Use HtmlEncode in Details View TemplateItem Control

I have details view control in my asp.net web form, which on of its item template gets it is value from database, and show this into a richtextbox :
<FTB:FreeTextBox id="txtDescription" runat="Server" AllowHtmlMode="false" Text='<%# (Eval("Description") )%>'
>
</FTB:FreeTextBox>
but when i click on insert or update button, i get the following error :
A potentially dangerous Request.Form value was detected from the client ....
i tried this :
Text='<%# HttpUtility.HtmlDecode((string)Eval("Description"))%>'
bu it did not work ethier, and i got the error again.
is there any way except turning validateRequest off.
Would you please help me?
No, there isn't a way to get this to work aside from turning Validate Request off. Which isn't a bad thing if you write your database functionality correctly and implement strict custom form validation.

asp.net validation control causing "random" error in javascript

I have an asp:FormView on a control (in an ascx file) which is loaded onto a portal page along with some other controls.
I need to add validation to the form, but when I add any of the validation controls I get an apparently random error in the generated JavaScript when the page is loaded, and the validation doesn't fire when I need it to.
This is the code that I've got:
<asp:TextBox ID="FPITextBox4" runat="server" Text='<%# Bind("SomeNumber") %>'></asp:TextBox>
<asp:RangeValidator ID="RangeValidator4" runat="server" ControlToValidate="FPITextBox4" Text="*"
ErrorMessage="The number must be a whole number between 0 and 100,000" Type="Integer"
MaximumValue="100000" MinimumValue="0" ToolTip="Must be between 0 and 100,000"></asp:RangeValidator>
This is the error I get when the page is loaded:
Message: Expected ';'
Line: 1159
Char: 60
Code: 0
When I look at line 1159 in the generated code it looks like this:
var ctl00_ctl00_main_col2_control_0_widgetcontrolX_3eba4918-7c7d-47aa-a089-
fd0ead0609fe_widgetcontrol_3eba4918-7c7d-47aa-a089-
fd0ead0609fe_FormView1_RangeValidator4 = document.all ?
document.all["ctl00_ctl00_main_col2_control_0_widgetcontrolX_3eba4918-7c7d-47aa-a089-
fd0ead0609fe_widgetcontrol_3eba4918-7c7d-47aa-a089-
fd0ead0609fe_FormView1_RangeValidator4"] :
document.getElementById("ctl00_ctl00_main_col2_control_0_widgetcontrolX_3eba4918-7c7d-
47aa-a089-fd0ead0609fe_widgetcontrol_3eba4918-7c7d-47aa-a089-
fd0ead0609fe_FormView1_RangeValidator4");
The code immediately above this is:
<script type="text/javascript">
//<![CDATA[
var Page_Validators = new Array(document.getElementById(
"ctl00_ctl00_main_col2_control_0_widgetcontrolX_3eba4918-7c7d
-47aa-a089-fd0ead0609fe_widgetcontrol_3eba4918-7c7d-47aa-a089-
fd0ead0609fe_FormView1_RangeValidator4"));
//]]>
</script>
The same code works when the control is just instantiated onto a test page all by itself.
It appears that the error only happens in IE 8. I've tested the same page in Firefox 3.6 and it all works as expected.
Correction
It appears that the validation is working. I had a ValidationSummary control set to show a message box and that wasn't appearing which led me to believe that the validation wasn't firing. When I changed that to ShowSummary="true" ShowMessageBox="false" the error message did appear and the data isn't saved.
So all it means now is that I've got a random script error that (at the moment) doesn't appear to be affecting the workings of the page.
However, I would still like to remove the error - just in case it's hiding something else.
you really found a bug in .Net client generation code - have a look at
http://haacked.com/archive/2006/07/14/ASP.NET2.0ClientValidationJavascriptBug.aspx
But no resolution from M$ - you'll have to generate IDs in a different way...
I've found that this type of error results because of an error in one of my scripts, where there is a missing ;. Do you have JS code in your page and is all of that correct, no missing semi-colons?
HTH.

CompareValidator works in listview's editItemTemplate but not in insertitemtemplate

I have a validation problem
I have a listview, in the edit item template I have two composite controls with a textbox inside
I put a comparevalidator on it
<asp:CompareValidator ID="myCompareValidator" runat="server"
ControlToValidate="mycompositecontrol1" ControlToCompare="mycompositecontrol2"
Operator="GreaterThanEqual" Type="Date" Display="Dynamic" ErrorMessage="there is an error !"
Text="!" ValidationGroup="myValidationGroup" />
It works great !
so I do exactly the same operation in the InserItemTemplate (It's a copy/paste)
but this time, it doesn't work, I have no error message in my validationsummary and near my control to validate!
If you know that problem, help me please
thanks in advance
This isn't really a good answer to the question directly, but:
I have never had any good luck with the baked in ASP validators. I always try to hammer their square peg into a round hole for a while, get close, get frustrated, then roll my own with my own logic, error messages in labels, and switching visibility with a CSS style.
P.S. when you copy pasted your validator, make sure you changed what control it is validating...

Resources