Add CSS class to a div in code behind
What if:
<asp:Button ID="Button1" runat="server" CssClass="test1 test3 test-test" />
To add or remove a class, instead of overwriting all classes with
BtnventCss.CssClass = "hom_but_a"
keep the HTML correct:
string classname = "TestClass";
// Add a class
BtnventCss.CssClass = String.Join(" ", Button1
.CssClass
.Split(' ')
.Except(new string[]{"",classname})
.Concat(new string[]{classname})
.ToArray()
);
// Remove a class
BtnventCss.CssClass = String.Join(" ", Button1
.CssClass
.Split(' ')
.Except(new string[]{"",classname})
.ToArray()
);
This assures
- The original classnames remain.
- There are no double classnames
- There are no disturbing extra spaces
Especially when client-side development is using several classnames on one element.
In your example, use
string classname = "TestClass";
// Add a class
Button1.Attributes.Add("class", String.Join(" ", Button1
.Attributes["class"]
.Split(' ')
.Except(new string[]{"",classname})
.Concat(new string[]{classname})
.ToArray()
));
// Remove a class
Button1.Attributes.Add("class", String.Join(" ", Button1
.Attributes["class"]
.Split(' ')
.Except(new string[]{"",classname})
.ToArray()
));
You should wrap this in a method/property ;)
Apply CSS for an HTML generic control like UL and LI in ASP.NET
Please try this,
ull.Style.Add("background-color", "Red");
Or, I have tested this, will definitely work, please check
ull.Attributes.Add("class", "yourClass");
Edit:
To test this solution I have provided you:
make new blank master page and put <ul runat="server" id="ull">
then add a new page and use the above master page.
make findcontrol ul and put in CSS as I have mentioned in the answer.
then run your page, and view source of your HTML page and you will find what you are looking for. Like
Add CSS class to DnnModule outer div
Found an answer on the DotNetNuke.com forums:
<script runat="server">
Private Sub Page_PreRender(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
Try
Dim cParent As HtmlGenericControl = CType(Me.Parent, HtmlGenericControl)
cParent.Attributes("class") = cParent.Attributes("class") + " span6"
Catch ex As Exception
End Try
End Sub
</script>
Adding this to the container .ascx file allows me to insert my own specific class to the wrapper div.
Source: http://www.dotnetnuke.com/Resources/Forums/forumid/109/threadid/458919/scope/posts.aspx
How to change CSS class of a HTML page element using ASP.NET?
The FindControl method searches for server controls. That is, it looks for controls with the attribute "runat" set to "server", as in:
<li runat="server ... ></li>
Because your <li> tags are not server controls, FindControl cannot find them. You can add the "runat" attribute to these controls or use ClientScript.RegisterStartupScript to include some client side script to manipulate the class, e.g.
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script language=\"javascript\">");
sb.Append("document.getElementById(\"li1\").className=\"newClass\";")
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "MyScript", sb.ToString());
how to change a css class of div in master page from update panel in content page
Without the UpdatePanel you would use FindControl to change the class.
Panel panel = Master.FindControl("Panel1") as Panel;
panel.CssClass = "myClass";
But since you use an UpdatePanel, the easiest way is to use jQuery.
On the master page the Panel and the script
<asp:Panel ID="Panel1" runat="server">Welcome to StackOverflow</asp:Panel>
<script type="text/javascript">
function changeClass(className) {
$("#<%= Panel1.ClientID %>").attr("class", className);
}
</script>
Then in the code behind of the aspx page, you can call that javascript function on PostBack.
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "changeClassName", "changeClass('myClass');", true);
Changing user control css class in code behind
So I solved the problem by defining the ListViews ItemDataBound event handler and assigning the css class in there. The problem previously was, that since my controls were inside updatePanels the ItemDataBound event was not firing. I had to manually call "DataBind()" inside the click method.
Inside my default.aspx
// Exposed click event, updates RecentBooks control sqlDataSource.
private void SidebarItems1_Clicked ( object sender , EventArgs e ) {
RecentBooks1.updateDataSource( SidebarItems1.FacultyId.ToString() );
SidebarItems1.DataBind();
}
Inside my user control.ascx
protected void sidebarListView_ItemDataBound ( object sender , ListViewItemEventArgs e ) {
LinkButton button = e.Item.FindControl( "NameLabel" ) as LinkButton;
if (FacultyName != null && button != null) {
if ( button.Text == FacultyName ) {
button.CssClass = "sidebarItemActive";
}
}
How to add HtmlGenericControl to a HtmlNode?
Try the code below. you will need to make some changes. controls innerHTML is well formed and can be used as innerXML for an xmlNode
XmlDocument doc = new XmlDocument();
//Load your xml here TODO
var divEl = doc.DocumentElement.SelectSingleNode("//div[@id='test']"); //Change your xpath here TODO
var newdiv = new HtmlGenericControl("div");
newdiv.Attributes.Add("id", "id");
newdiv.Attributes.Add("text", "text");
newdiv.Attributes.Add("class", "class");
XmlNode newNode = doc.CreateNode("NodeType", "NodeName", "URI/if/any"); //update your variables here
newNode.InnerXml = newdiv.InnerHtml;
divEl.AppendChild(newNode);
Hope this helps
Related Topics
How to Determine the Standard Deviation (Stddev) of a Set of Values
Incorrect List Model Binding Indices When Using HTML Helpers
How to Install Android APK from Code in Unity
How to Convert JavaScript Date Object to Ticks
Order of Serialized Fields Using JSON.Net
Best Way to Determine If Two Path Reference to Same File in C#
Use the Long Reserved Word as a Variable Name in C#
C# Tcpclient: Send Serialized Objects Using Separators
How to Redirecttoaction in ASP.NET MVC Without Losing Request Data
Better Naming in Tuple Classes Than "Item1", "Item2"
Getting HTML Body Content in Winforms Webbrowser After Body Onload Event Executes
How to Manage Files on an Mtp Portable Device
Is There a Standard Way to Encode a .Net String into JavaScript String for Use in Ms Ajax
How Does Hashset Compare Elements for Equality
Serialization of Entity Framework Objects with One to Many Relationship
Could Not Load File or Assembly System.Runtime.Compilerservices.Unsafe