Asp.Net code blocks not executed - asp.net

I have the below code in my ascx file.
The enclosing <tr> has runat="server".
<td id="loading" style='<%= ShowLoadingImage("PageLoad") %>'></td>
This code is rendered to HTML without being executed, i.e. I see the same text on the html.
Whats the mistake I am doing?

You can not use <%..%> inside a control that has runat=server or in a control that is nested with some other control having runat=server.
I would suggest, make <td id="loading" runat="server"... and in server side code assign style like
loading.Attributes.Add("style", ShowLoadingImage("PageLoad"));

Related

Combine code with info from databound item in ASP.NET markup code blocks

Man, I never really learnt all the embedded code blocks and stuff you can use in ASP.NET. What I'm trying to do is the following:
I have a repeater
It renders a table
In each row, I need to add a data-bind attribute (yes, for Knockout) containing some text and the rowindex.
More specifically, I want to render:
<table>
<tr data-bind="with:myItems()[0]">
...
</tr>
<tr data-bind="with:myItems()[1]">
...
</tr>
<tr data-bind="with:myItems()[2]">
...
</tr>
</table>
I've tried:
data-bind="<%# String.Format("myItems()[{0}]", Container.ItemIndex) %>"
But that doesn't work (data-bind="<%# Container.ItemIndex %> will however. So I'm trying to combine code with information from the databound item.
I know there is a foreach binding in Knockout, but I can't use it because:
I want/need my HTML to be constructed server-side initially
There's other, specific javascript that needs the HTML to exist already so I can't let Knockout populate the table
I'm using an ASP.NET Repeater, which doesn't mix well with Knockout's templates.
I also know, I could just do this in code-behind (with <tr runat="server" ... >) but I'm trying to put all my layout and javascript in markup and js files, not in C# code.
So, can I, in some way, add code in my markup to combine text I choose, with info from the current databound item?
Bummer, apparently, the answer is dead simple, and it didn't work the first time because of another mistake I made:
<tr data-bind="with: myItems()[<%# Container.ItemIndex %>]">
I put more info on my Blog and a working example on GitHub.

How do I set html table caption from code behind? ASP.NET

I have a table in my aspx page:
<table id="tbl" runat="server">
</table>
I need to set to set the table caption in the code behind, so that it renders as follows:
<table id="tbl" runat="server">
<caption>Monthly savings</caption>
</table>
Any help will be greatly appreciated.
The previous response from Brad M is almost correct, you must add a runat="server" attribute, an ID attribute and set it to some value you see fit, and then on the server side code:
One big caveat thought, you need to place the caption before the table element, inside is not possible
idYouGave.InnerText = "Monthly savings";
Since you can't use the directly inside the , do something like this to achieve what you want:
<tr>
<th colspan="numOfCols"><caption>...</caption></th>
</tr>
Just add the runat="server" attribute to your caption element, as well as give it an ID. Then just refer to it in code behind as caption.InnerText = "Monthly savings";
It is not possible. Control HtmlTable can contain <tr> and only them, everything else will be removed. Here is the full note from MSDN:
A complex table model is not supported. You cannot have an HtmlTable
control with nested <caption>, <col>, <colgroup>, <tbody>, <thead>, or
<tfoot> elements. These elements are removed without warning and do
not appear in the output HTML. An exception will be thrown if you
attempt to programmatically add these table model elements to the
Control.Controls collection of the HtmlTable control.
Your options are either to switch to asp:Table control, or switch back to plain markup.

how to get html control in VB code behind for aspx?

WIth aspx, there are UI part(.aspx) and code behind(.aspx.vb).
In Markup, there is some html element, like Table with ID='Tab1'
How to got the html element Table in code behind to change its attributes?
Make the table accessible in code behind by assigning it id and making it runat="server"
<table id="tbl" runat="server" >....

asp:Repeater within asp:Content

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).

'ImageButton' must be placed inside a form tag with runat=server

I've got the ImageButton wrapped inside a form tag with runat="server" but STILL getting this error at runtime. The form tag is at the very beginning (before my table) and end tag is at the end (after the table).
<td>
<div>
<div id="pay-Button"><asp:ImageButton ID="PayButton" ImageUrl="<%=PayButtonImageUrl %>" OnClick="RedirectTest" runat="server" /></div>
</div>
</td>
You can't have managed controls within a <form> tag without the runat='server'. ASP.Net supports multiple form tags, but with only one of them having a server-side designation. Also, I don't believe that HTML supports nested forms. You'll either want to have it be a non-managed control in a separate form or remove the nested <form> tag from the server-side form.
Look here for further explanation.
remove the ImageUrl="<%=PayButtonImageUrl %>" part, I don't believe you can use those inline snippits for server control properties (unless you're databinding)
Does your tag also have
runat="server"
as an attribute?
It would be helpful if you showed more of your code so we can help.

Resources