changing the asp:Image ImageUrl attribute Depending on Condition in Asp.net - 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.

Related

How can I access an object inside of an asp:LoginView

I have a LoginView on a site master page that displays the username of the logged-in user. I modifies it to make the username a link to that user's settings, using a LinkButton.
I need to be able to conditionally enable or disable the LinkButton from within Page_Load. How do I get a reference to the LinkButton?
The LinkButton doesn't appear in the designer.cs file, but the LoginView does. I have tried looking at its controls property in the debugger and also tried using FindControl(LinkButton's ID) but that returns null.
--Jacob
You can't use the LogIn "as is", you have to create a LayoutTemplate inside the control.
<asp:Login ID="LoginUser" runat="server">
<%--the LayoutTemplate must include controls (with Text property)
with ID values UserName and Password--%>
<LayoutTemplate>
Your stuff here. Textboxes for user name and password, etc...
</LayoutTemplate>
</asp:Login>
Then you can find a control by
Label myErrorLabel = (Label)LoginUser.FindControl("logInErrorDetails");
Make sure that LinkButton is Asp.Net server control with "runat="server" attribute. If it is a server control then it should appear in your designer.cs file. Once it appeared in designer.cs file, you can access that control by its name or using FindControl method.
Some times Visual Studio IDE creates problem and it does not update desinger.cs file. Try to switch between designer view, mark up view and code view. It will update designer.cs file if the markup is correct.

ASP ChangePassword control changepasswordfailuretext does not show

I'm using the ASP ChangePassword control. When a user fails to enter their current password correctly I just get a control refresh with all fields cleared, not the changepasswordfailuretext. Googled this and found that you should use ChangePasswordError method to catch this. I put a breakpoint in this method and did not get stop here when the current password was incorrect.
How do I show my users that they have entered their current password incorrectly?
Just ran into this myself and found an answer. If you are customizing the display using ChangePasswordTemplate and SuccessTemplate, you also need to add a control with the id "FailureText". This control gets populated with your configured message in the event of an error:
<asp:Literal ID="FailureText" runat="server" />
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.changepassword.changepasswordtemplate.aspx

Ajax Toolkit AjaxFileUpload throw exception

I have an entry form which I want to add a file uploader I added a toolkit script manager to the master page
then i added an ajaxfileupload control <http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AjaxFileUpload/AjaxFileUpload.aspx>
the aspx looks like this
<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" ThrobberID="myThrobber" ContextKeys="fred"
AllowedFileTypes="jpg,jpeg" MaximumNumberOfFiles="2" runat="server" UploadedComplete="AjaxFileUpload1_UploadedComplete" />
the method AjaxFileUpload1_UploadedComplete is empty for now.
I got this exception and no event is firing except for the page_load
Sys.ArgumentException: Sys.ArgumentException: Cannot deserialize. The
data does not correspond to valid JSON.
I realise this is an old question but i think this is caused by a 'bug' in the control. It appends query string parameters but assumes there are no existing parameters.
There are details and a fix here: https://ajaxcontroltoolkit.codeplex.com/workitem/27149 (involves changing the source and rebuilding the toolkit)

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