<script> tags inside an <asp:repeater> - asp.net

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.

Related

The Controls collection cannot be modified because the control contains code blocks - Error only when it is in head block

Agree my question is duplicate of this one and accepted answer works for me too. Let me clarify why.
When I have <%= in head it gives error.
When I have <%= in body it works.
When I have <%# in head it works.
I am just curious to know the reason for all three scenarios.
Additionally I created test project to emulate the issue but in that case all three situation works.
My page is too big and I am unable to decide what code to paste.
<%= %> is in fact doing Response.Write, which is literally writing symbols to the response. To the final markup that is.
Now notice that your head tag has this attribute runat="server". That makes it a server control. That is, this is not a final markup, and but rather a control that will output some markup to response during the control rendering stage. You cannot call Response.Write on this control, because it is not a final markup yet.
For the same reason it would work/not work in the body of the page. If you put it somewhere in plain markup it would work no problem:
<div><%= "Blah" %></div> <%-- works! --%>
But as soon as it appears inside anything with runat="server" you'll get an error
<div runat="server><%= "Blah" %></div> <%-- error! --%>
<asp:Panel runat="server"><%= "Blah" %></asp:Panel> <%-- error! --%>
Now <%# %> is a different beast. This is a data binding markup, something that is being evaluated when the server side control is being data bound. Thus is makes no sense (and is invalid) inside plain markup, and can be used whenever your control is bound to some data. Using it with header is not very common, use cases with GridView or Repeater are the most typical ones that come to mind.

ASP.Net Placeholder vs if directive

When working with markup if I want to include some content conditionally, I use a placeholder in a normal way:
<asp:Placeholder Visible=<%# IsExpired %>
<span>Prolong your subscription</span>
</asp:PlaceHolder>
Also I can use if-directive:
<% if(IsExpired) {%>
<span>Prolong your subscription</span>
<% }%>
I prefer using the first one just because it does not make my markup messy. And what's the best way to conditionally include content? From the performance point of view, are they similar?
native HTML tags are always faster than rendering server controls as there is no time spent for rendering them
I think there is hardly anything to do with performance whether way you choose here. But actually you may use the following code:
<asp:Label runat="server" Visible=<%# IsExpired %>
Prolong your subscription</asp:Label>
instead the other two. This could make it look more straight-forward.
I'd never use C# code in Web Forms view. In addition I will avoid setting the Visible property in the markup and I will set it in the code behind on some event.
phWhatever.Visible = IsExpired;
Quite often you can avoid creating the IsExpired property.
Of course what #Johnny suggested is correct. If you need to hide what is effectively only one control you hide the control directly.

How to access the current row's data in telerik's grid?

I have a grid, and say I am in given telerik:GridTemplateColumn (or GridboundColumn?) I want to display the result of 2 other columns, how would I do this?
In asp.net repeater I could do:
<%# Container.DataItem("Col3") %> <%# Container.DataItem("Col4") %>
How would I do this in a telerik grid?
You should be able to use very similar syntax. Inside the GridTemplateColumn, you need to make an ItemTemplate. In that, you can put whatever markup you want to be displayed in the column. For example:
<telerik:GridTemplateColumn HeaderText="Col3 and 4" UniqueName="TemplateColumn">
<ItemTemplate>
<%# Container.DataItem("Col3") %> <%# Container.DataItem("Col4") %>
</ItemTemplate>
</telerik:GridTemplateColumn>
For more information, you can reference the documentation for the various column types: http://www.telerik.com/help/aspnet-ajax/grid-column-types.html (you'll need to scroll down a ways to get to the section on GridTemplateColumn.
Also, I'm assuming you are using Visual Basic? Otherwise, I think the syntax would be different.

ASP.NET Code Expression, Data Binding, and other Declarative Expressoins

