1

I have a specific control gridview , i apply specific CSS file on it , i wanna to change this CSS under specific condition in my .cs file, is there away to do that?

for example :

<ItemStyle CssClass ="normal"/>

i want to change this in .cs under specific condition.

1
  • Do you want to change the ItemStyle Property for a specific item, like conditional formatting, or ItemStyle of a whole Column object? Commented Mar 2, 2011 at 14:17

7 Answers 7

1

Do you mean GridView?

It doesn't have and ItemStyle Property. Columns within it do though.

So you can use:

gv.Columns[0].ItemStyle.CssClass = "RedItem";

DataGrid does have an ItemStyle property:

dg.ItemStyle.CssClass = "MoreThanNormalClass";

Its not clear from your question what you want to change...

Example,

CSS Classes:

<style>
    .Normal{ background-color:Lime; }
    .Warning{ background-color:Red; }
</style>

ASP.NET markup:

<asp:DataGrid runat="server" id="dg" onitemdatabound="dg_ItemDataBound" >
    <ItemStyle CssClass="Normal" />
</asp:DataGrid>

C# code behind :

protected void Page_Load(object sender, EventArgs e)
{
    int[] someInts = { 1, 15, 20 };
    dg.DataSource = someInts;
    dg.DataBind();
}

protected void dg_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    if (e.Item.DataItem != null)
    { 
        int v = (int)e.Item.DataItem ;
    
        if (v > 10 && v < 20)
        e.Item.CssClass = "Warning";
    }       
}

Outputs:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

iam not use gridview , i want to give an example, if i Set the ItemStyle of DataGrid for example in .aspx with CssClass value comes from .Css file , i ask if i can under specific condition to change this value in .cs file this what i ask about , if not possible , is there any alternative solution?
1

Your styles doesn't have to be in a .css file: You can change the file to an .aspx or maybe create an HttpHandler to serve up your CSS.

Comments

1

You have to get something like this rendered to the HTML page.

<style type="text/css">
  .itemstyle
  {
    /* whatever styles you need */
  }
</style>

This can override whatever you have in your CSS file. One option is to put a Literal control on the ASPX page:

<asp:Literal ID="litStyle" runat="server" />

and use that to write out the necessary styles from code behind:

litStyle.Text = "<style type=\"text/css\">.itemstyle{" + myStyles + "}</style>";

5 Comments

and what is the relationship between the literal control and the gridview item style??
misunderstood the question, and it's been edited since i answered. :/
i mean what is the role of the literal control here, i mean i want to change the ItemStyle for the gridview.
a literal control writes out whatever text you give it directly to the rendered HTML. you can give it text, HTML or whatever - it will write it in the place where you put the literal control.
the literal control won't help you change the class that's applied to the ItemStyle-CssClass on the gridview, but it will help you override a particular style on a css class that's always applied to the ItemStyle. that's the only way it could help you with your problem.
1

You can inject a <style> tag with the necessary adjustments to it onto the page without modifying the CSS file.

Just create a literal control, acting as placeholder for the modified styles, on the ASPX page. From the code-behind, render something similar to the following to the literal:

Literal1.Text = "<style type=\"text/css\">.normal { background-color:red; }</style>";

...with "normal" being the original CSS class that you want to modify. The beauty of CSS is that it would first apply styles set in the included files, then any "overwritten" styles explicitly specified on the page in <style> tags.

2 Comments

please how to do that , i mean for example , i have itemstyle , i wanna to change it in .cs file under specific condition
I added an example to my original answer.
1

Dynamic CSS is generally frowned upon.

You should define two ( or more) classes within your CSS and then set the ClassName property of the element in a PreRender event( or client side) script as needed.

1 Comment

I'd suggest you to double-check the question. It's not about that.. Author is requesting for a way of including CSS files depending on some control. How does this solve the problem? I belive he already did that, but, what about the CSS stylesheet file having the whole CSS classes? Where do you include then? Are you including them in any page even if it doesn't need it?
1

Well, controls aren't responsible of managing CSS source files and its addition to the page, so, the easy answer is no, you can't do that.

By the way, there's some solution for doing that.

You can include a CSS file with style HTML element from your control by adding a server control (HtmlGenericControl, for example), with the apropiate attributes and values, so, if container control requires some specific CSS file, you can add it during container control's life cycle, just before rendering it, to HTML head element (marked with runat="server" attribute) of some ASP.NET page.

Maybe a good way of doing that should be creating a configuration section in your web.config implementing your own one which may support creating dependencies of controls/pages and CSS stylesheet files, so, using this approach, you would be able to implement some method in a derived from System.Web.UI.Page class that may add CSS files depending on controls:

<cssDependencies>
   <control type="YourNamespace.YourControl" cssFile="~/Styles/Default/YourControlStyle.css" />
<cssDependencies>

And then, your CustomPageBase would have its own "AddControl" method which should register its type in some collection that may be iterated in the PreRender method, so, there you can add CSS files based on control's types.

I'm just giving you ideas! :)


EDIT & NOTE: Anyway, this approach, and your goal, could have problems in terms of performance optimization.

Best optimized sites should combine all needed CSS into one, so browser should load one instead of many during page renderization.

I believe combining all CSS files into one can be achieved with "CSS files and control types approach", and I would suggest you to go this way, because if you don't do that, you can end with pages having dozens of style elements.

Have you heard about DotLess project? Check it out here: http://www.dotlesscss.org/ Maybe it can give you a better approach with less effort!

2 Comments

this seems to me to be a hell of a lot of trouble to go to...config sections + overriding Page class.....
But maybe question's author has another needs that may force him to use this approach instead of yours, which is, right, better in some scenarios.
1

The best way would be to have different classes set up in your static css-file and change which of these classes your gridview uses from codebehind (.cs). This way you will still get the benefit of the css being cached and well separated from your view (.aspx).

css:

.normal { background-color:white; }
.alternate { background-color:#EEE; }

codebehind

var css = SomeLogic() ? "normal" : "alternate";
gridView.RowStyle.CssClass = css;

1 Comment

can u clarify the idea with example please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.