how to get html control in VB code behind for aspx? - asp.net

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

Related

Container id structure on controls added to placeholder in repeater

Just ran across a weird thing. We have a page with items rendered in a Repeater. The items in the list have one panel for display and a PlaceHolder for edit mode where controls are added in the item data binding.
We have some client side code working with the fields that got added, and the code was expecting the ids to follow the same container structure you usually see, but for some reason, one level is getting skipped and I don't know why.
With the template below, you'd have the outer panel with an id like ...Repeater_ctl46_ctl100_Display. The PlaceHolder itself doesn't render, nor get included in the id structure in the html, but the id of a HiddenField added to the EditPlaceHolder ends up like ...Repeater_ctl46_ctl100_valueHidden.
The _Display level just doesn't appear. This threw off our client side script because it was expecting the outer panel container to be reflected in all the child controls in the edit template, and it was using that to look them up.
Anyone know why the containing Panel wouldn't be in the id hierarchy?
<ItemTemplate>
...
<asp:Panel ID="Display" runat="server">
<asp:Panel ID="ViewPanel" runat="server"><asp:Label ID="ValueLabel" runat="server" /></asp:Panel>
<asp:PlaceHolder ID="EditPlaceHolder" runat="server" />
</asp:Panel>
<asp:Panel ID="ModButtons" runat="server"><asp:LinkButton ID="EditButton" Text="<%$ Resources:Messages,Edit %>" runat="server" /></asp:Panel>
...
</ItemTemplate>
Okay, after a lot of digging, asp:Panel does not implement INamingContainer, which is why the Panel is not participating in the id naming conventions.
INamingContainer is just a marker interface; it has no properties or methods associated.
So if you want a Panel that can participate in naming conventions, you can make a little derivation and use that instead:
public class NamingPanel : Panel, INamingContainer
{...}

Asp.Net code blocks not executed

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

Dynamic CssClass using inline tag

Is there a way to dynamically set the css class name dynamically using the following construct:
<asp:Panel ID="pDisplayMode" class="dynaClass<%= this.codeBehindVar )%>" runat="server">
I tried this as well to no avail:
<asp:Panel ID="pDisplayMode" CssClass="dynaClass<%= this.codeBehindVar )%>" runat="server">
I realize that I can manipulate the CssClass in the code behind, however, I would like to keep it in the markup only, if possible.

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.

Display html text as html

There's a moment in my page where I connect to a database and retrieve some data. One field in special is html code. I want to display it in my page as html inside some control that understands it.
What are my options?
Use a literal.
.aspx:
<asp:Literal runat="server" ID="myLiteral" />
codebehind:
myLiteral.Text = "<h2> this is a h2 html tag</h2>";
this will print out
This is a h2 html tag

Resources