What are the differences in these tags?
<%
<%#
<%=
<%$
More importantly, how do I display a page property using declarative syntax in an ASP.NET control? I'm trying to do this in an ASP.NET control. The task is to set the text of a label but I do not want to do this pro grammatically in the event I want to change the output control. I get an error about server side controls can't contain this syntax. I'm not sure that I need a databound control for what I want to do but that is another option.
Partial answer coming up.
Update
There is a new tag I've seen in ASP.NET 4.5? site
<%:
Partial answer
quoted from Mike Banavige
<% %> An embedded code block is
server code that executes during the
page's render phase. The code in the
block can execute programming
statements and call functions in the
current page class.
http://msdn2.microsoft.com/en-gb/library/ms178135(vs.80).aspx
<%= %> most useful for displaying
single pieces of information.
http://msdn2.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx
<%# %> Data Binding Expression Syntax.
http://msdn2.microsoft.com/en-us/library/bda9bbfx.aspx
<%$ %> ASP.NET Expression.
http://msdn2.microsoft.com/en-us/library/d5bd1tad.aspx
<%# %> Directive Syntax.
http://msdn2.microsoft.com/en-us/library/xz702w3e(VS.80).aspx
<%-- --%> Server-Side Comments.
http://msdn2.microsoft.com/en-US/library/4acf8afk.aspx
Update:
Okay this appears to work
<asp:Label ID="MyLabel" runat="server" Text='<%# MyProperty%>'></asp:Label>
If I use the eval syntax then I get an error about databound control or I use the <% then I get a server side controls error. Any more color appreciated.. not sure I really understand what is going on.
Perhaps it has something to do with the render phase.
Few more observations:
I can use <%= without databinding and get the property value but can not use it in a server side control without getting error.
If I use <%# in server side control but I'm required to do a Page.Databind.
Interestingly, I can use either <%= or <%# when I want to render text that is not inside a control. Although the latter requires databinding.
The new <%: syntax is explained, also called code expression syntax
With ASP.NET 4 we are introducing a new code expression syntax (<%:
%>) that renders output like <%= %> blocks do – but which also
automatically HTML encodes it before doing so.
http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2
No, server-side controls can't. For example, I have a string property named SkinPath that give me the full App_Themes path to the current theme. I use it in the following way:
<img src='<%= SkinPath %>/Images/myImage.png' />
However, the following doesn't work:
<asp:Image ID='image' runat='server' ImageUrl='<%= SkinPath %>/Images/myImage.png' />
Instead, it renders the src literally in the result <img>.

Aspx Property Interpolation

I'm a bit new to .Net development, been working in Java for some time now. I have an aspx page and we need to externalize some strings to make it more flexible.
If I have a table somewhere and there is just a string sitting outside an asp tag, I can replace it so that
<th> Specific Foo String </th>
becomes
<th> <%= Strings.foo %> </th>
and everything is fine, the problem I'm running into is how do you do this kind of interpolation on an asp tag property
I tried changing
<asp:Label runat="server" ID="lblFoo" Text="Specific Foo String Entry" />
to
<asp:Label runat="server" ID="lblFoo" Text='<%= Strings.foo %> Entry' />
and
<asp:Label runat="server" ID="lblFoo" Text='<%#Eval("Strings.foo") %> Entry' />
but neither worked. Is what I'm doing not possible in the aspx file, I know that I can simulate this by rewriting their properties in the code behind, but that's a level of overhead I'd rather not deal with.
Thanks
I think you are looking to do this:
<asp:Label runat="server" id="label1" Text='<%# Strings.Foo + " Entry"%>' />
Then in your code behind (most likely in your OnPageLoad) you need to call
if(!Page.IsPostBack) Page.DataBind();
You need to be cautious however as calling DataBind on controls like textboxes or any labels that may have changed due to logic in the code behind will have their values overwritten with the bound values. Checking that you are not on a post back can help with this, but there are still gotchas.
Also note that I had to move the " Entry" text into the binding statement. If it is placed outside the last '%>' then the binding does not work and it will spit out:
<%# Strings.foo %> Entry
In the codebehind of the page you would do this:
lblFoo.Text = Strings.foo + " Entry";
A good place to put this code would be in the overriden OnLoad method but that is simply a suggestion as I am unfamiliar with your application and the life cycle needs of your page.
If you want to do all this in the aspx page then simply do this:
<span><%= Strings.foo %> Entry</span>
as a Label renders as a span anyhow.
If your objective is an HTML table of strings, then you can create either a ListView or a GridView and DataBind to that. It would save you the trouble of writing out all of your properties and will also produce the correct table tags for the data.
Without knowing more about your data, I cannot provide a detailed code snippet.
You're talking about resources. Read Basic Instincts Resources and Localization in ASP.NET 2.0 which shows you the built in resource editor, and how to use the "<%$ ... %>"-binding, or using meta:resourceKey attribute.

Resources