How to Add Style from Code Behind

How do you modify a CSS style in the code behind file for divs in ASP.NET?

testSpace.Style.Add("display", "none");

How to add style from code behind?

:hover is a selector, and not a style. What you're doing in your example is adding inline styles to an element, and a selector equivalent for that obviously doesn't make much sense.

You can add a class to your link: hlRow.CssClass = 'abc';
And define your class as such:

a.abc:hover {
...
}

Add mulitple inline style from code behind

Here is a way to write it in one line

imgStory1.Attributes.Add("style", "display: center;border: none; outline: none");

for overwriting existing

imgStory1.Attributes["style"] = "display: center;border: none; outline: none";

Creating a Style in code behind

You need to add setters to the style rather than using RegisterName. The following code, in the Window_Loaded event, will create a new TextBlock style which will become the default for all instances of a TextBlock within the Window. If you'd rather set it explicitly on one particular TextBlock, you can set the Style property of that control rather than adding the style to the Resources dictionary.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
Style style = new Style(typeof (TextBlock));
style.Setters.Add(new Setter(TextBlock.ForegroundProperty, Brushes.Green));
style.Setters.Add(new Setter(TextBlock.TextProperty, "Green"));
Resources.Add(typeof (TextBlock), style);
}

How to set a:before css style from code behind aspx.cs page

After researching on it finally i found a solution to my problem. For this i have created an object for LiteralControl, then created a style tag as a string using string builder by appending my property in it, now i have initialized LiteralControl's Text property to above style tag string and finally added the LiteralControl to page header.

It is rendering the generated style tag with property values in my page header and rendered style is applied on my page. Following is the code i used in code behind:

System.Web.UI.LiteralControl ltr = new System.Web.UI.LiteralControl();
StringBuilder sb = new StringBuilder();
sb.Append("<style type=\"text/css\" rel=\"stylesheet\"> .m-quick-links ul li a:before { background-image: url(");
sb.Append(FirstLinkIcon); //here i am appending property value in css style
sb.Append(") !important; } </style>");
ltr.Text = sb.ToString();
this.Page.Header.Controls.Add(ltr);

Following is rendered style tag on my page header from this:

<style type="text/css" rel="stylesheet"> .m-quick-links ul li a:before { background-image: url(http://localhost:8082/assets/img/sprites/global-se8a7877705.png) !important; } </style>

How to change style of an element in code behind .net Core

The first way,you could judge the data in client side like below:

@model Test
<div id="MainDiv" class="flex justify-between items-center @(Model.IsDone=="Yes"?"bg-red-300":"bg-green-300")">

Controller:

public IActionResult Index()
{
var model = new Test()
{
IsDone=="Yes"
};
return View(model);
}

The second way,you could use ViewData to store the class and judge in the server side:

Index.cshtml:

@{
var styleClass = ViewData["Style"];
}

<div id="MainDiv" class="flex justify-between items-center @styleClass">
<a>Done</a>
</div>

Controller:

public IActionResult Index()
{
if(YourCondition)
{
ViewData["Style"] = "bg-red-300";
}
else
{
ViewData["Style"] = "bg-green-300";
}
return View();
}

How to set Style property in Code Behind for Specific DataRow

You can use DataTrigger to do this. In the example below if the State has value of State1, it will be red and if it is State2, it will be Green. You can bind it to another property of your datatable, another value and whatever color you prefer.

<DataGrid ItemsSource="{Binding YourItemsSource}">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding State}" Value="State1">
<Setter Property="Background" Value="Red"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding State}" Value="State2">
<Setter Property="Background" Value="Green"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>

WPF create style binding from code behind

Just create a Binding object with the same path:

Style myStyle = new Style(typeof(DataGridCell));
myStyle.Setters.Add(new Setter(DataGridCell.BackgroundProperty, new Binding("SelectedColour[0]")));
this.Resources.Add("MyStyle", myStyle);

Set button style in code behind

Be sure to use the WPF libraries and not the WindowsForms or GDI+ ones...

What you should use: System.Windows.Media.Brushes which contains DarkGreen as System.Windows.Media.SolidColorBrush (in PresentationCore.dll).

What you currently use is System.Drawing.Brushes and System.Drawing.SolidBrush.



Related Topics



Leave a reply



Submit