asp:Repeater within asp:Content - asp.net

I have the following piece of code:
<asp:Content id="Content1" runat="server" contentplaceholderid="PlaceHolderPageDescription">
<table class="custom-table">
<asp:Repeater ID="oRepeater" runat="server" >
<ItemTemplate>
<tr onclick="javascript:location.href='/nuovoTema/viewIdea.aspx?ID='">
<td><%# ((SPListItem)Container.DataItem)["ID"] %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</asp:Content>
The compiler complains that In content pages, content is not allowed outside <script> or <asp:Content> regions.
How can I use asp:Content and asp:Repeater together? I have a list to display.
Thanks

Doesn't look like the error is in the part of the page that you posted. Typically this pops up when you either attempt to place content outside of an asp:content block while in a web content page, or b) have some sort of markup error - lack of a closing tag or similiar - that makes the parser think that you're placing code outside of a content block.
Check your tags to make sure everything that needs to be closed is, in fact, closed. A good way to start is to simply delete everything inside of the content block - well, copy it somewhere - and see if the page still complains. (You'll probably get errors in the code behind).

Related

cannot access controls in asp.net application

I am working on an ASP.NET VB.NET Web Application. I inherited a bunch of forms from another application we have in house. I'm running into a very strange problem when working on the Login page.
This is an abbreviated version of my code:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<table>
<tr>
<td>
<span id="Span1" runat="Server" style="Color: Red"></span>
</td>
</tr>
<tr>
<td>
<asp:Login ID="Login1" runat="server">
<LayoutTemplate>
<table>
<tr>
<td>
<span id="Span1" runat="Server" style="Color: Red"></span>
</td>
</tr>
</table>
</LayoutTemplate>
</asp:login>
</td>
</tr>
</table>
</asp:Content>
I have a <span id="span1"> that is located inside my web form, within the Content part of the page. I can easily access this in my CodeBehind, and do whatever I want to do with it. However, if i move that span and put it inside the <asp:login> part of the page, it doesn't seem to recognize it, it won't let me access it in code behind, it gives me a squiggly blue line and says
span1 is not declared. It may inaccessible due to protection level
This bit is from the top of the webform in designer
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Login.aspx.vb" Inherits="_Default" MasterPageFile="~/Site.master" %>
This bit is from the Login.aspx.vb page
Partial Class _Default
Just to say it again, id="span1" works perfectly fine where it is shown in the code above, but when I move it inside the I cannot reference it anymore. Since I'm talking about this issue, for that matter I cannot add any new controls inside because I am not able to reference any other controls in vb.net. (this form was pretty much copied from another project, everything works properly I'm just not sure why I'm having this strange issue)
I noticed that a lot of people have similar issues, but in my case I'm working with <asp:login> and I'm really not sure how it's affecting my controls.
EDIT: <span id="Span1" runat="Server" style="Color: Red"></span>
You need to use FindControl on the Login1 Control
HtmlGenericControl hgc = Login1.FindControl("Span1") as HtmlGenericControl;
hgc.InnerText = "Span Found";
VB
Dim hgc As HtmlGenericControl = CType(Login1.FindControl("Span1"),HtmlGenericControl)
hgc.InnerText = "Span Found"
As per my comments, and as requested by the OP...
You're hitting a problem with the naming container.
When the <span runat="server"> is outside of the <asp:Login><LayoutTemplate> it exists as an object within the page, which you can reference directly.
As soon as it's moved within that <LayoutTemplate> it becomes a child of the <asp:Login> control instead.
So to access the control, you can use the following...
CType(Logon1.FindControl("span1"), HtmlGenericControl).InnerHtml = "hello"
The FindControl will bring back an object, but it needs to be "boxed" into the correct type before you can access the InnerHtml property
to access a control on server side, you must include the "runat='server'" attribute on a tag. That's what tells .NET that any given control is supposed to be worked with on the server side as well as the front-end.
Do note that it will change the ID produced in the rendered HTML
<span id="span1" runat="server"></span>

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.

Linkbutton in ASP.NET MasterPage and child Webform data control creating extra href=""

EDIT Final: Just in case someone else sees this behavior, I wanted to explain my work around. I was using a placeholder on my Master page for the webform described below. I added a server control to the placeholder in the Master page OnInit event. Through a process of elimination, I figured out that the behavior described below only happens when I add this server control to the Master page.
titlebarPlaceHolder.Controls.Add(sctitlebar)
I re-wrote the Master Page to not need the server control added and the behavior described below went away. I have no idea what caused it. It was a simple server control, but this is my work around.
EDIT 2: The same behavior happens when the container is a table in the repeater control:
<asp:Repeater ID="rptAuditList" runat="server">
<ItemTemplate>
<tr class="odd">
<td><asp:LinkButton ID="lnkOpenAudit" runat="server" Text='<%# Eval("auditname") %>'></asp:LinkButton> </td>
</tr>
</ItemTemplate>
</asp:Repeater>
Here is the HTML output:
<td>Demo PreClose July 2012 </td>
EDIT: In my testing I just noticed that if I run the extact same control OUTSIDE of a MasterPage, it worked correctly, but if I run it inside a MasterPage, it behaves in the way described below.
I have tried this with a Repeater, DataList and Listview and the results are always the same.
Here is the HTML:
<asp:ListView ID="lvwAuditList" runat="server" >
<LayoutTemplate>
<ul><li runat="server" id="itemPlaceholder"></li></ul>
</LayoutTemplate>
<ItemTemplate>
<li><asp:LinkButton ID="lnkAudit" runat="server" Text='<%# Eval("auditname") %>' >
</asp:LinkButton></li>
</ItemTemplate>
</asp:ListView>
Here is the output:
<ul>
<li>Demo PreClose July 2012</li>
<li><a id="contentMain_lvwAuditList_lnkAudit_1" href="javascript:__doPostBack('ctl00$contentMain$lvwAuditList$ctrl1$lnkAudit','')">Demo PostClose Audit June 2012</a></li>
</ul>
The first row always has an extra href="" added. I have never seen this behavior before. I have stripped down the html and code behind to its most basic, yet I still get this extra href="". The code behind just sets the datasource and binds it, nothing else.
Thank you.
If you are using the first list tag as a placeholder and replacing the content in the code behind I would use a slightly different approach adding an ASP.NET PlaceHolder inside the list tag as follows:
<ul><li><asp:PlaceHolder runat="server" ID="itemPlaceholder" /></li></ul>
I have experienced some unusual artefacts appearing in Response code when using ASP.NET tags in a manner for which they were not particularly intended. This may well be causing your problem as well!

dynamically populating div in asp.net

I am working on a weboage that will display questions and answers (maybe 5 at one time, maybe 7 at another time) returned from a database table. The questions will each be displayed in a div and the related answers displayed in another div. The question will have an icon "Show Answer / Hide Answer"
How can I go about creating a div and then populating it with values from a table?
Thanks
I would use repeater for that.
1.Create data source pulling data from your database
<asp:sqlDataSource Id="sqldsQuestionsAnswers" ... />
2.Create repeater linking to that data source:
<asp:repeater DataSourceId="sqldsQuestionsAnswers" runat="server">
<itemTemplate>
<div>
<%# Eval("question") %>
<hr/>
<%# Eval("answer") %>
</div>
</itemTemplate>
</asp:repeater>
The repeater will display anything whats in <itemTemplate> tag for every row returned by your query.
So if your query returns 2 questions like that:
Question-------------Answer
-----------------------------------
question1?----------answer1
question2?----------answer2
The output would be:
<div>
question1?
<hr/>
answer1
</div>
<div>
question2?
<hr/>
answer2
</div>
I hope it helps...
We need to know more about how you retrieve your question data and the context of the rest of your page, but you can do a few things here (roughly in order of preference):
Bind your data to an <asp:Repeater > control (or even one of the grid controls)
Build a custom or user control to render your questions and drop that on your page
Build your desired html as a string in code and set it to <asp:Panel > control (Panels render as div tags). If you want to be able to refresh your div without reloading the entire page (AJAX), you can use an <asp:UpdatePanel >.
Build your desired html in code and write directly to the response, either via <%= %> or <%: %> bee-stings or with the Response.Write() method.

<script> tags inside an <asp:repeater>

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.

Resources