how to remove an tag attribute - c#

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.

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

Changing HTML element's style property from code behind

I have a HTML page which is dynamically generating by server. The application has an IDE to generate and design pages then deploy the server. The server displaying this pages in an iframe. We can use all c# methods as well as Page_Load and Page_PreRender events in pages. But I can't modify source code of the asp.net page (I mean can't add runat="server").
What I want to do, finding a html tag by css class (#form1 > span) before pre-render then add a new css property in code behind.
<form id="form1" action="DocumentViewer.aspx" method="post" autocomplete="off">
<span>
<table>
<tr>
<td></td>
</tr>
</table>
</span>
Without runat="server" you cannot access the control in code behind. Best way to do it is to inject the jquery script from code behind to do the same work.
Please take a look at this answer.

How do I toggle twitter bootstrap as validation in web form?

I know I can place any value from my code behind virtually anywhere on my aspx by using:
<%=myString%>
I also know that in order for me to make a field required using twitter bootstrap I need to use required before my tag closes as in:
<asp:TextBox ID="txtFName" runat="server" CssClass="standard_tb" required/>
In my project I want to be able to set that variable to required or null.
When I try using the method above I get the The server tag is not well formed if i use <%#myString%> within my label or I get the error Server tags cannot contain <% ... %> constructs if I use <%=myString%>
How do I get around this?
As you've seen you cannot add <% %> tags to a .NET server control.
What you need to do is in the code behind set:
if (someCondition)
txtFName.Attributes.Add("required", null);
else
txtFName.Attributes.Remove("required");
If you need XHTML compliance then instead you'd use the line:
txtFName.Attributes.Add("required", "required");
Which would render as:
<input id="txtFName" class="standard_tb" required="required" />

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 edit CSS style of a div using C# in .NET

I'm trying to grab a div's ID in the code behind (C#) and set some css on it. Can I grab it from the DOM or do I have to use some kind of control?
<div id="formSpinner">
<img src="images/spinner.gif" />
<p>Saving...</p>
</div>
Add the runat="server" attribute to it so you have:
<div id="formSpinner" runat="server">
<img src="images/spinner.gif">
<p>Saving...</p>
</div>
That way you can access the class attribute by using:
formSpinner.Attributes["class"] = "classOfYourChoice";
It's also worth mentioning that the asp:Panel control is virtually synonymous (at least as far as rendered markup is concerned) with div, so you could also do:
<asp:Panel id="formSpinner" runat="server">
<img src="images/spinner.gif">
<p>Saving...</p>
</asp:Panel>
Which then enables you to write:
formSpinner.CssClass = "classOfYourChoice";
This gives you more defined access to the property and there are others that may, or may not, be of use to you.
Make sure that your div is set to runat="server", then simply reference it in the code-behind and set the "class" attribute.
<div runat="server" id="formSpinner">
...content...
</div>
Code-behind
formSpinner.Attributes["class"] = "class-name";
This question makes me nervous. It indicates that maybe you don't understand how using server-side code will impact you're page's DOM state.
Whenever you run server-side code the entire page is rebuilt from scratch. This has several implications:
A form is submitted from the client to the web server. This is about the slowest action that a web browser can take, especially in ASP.Net where the form might be padded with extra fields (ie: ViewState). Doing it too often for trivial activities will make your app appear to be sluggish, even if everything else is nice and snappy.
It adds load to your server, in terms of bandwidth (up and down stream) and CPU/memory. Everything involved in rebuilding your page will have to happen again. If there are dynamic controls on the page, don't forget to create them.
Anything you've done to the DOM since the last request is lost, unless you remember to do it again for this request. Your page's DOM is reset.
If you can get away with it, you might want to push this down to javascript and avoid the postback. Perhaps use an XmlHttpRequest() call to trigger any server-side action you need.
Add the runat="server" attribute to the tag, then you can reference it from the codebehind.
Add runat to the element in the markup
<div id="formSpinner" runat="server">
<img src="images/spinner.gif">
<p>Saving...</p>
</div
Then you can get to the control's class attributes by using
formSpinner.Attributes("class")
It will only be a string, but you should be able to edit it.
How do you do this without runat="server"? For example, if you have a
<body runat="server" id="body1">
...and try to update it from within an Updatepanel it will never get updated.
However, if you keep it as an ordinary non-server HTML control you can. Here's the Jquery to update it:
$("#body1").addClass('modalBackground');
How do you do this in codebehind though?
If you do not want to make your control runat server in case you need the ID or simply don't want to add it to the viewstate,
<div id="formSpinner" class="<%= _css %>">
</div>
in the back-end:
protected string _css = "modalBackground";
If all you want to do is conditionally show or hide a <div>, then you could declare it as an <asp:panel > (renders to html as a div tag) and set it's .Visible property.
To expand on Peri's post & why we may not want to use viewstate the following code:
style="<%= _myCSS %>"
Protected _myCSS As String = "display: none"
Is the approach to look at if you're using AJAX, it allows for manipulating the display via asp.net back end code rather than jquery/jscript.

Categories