Trying to pass a parameter in ASP.NET ! Parsing error - asp.net

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") %>>

Related

Do boolean attributes require quotes in ASP.NET?

In ASP.NET markup do boolean attributes require quotes?
<asp:tag name="myTagName" IsEnabled=true />
or
<asp:tag name="myTagName" IsEnabled="true" />
It is most common to use quotes, although the ASP.NET page parser doesn't require nor enforce this.
All 3 buttons below will be disabled, using double quotes, single quotes and no quotes to set the value of the Enabled property.
<asp:Button runat="server" Text="Button1" Enabled="false" />
<asp:Button runat="server" Text="Button2" Enabled='false' />
<asp:Button runat="server" Text="Button3" Enabled=false />
Only when an invalid mixed format is being used like here below,
<asp:Button runat="server" Text="Button4" Enabled="false' />
being a combination of double and single quotes, Visual Studio shows a warning/error feedback, mentioning that the quotation marks must match in case they are being applied.
The message doesn't state that quotation marks must be used.
Validation (ASP.Net): If this attribute value is enclosed in quotation marks, the quotation marks must match.
At runtime the page will then fail with the error below.
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.
Parser Error Message: The server tag is not well formed.
Yes Because these tags are in XML format and passing data should be in quotes,
XML Syntax
Edit:
In IBM Website they have this example:
Incorrect
<?xml version= “1.0” encoding=“ISO-8859-1”?>
<note date=05/05/05>
<to>Dick</to>
<from>Jane</from>
</note>
Correct
<?xml version= “1.0” encoding=“ISO-8859-1”?>
<note date=”05/05/05”>
<to>Dick</to>
<from>Jane</from>
</note>
source

encode url having Xpath in Query String

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>

code works on dev pc but not server

The below code correctly processes data for my gridview on my dev machine but not my server.
I am simply trying to add hours to a time parameter that appears in the gridview. It works fine on my dev PC, but posts this error on server....
String was not recognized as a valid DateTime.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: String was not recognized as a valid DateTime.
Here is the code for the template field in my gridview...
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# String.Format("{0:hh:mm:ss tt}", Convert.ToDateTime(Eval("eventTime").ToString).AddHours(-5)) %>' ></asp:Label>
</ItemTemplate>
How can I check for Null with inline code?
Any suggestions would be greatly appreciated.
It sounds like a data issue between local dev and the server; I would check what is being pushed to eventTime. I bet it is not a valid DateTime.
EDIT
Per your edit, you can use the ternary operator (?:) to perform an inline null check:
<asp:Label ID="Label1" runat="server" Text='<%# Eval("eventTime") != null ? String.Format("{0:hh:mm:ss tt}", Convert.ToDateTime(Eval("eventTime").ToString).AddHours(-5)) : "No time specified" %>' ></asp:Label>

Print a Resources.resx file value within ASP.NET tags

I'm required to print a value of a string which within a resource files (.resx) within asp.net tags
i used the following approaches
for example :
<ext:ComboBox ID="cmbProgram" runat="server" FieldLabel="<%= Resource.StringName %>"
<ext:ComboBox ID="cmbProgram" runat="server" FieldLabel="<%= Response.Write(Resource.StringName) %>"
But both approaches didin't provide the value of the string, but provides the ID. How to get the values within the ASP.NET tags?
Assuming you have Control.ascx:
<ext:Tag runat="server" meta:resourcekey="fooBar" />
Then put Controls.ascx.resx in App_LocalResources containing appropriate strings:
fooBar.PropetyName = "x"

Validation controls error message from Resource file and parameterized

I would like to get validation messages for validation controls from resource files. I know I can do that easily by following code snippet.
<%$ Resources:[filename prefix,]resource-key %>
or
<asp:Label ID="Label1" runat="server" meta:resourcekey="resource-key-prefix" />
But I would also like to parameterized it.
e.g.
Above Resource Expression will give me message like "Fill Information.". What I have in resource file is "Fill {0} Information." which should show end user message like "Fill Address Information.".
So basically you want to have a localized Formatstring.
You can access the resource file from the codebehind, perform a String.Format and pass the value on to a control.
E.g.
myLabel.Text = string.Format(ProjectnameSpace.Resources.xxy, VALUE);
Try intellisense to get the full path of the property, I don't have visual studio here atm

Resources