I want to send title of news to some other page through query string. My title is dynamic so it may contain '&' or other special characters. So I tried this
<a href="NewsByTitle.aspx?title=<%#HttpServerUtility.HtmlEncode(XPath("title"))%>"
But I am getting error "The best overloaded method match for 'System.Web.HttpServerUtility.HtmlEncode(string)' has some invalid arguments"
Please help me.
Replace with this code:
<a href='NewsByTitle.aspx?title=<%#Server.UrlEncode( XPath("#title").ToString()) %>' ><%# XPath("#title") %></a>
Related
Is there a clean way to include a string substitution inside a razor page?
That is, I have some razor code that looks like this but does not work. I feel like I'm missing some punctuation.
<div class='card-img'>
<img src='images/User-#Model.Id.jpg'/>
</div>
With your current code, razor thinks the the expression Model.Id.jpg is some server side code it needs to execute.So you are going to get an error message saying there is no such method/property called jpg on whatever the type of Id is or there are no extension method called jpg in that type.
To fix the problem, you shall wrap your C# expression with an explicit ( and )
<div class='card-img'>
<img src='images/User-#(Model.Id).jpg'/>
</div>
This tells razor that Model.Id is the C# expression it needs to execute. The output of exuecuting that expression will pe rendered in the HTML.For example, If the value of Model.Id is 100, the rendered output will be
<img src="images/User-100.jpg">
Now make adjustments to your path (src value) based on where you are storing the image.
Try to use either Razor code block #(...) or #Url.Content() helper for string concatenation:
#* Code block string concatenation *#
<img src='images/User-#(Model.Id).jpg'/>
#* Content helper string concatenation *#
<img src='#Url.Content("images/User-" + Model.Id + ".jpg")'/>
Take note that #Model.Id.jpg doesn't work because Id property doesn't have member named jpg, since the second dot (.) treated as member access expression.
I am trying to pass a parameter from a page to another. I am getting parsing errors (Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. )
This is the code :
<a runat="server" href="~/ProductDetails.aspx?IDProduct=<%# Eval("IDProduct") %>">
<asp:Label Font-Size="16px" ForeColor="Red" runat="server">get specifications</asp:Label>
</a>
I am getting errors at IDProduct=<%# Eval("IDProduct") %>!
How should i write it ?
The proper way to handle such cases is to make the whole attribute value generated inside <%# %>. Also note the updated quoting pattern - single quotes around attribute value, and double quotes inside <%# %>.
href='<%# "~/ProductDetails.aspx?IDProduct=" + Eval("IDProduct") %>'
Try to do this
<a runat="server" href="~/ProductDetails.aspx?IDProduct="<%# Eval("IDProduct") %>>
Previously i passed the parameters to my page in a query string. Now, how am i supposed to get them and use them in my page ?
I tried this code and it doesn't work :
<asp:Label runat="server" ID="ShowParameter"><%# Request.QueryString["IDProduct"] %></asp:Label>
You need to use <%= Request.QueryString["IDProduct"] %>. # is used for databinding.
It also goes without saying that you should also check to make sure the query string is not null and that it actually exists. It is also generally not a good idea to directly output a query string like this for a number of reasons.
For example:
data-val-equalto="'MyProperty5' and 'MyProperty4' do not match."
Question: Why is the & character encoded again into & ('), instead of just outputting the character reference as is (') ?
The jquery.validate plugin seems to be parsing ' as '.
The problem doesn't seem to be in Razor, but with the code that generates the unobtrusive validation attributes, the following code:
<span title="#("'MyProperty5' and 'MyProperty4' do not match.")"></span>
... outputs correctly:
<span title="'MyProperty5' and 'MyProperty4' do not match."></span>
Found out that the problem is in ASP.NET MVC, there's a method called GetValidationAttributes that adds HTML-encoded values to a dictionary, and then the values are encoded again by TagBuilder. It would be good to know why they are doing this.
Try outputting using the Html.Raw method.
Otherwise, Razor does not assume you are trying to output encoded HTML and encodes it again.
Given
string text = `Bread & Breakfast`;
#text will be output as Bread & Breakfast because the & is HTML Encoded
#Html.Raw(text) will be output as "Bread & Breakfast"
UPDATE based on your update
I can't tell you why jQuery Validate works that way, but there's an old adage "if it hurts when you do X, stop doing X".
You don't really need to encode the single quote in your output HTML. Both of the following produce the same result:
<span title="'MyProperty5' and 'MyProperty4' do not match.">
Span With Encoded Title
</span>
<br />
<span title="'MyProperty5' and 'MyProperty4' do not match.">
Span With non-Encoded Title
</span>
This issue was fixed on MVC v4.
I add asp.net file upload control as follows:
<asp:FileUpload ID="filesFileUpload" runat="server" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="file types not supported"
ValidationExpression="\.(zip|rar|jpg|gif|png|eps|ai|psd|pdf)$" ControlToValidate="filesFileUpload"></asp:RegularExpressionValidator>
And always when I upload file that match the reg expression it show the error. How can I resolve this?
Your regular expression checks for a single dot, followed by one of the extensions, all the way to the end of the string. You need to match the rest of the the filename (.+ matches one or more characters , ^ mean start of string):
ValidationExpression="^.+\.(zip|rar|jpg|gif|png|eps|ai|psd|pdf)$"
See this handy cheat sheet.