IE9: Validation message not getting cleared - asp.net

I am using an upload control which accepts only image files.If I select any non-image files I will be showing a validation error message "File type is invalid". The upload control i use is as follows
<UC:UploadControl runat="server" ID="file_logo" IsRequired="false" ReqValidationGroup="jpg" onkeypress="Javascript:CheckNumeric(event);" RequiredFieldMessage="Please upload Image file" CheckFileType="true" Width="870" Height="100" CheckDimension="false" RegexpFieldMessage="File type is invalid." FileFormats="gif,GIF,jpg,JPG,jpeg,JPEG" RegDisplay="true" />
I use another link to clear the error validation message. The code is
<a href='javascript:chkFileTypes("ctl00_mainContentPlaceHolder_file_logo_uploadfiles");clearInvalidLabel("ctl00_mainContentPlaceHolder_file_logo_uploadfiles")' >Clear File</a><br />
My Javascript function used to clear validation message is
function clearInvalidLabel(control) {
if (control == "ctl00_mainContentPlaceHolder_file_logo_uploadfiles") $("span[id$='file_logo_regexpType']").hide();
if (control == "ctl00_mainContentPlaceHolder_PopUp_logo_uploadfiles") $("span[id$='PopUp_logo_regexpType']").hide();}
Now if I again select an improper file using UploadControl, the error validation message is not getting displayed(Only in IE9). It works perfectly in other browsers. Please help me out.
Thanks.

Hard-coding IDs is easy to break. Try using the ClientID to access the control in JavaScript.
<a href='javascript:chkFileTypes("<%=UploadFilesControl.ClientID%>");clearInvalidLabel("<%=UploadFilesControl.ClientID%>")'>Clear File</a>
I would use the ClientID to get the validation labels too:
$("#<%=regexpType.ClientID%>").hide();

Related

Typical FileUpload RegularExpressionEvaluator Triggers Error, how to fix?

On my page I have an standard asp:FileUpload control. Right below it is a RegularExpressionValidator that is supposed to only allow pdf and Word documents to be uploaded. It's the same regex seen all around the web. For some reason, it worked fine in aother project, but not in my current one. When loading the page in Firefox, I immediately get the validator to show me the error message right after selecting the file. This tells me it's a client side issue as the page has yet to be submitted to the server.
The tag:
<asp:RegularExpressionValidator id="Resume_Validator" runat="server"
ControlToValidate="Resume"
ValidationGroup="applicationForm"
ErrorMessage="Upload PDF or Word files only<br />"
Display="Dynamic"
CssClass="validationMsg"
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.pdf|.PDF|.doc|.DOC|.docx|.DOCX)$"
/>
Why not just use jQuery file validation on the client-side?
//Check file extension
var ext = file.split('.').pop().toLowerCase(); //Check file extension if valid or expected
if ($.inArray(ext, ['txt']) == -1) {
$(".errorDiv").html("Select valid text file (txt).");
e.preventDefault(); //Prevent submission of form
}
else {
//Do your logic here, file upload, stream. etc.. if file was successfully validated
}

changing the asp:Image ImageUrl attribute Depending on Condition in Asp.net

i'm building a simple reporting applications , in which user has notifications i want to check if the notification is unRead i'll display a read image icon
and if it was read , i'll display Read Icon
ive created a TicketIsUnRead() function to check if the ticket was read|unread
and i've used this code in aspx page that attaches different resource to the asp:image control different resources are responsible for changing the ImageUrl attribute
<asp:Image runat="server" id="ticketIcon" meta:resourcekey='<% (TicketIsUnRead(ticketNumberField.ToString()))? "UnReadticketIconResource" :"ReadticketIconResource"%>' />
and inside the Resx page
UnReadticketIconResource.ImageUrl images/UnReadMail.png
ReadticketIconResource.ImageUrl images/ReadMail.png
it seems that the Meta:resourcekey attribute doesn't allow the <% %> code
it returns parse error exception
Parser Error Message: '<% (TicketIsUnRead(ticketNumberField.ToString()))? "UnReadticketIconResource" :"ReadticketIconResource"%>' is not a valid resource key.

ASP CKEditorControl's content length on client-side

I'm using CKEditor in my ASP.NET webforms application. I'm using CKEditor control to put it to my page:
<CKEditor:CKEditorControl ID="CKEditorControl" runat="server" />
But I'd like to get the editor's content length on client-side. I'm trying to do it this way:
CKEDITOR.instances['<%= CKEditorControl.ClientID %>'].getData().length;
Though it's not working because: "Uncaught TypeError: Cannot call method 'getData' of undefined"
So my question is. What am I doing wrong? And is it possible to get the CKEditor's length on client-side when using CKEditorControl?
this code works:
$('#cke_<%= CKEditorControl.ClientID %> iframe').contents().find('body').html()
I'm not sure now but I guess all my problems were because I was trying to get the data before the editor was loaded.

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 sanitizing user input

Does asp.net have a built in mechanism that can sanitize all textbox input instead of redirecting to the error page?
I have a textbox input where a user can enter a name, but if they try to enter and <> tags the page automatically throws an error. I just want to handle this error in a user friendly way.
You'll want to look at the AntiXSS library for that. It's a dll so it's easy to drop in and start using it.
The download is at CodePlex.
You can use the ASP.NET RegularExpressionValidator control with a pattern like: ^[^<>]*$
<asp:RegularExpressionValidator ID="rev" runat="server"
ControlToValidate="txtBox"
ErrorMessage="The <> tags are not allowed!"
ValidationExpression="[^<>]*" />
<asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtBox"
ErrorMessage="Value can't be empty" />
The RequiredFieldValidator is used in conjunction with the RegularExpressionValidator to prevent blank entries. If that textbox is optional, and only needs to be validated when something is entered, then you don't have to use the RequiredFieldValidator.
The benefit of doing it this way is that the error can be handled gracefully and the user can be notified on the same page.
However, if you need to do this for many textboxes and you just want to present something nicer than the error page, you could handle the ValidateRequest error to provide a friendlier message and keep the user on the same page (not just replace it with a custom error page). For more info, check out Kirk Evans' post: Handling ValidateRequest errors within a Page (refer to the section titled Overriding the OnError Method).
Read this for a step-by-step: http://yourtahir.wordpress.com/2008/03/28/aspnet-not-allow-html-in-text-boxserver-error-in-application-a-potentialy-dangerous-requestform-value-was-detected/
You have to do some web.config work.
ASP.net has validation controls
[http://msdn.microsoft.com/en-us/library/7kh55542.aspx][1]
Also there is Mark Down Editor which is a control that strips out html tags etc.

Resources