In my .ascx usercontrol i'm trying to dynamically generate links using a value i've stored in web.config.
link
and when i try running, i get a parser error
Literal expressions like '<%$appSettings.MYPATH %>' are not allowed. Use <asp:Literal runat="server" Text="<%$appSettings.MYPATH%>" /> instead.
I know i'm probably missing something relatively minor.
<%= ConfigurationManager.AppSettings["myKey"] %>
EDIT:Don't forget the =
link
should work (it at least does on the IIS server I use). (Unfortunately it's more verbose)
Try this instead
.ascx
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
in code behind
Literal1.Text = "link"
More accurate answer will be next:
Link
Use a colon instead of a dot and add runat="server":
link
The documentation isn't very clear on this point, but ASP.Net Expressions are for use within server tags. Thus, if you want to use one in a plain html tag, you must add runat="server" so that the tag is processed on the server where the expression will be evaluated.
Related
I have the following two buttons:
<asp:Button ID="btnVote" runat="server" Text="Vote!" PostBackUrl="<%$RouteUrl:id=2, routename=Results%>"/><br />
<asp:Button ID="btnResults" runat="server" Text="Results ->" OnClientClick="location.href='<%$RouteUrl:id=2, routename=Results%>'"/>
The first <%$ %> expands as intended, while the second (identical!) one gets used as typed ( = not expanded). I am very new to ASP.NET, coming from PHP, and this is from my learning/test site.
What am I doing wrong and how can I fix it?
The ASP.NET expression syntax (<%$ ... %>) can only be used to directly assign values to properties of server controls. I think the problem you're having is that the expression syntax is embedded within a string, and not directly bound to the "OnClientClick" attribute.
Trying changing your second button to
<asp:Button ID="btnResults" runat="server" Text="Results ->" OnClientClick="<%$RouteUrl:id=2, routename=Results%>" />
If that works, you may need to modify your expression to return that extra text you need. Or, create another route that returns said info (the current value wrapped in the "location.href" attribute).
You can find more information about these expressions here: ASP.NET Expressions Overview.
So, currently, within an asp gridview, I have the following
<span id="btnEdit" runat="server" onclick="ShowEditCriteriaFilterDialog('<%#Eval("intSMCID")%>', '<%#Eval("strDescription")%>')" class="linkText">Edit</span>
What I'm essentially looking for is the syntax for quotes/double-quotes to actually accomplish this properly, as what I have above doesn't properly work.
First of all, if i encapsulate the entire onclick with single quotes, and not put any other quotes inside, it works for rendering purposes, but when i actually click the link at runtime, nothing happens.
If I encapsulate the entire onclick with double-quotes, like most properties of an ASPX element, it doesn't render properly, and everything after the comma which is after the first <%#Eval %> statement shows up as actual text on the screen. This leads me to believe there needs to be some escaping done to prevent it from thinking the click handler ends somewhere in the middle of that <%#Eval %> statement.
note that if I take away runat="server" and just encapsulate it in double-quotes, that seems to work better...but i need the span to be a server-side control for alot of other functionality I have in the page's code behind where I need to access the control through FindControl
The Eval function needs double-quotes, so you have to wrap the attribute value in single quotes.
You can't mix static content and <%# ... %> blocks in a single property value for a control with runat="server", so you need to build the entire string within a single <%# ... %> block.
Within a string in a <%# ... %> block, you can use ' to insert a single quote:
EDIT That only works in .NET 4.0; in 2.0, it generates ', which won't work. Use double-quotes instead:
onclick='<%# string.Format(
"ShowEditCriteriaFilterDialog(\"{0}\", \"{1}\")",
Eval("intSMCID"), Eval("strDescription")) %>'
Depending on your data, you might also need to encode the string value for JavaScript:
onclick='<%# string.Format(
"ShowEditCriteriaFilterDialog(\"{0}\", \"{1}\")",
Eval("intSMCID"),
HttpUtility.JavaScriptStringEncode(Eval("strDescription", "{0}"))) %>'
(Code wrapped for readability - it needs to be on a single line.)
I think the solution is
onclick='<%# "ShowEditCriteriaFilterDialog(" +Eval("intSMCID") + ","+ Eval("strDescription") +" );" %>'
Is there any advantage/downfalls between using the inline write tag instead of the resource tag? Example:
<%=Resources.Site.SampleString %>
The resources tag (expression tag) as seen in any MSDN example:
<asp:Literal id="Literal1" runat="server" text="<%$ Resources:Site, SampleString %>" />
I find the first option far easier to use, and it has IntelliSense, but maybe it won't function the same?
These methods will function exactly the same. The latter simply calls first one; there is a reason why strongly-typed resources access code is being generated in the background. Therefore you can use whatever method you please.
By the way, there is also another way - by using meta:resourcekey attribute. So you should be able to write:
<asp:Literal id="Literal1" runat="server"
meta:resourcekey="SampleString" text="Default one" />
and it all should work exactly the same.
EDIT on implicit Localization.
What I forgot to mention, is that with meta:resourcekey certain conditions have to be met. The conditions are:
Values are taken from App_LocalResources, therefore related resource file need to exist
Related resource file name must be pagename.resx, for example: Default.aspx.resx, Default.aspx.es.resx
The resource file must contain keys in form of resourcekey.propertyname, for example SampleString.Text, SampleString.ID (although I wouldn't localize control ID's)
Because of how resources are generated, the key mentioned above must exist in invariant resource file (Default.aspx.resx), or it won't be localized.
I realised after some time that the <%=Resources.Site.SampleString %> method does not have designer support (which is understandable). This doesn't bother me, but it is a difference (for future readers).
So if you need or want designer support, the second option would be necessary.
I'm outputting a few lines of Javascript within a Repeater control on an ASPX page. I want to use a value from my DataSource inside the script tag.
A very basic example might be:
<asp:Repeater ID="RepeaterBlah" runat="server">
<ItemTemplate>
Hello <%# DataBinder.Eval(Container.DataItem, "SomeName")%>
<script>myfunction(<%# DataBinder.Eval(Container.DataItem, "SomeNumber")%>)</script>
</ItemTemplate>
</asp:Repeater>
I'm aware that most people won't repeat script tags like this, but I am using a small snippet of code from a third-party that you can place anywhere on a page to create a Flash object. You pass it a number so it knows which image gallery to display. No problems using several on one page.
To begin with, this worked fine, although I noticed the colours in Visual Web Developer indicated that it didn't really like the <%# being used inside a <script> tag. Intellisense was going a bit nuts in the code-behind too!
So what is the correct way to pass Dataset items into a script tag?
This perhaps? (Can't quite remember if the + signs should in fact be & signs though)
<%# "<script>myfunction(" + DataBinder.Eval(Container.DataItem, "SomeNumber") + ")</script>" %>
Another alternative syntax would be the following:
<%# Eval("SomeNumber", "<script>myfunction({0});</script>") %>
This uses the optional parameter where you can supply a format string.
I run into similar codes like this all the time in aspx pages:
<asp:CheckBox Runat="server" ID="myid" Checked='<%# DataBinder.Eval(Container.DataItem, "column").Equals(1) %>'>
I was wondering what other objects I have access to inside of that <%# %> tag. How come DataBinder.Eval() and Container.DataItem are not visible anywhere inside .CS code?
Within <%# %> tags you have access to
Anything that is visible in your code-behind class (including protected methods and properties).
Anything declared on the aspx page using <#import #>.
Anything passed in as the event arguments when the ItemDataBound event is fired (e.g. RepeaterItemEventArgs, DataListItemEventArgs, etc).
Container is actually a wrapper for RepeaterItemEventArgs.Item, DataListItemEventArgs.Item, etc. So you can actually access it in code behind within your ItemDataBound events as e.Item (e normally being the event arguments parameter name).
DataBinder is also accessible in code behind by using System.Web.UI.DataBinder.
On a side note, casting the Container.DataItem is preferred over using Eval. Eval uses reflection so there's an overhead there. In VB.NET it would be something like
<%#DirectCast(Container.DataItem, DataRow)("some_column")%>
Or C#
<%#((DataRow)Container.DataItem)["some_column"].ToString()%>
I believe you have access to anything within scope of the page class, though the results of the expression are converted to a string, so you can't embed conditional expressions the way you can with "<%" expression holes.
Here is a nice blog post which dives under the covers of the generated ASPX class.
Hope this helps.
<%# is specific to inline ASPX databinding like the link ckramer posted suggests.
How come DataBinder.Eval() and Container.DataItem are not visible anywhere inside .CS code?
To access the binding item in codebehind you would need to set up an ItemDataBound event.
ASP.NET generates a subclass of TemplateControl for each occurence of a template. Databinding statements are expressions used in a method inside that class. Thus, you can call any public/protected instance method on TemplateControl. See any example that uses XPath, as those will use the XPath and XPathSelect methods; Eval, XPath and XPathSelect are all instance methods on TemplateControl.
DataBinder is actually a separate class, and Eval is a public static method on it; it's in System.Web.UI. DataBinder.Eval and plain Eval are not directly related though they do very similar things visibly.
I believe that "Container" is actually a local variable or parameter where databinding statements are compiled. I can't remember its type at the moment.
using <%# %> actually means that code inside this block will execute when page.DataBind() method is being executed. Thus you can access anything at that point accessible as protected/public to that particular page/control.
Great example
<%#((System.Data.DataRow)Container.DataItem)["ColumnName"].ToString()%>