Using asp:Literal text in html attributes - c#

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

Related

C# NotSupportedException when adding InnerHTML into a HTML table

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>

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.

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>

ASP.Net Checkboxlist Background color

I have a checkboxlist on aspx
<asp:CheckBoxList ID="cblCalendarFilter" runat="server" />
I add the listItems from code behind.
newCkItm = new ListItem();
newCkItm.Text = childrenFoldersData[i].Description.Trim();
newCkItm.Value = childrenFoldersData[i].Id.ToString().Trim();
newCkItm.Selected = true;
I can add the background of the ListItem by
newCkItm.Attributes.Add("style", "background-color:Red;");
The colors will be different from one item to another and the name of the item will also be different. So, the problem is the background color is only covering the text length. The background color is not aligned for all Items. I checked with the inspector and found out that..
The background color style is applying to <span> surrounding the text
All spans are inside the <td>.
I am wondering if there is a way to apply that style to <td> without much effort. I'd rather not use jquery and javascript by searching the name.
Is there any way to do that??
I have tried and found this solution & it worked for me.
Just try to give padding-right
for example:
newCkItm.Attributes.Add("style", "background-color:Red;padding-right:30px");
Instead of adding a style attribute to each item, and because you mentioned each item needs a different background colour, I'd add an id attribute:
newCkItm.Attributes.Add("id", "alpha");
That way you can keep all your styles separate from your code and not have to recompile, etc. every time you need to tweak the CSS.
With the <asp:CheckBoxList /> the CSS itself would look something like this:
#cblCalendarFilter {
border:none;
border-collapse:collapse;
}
#cblCalendarFilter td {
padding:0;
}
#cblCalendarFilter span {
display:block;
padding:2px;
}
#cblCalendarFilter #alpha {
background:red;
}
#cblCalendarFilter #beta {
background:yellow;
}
Hopefully making the span block-level will fix the issue you had with the background colour only covering the text length.
How about if you use a Repeater which has tr/td with CheckBox inside it and do the logic on the ItemDataBound event where you can retrieve the td:
<table>
<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_ItemDataBound">
<ItemTemplate>
<tr>
<td id="myTd" runat="server">
<asp:CheckBox runat="server" ID="myCheckBox" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
I think there is no other way, you have to individually access the element and assign the css to items. I tend to use foreach loop, access the item and add attributes. TBH, I would stay with checkboxlist compared to repeater, unless I need some extra functionality

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