C# NotSupportedException when adding InnerHTML into a HTML table - c#

In my .aspx file I have the table below:
<table id="table1" style="width: 100%;" runat="server"></table>
I want to access this table and insert html into this table in C#(.aspx.cs),I tried this:
HtmlTable table = (HtmlTable)(form1.FindControl("table1"));
table.InnerHtml = "<tr><td></td></tr>";
But I got NotSupportedException. How can I solve this?

If the control does not have a runat="server" your code behind will not be able to access the object at runtime. :)

use runat server to make the table available serverside
<table runat="server" id="table1" style="width: 100%;"></table>
For reference you could take a look at this SO question - Why does ASP.NET webforms need the Runat=“Server” attribute?

If you want to be able to retrieve the table in the code-behind, you need to use a .Net control and declare your table using
<asp:Table runat="server" ... />
...
</table>

Related

Using asp:Literal text in html attributes

quick question. Is there a way to use asp:Literal text in an HTML attribute?
Example
<asp:Literal Text="hidden" runat="server" ID="ClassTag"></asp:Literal>
<tr class='<%= ClassTag %>' run="server" > </tr>
I am working on an overall solution to a repeater table row collapsing problem (asp:repeater collapsing table rows) for context, and this is perhaps the last thing I'm stuck on.
Please let me know. Thanks!
Not really, but are you just trying to set the class on the TR from code?
In that case, write the following the ASP.NET:
<tr runat="server" ID="CustomRow">
And in the codebehind set the class through the Attributes collection:
CustomRow.Attributes.Add("class", "[desired css class]");

how to remove an tag attribute

I have a table that has a summary tag that I want to remove
<table summary="Summary">...
I need to remove the summary tag and I can't use jquery. I need to remove it from the page source.
Is this possible with c#?
Without jQuery:
document.getElementById("myTable").removeAttribute("summary");
or with C#:
<table runat="server" id="myTable" summary="Summary">...
(mark-up)
myTable.Attributes.Remove("summary");
(C#)
Note, the above assumes you're working in asp. net.

C# Repeaters while amending some figures

I have a very large finance table, which will be repeated 4 times on a page, across many different pages. I don't like the idea of just copying the table across pages. Am I correct in thinking that I can create a repeater which repeats it? But also at the same time amend figures in the backend?
I have looked around, but wasn't to sure whether the amending of figures was possible as well as more efficient?
<div class="span3 finance-boxes">
<table class="table table-bordered table-hover finance-table">
<tr><td>Title</td></tr>
<tr><td class="fig">Figure</td></tr>
<tr><td>Title</td></tr>
<tr><td class="fig">Figure</td></tr>
<tr><td>Launch Finance Details</td></tr>
</table>
</div>
As I understand from your explanation you need to repeat the same piece of HTML on many places with only difference of some values of some cells. So I suggest you create a new ASCX control.
Webusercontrol is very similar to asp.net page, but has .ascx extension and can be inserted onto page or other control. So to create it use Add -> New Item -> Web User Control in VisualStudio.
The web user control will have markup file .ascx and code-behind .ascx.cs file.
Place your html on markup file:
<div class="span3 finance-boxes">
<table class="table table-bordered table-hover finance-table">
<tr><td>Title</td></tr>
<tr><td class="fig" runat="server" ID="cell1"></td></tr>
<tr><td>Title</td></tr>
<tr><td class="fig" runat="server" ID="cell2"></td></tr>
<tr><td>Launch Finance Details</td></tr>
</table>
</div>
Now in the code-behind you can access your cells by cell1 and cell2 and you can change their inner html by cell1.InnerHtml property.
You can create public properties in your code behind file to have access to these cells from the page:
public string Cell1Text
{
get
{
return cell1.InnterHtml;
}
set
{
cell1.InnterHtml = value;
}
}
Then you can place your web user control on your page.
You can read more about this here:
http://weblogs.asp.net/scottgu/archive/2006/11/26/tip-trick-how-to-register-user-controls-and-custom-controls-in-web-config.aspx
Finally you will be able to access and modify your control's properties by this piece of code:
myUserControlName.Cell1Text = "NEW TEXT";

Making non-asp table invisible in behind-code

I'm working with code someone else has written that I cannot change too much for now.
It has a table defined in the html, something like this:
<table id="tblResult">
some stuff defined in here.
</table>
I would like to use the behind code to make this table and all its contents invisible, but I notice I can't address the table directly as tblResult.visible in the code behind. This makes sense to me, since this is not an asp object. Simply changing this to an asp:table doesn't work, as there's some stuff going on inside that table I prefer not to mess with. Is it possible to address that table and set visibility to false from the behindcode?
Wrap it into a <asp:PlaceHolder> amd then toggle the placeholder visibility.
Add runat='server' to the tag. The other thing you can do is wrap it around a server side tag of div, panel, etc and set them to visible='false' Something to this effect:
<div id='myDiv' runat='server'>
<table id="tblResult">
//stuff
</table>
</div>
Then in your code-behind:
this.myDiv.Visible=False;
This will now ensure your table is not visible. Again you can use div's, panels (which are just divs really), literals, placeholders, etc.
You can wrap it in a Literal:
<asp:Literal runat="server" ID="Literal1" Visible="False">
<table> ... </table>
</asp:Literal>

Selecting an element without an Id

I'm trying to select an element from my webpage...
I have inserted a control into my page and i need to set some values an element inside the control at pageload from c# code.. The thing is, as soon as I insert the control into the page... A prefix is appended to the id name... Because of that new name, my css definition won't be appended...
Is there any way to access this element from C# without the need to make it an Id?
Edit: To clarify what Im trying to do here. Im trying to make a generic control, which gets a width and height set to it's parameters. I need to set this manually to the element by adding a style attribute. I can't make the element an id, because this will stop the possibility of making it generic.
This is whats inside of the control... the fact is, I need the imageRotatorDiv to be a class instead of an id. Otherwise i can't use multiple image rotators on one page.
But how can I select a class in a page from c# code? Is it possible?
<div id="imageRotatorDiv" runat="server">
<div class="imageRotator">
<asp:Repeater ID="rprImages" runat="server">
<ItemTemplate>
<asp:Image ID="imgItem" runat="server" />
</ItemTemplate>
</asp:Repeater>
</div>
</div>
you can define your style to a class name instead of id.
<asp:TextBox ID="MyText" CssClass="someclass" runat="server" />
html output
<input type="text" id="Something_MyText" class="someclass" />
css
.someclass { border:solid 1px red; }
In JQuery :
$("div[id$=MyDiv]").addClass("myDiv")
And you just need to define the myDiv CSS class. Or, to modify directly the style :
$("div[id$=MyDiv]").css("font-size", "14px");
JQuery details

Categories