开发者

How to pass parameter in CSS and then how to apply if..else on that parameter in ASP.NET?

i have a grid with an image column, i want to change images based on data source. if data 开发者_如何学JAVAfield for a row is 2 then image will be *.gif and if it is 3 then will be different so it is not fixed that which type of images we will have so i want to do "if..else" on css because in that case we can easily change images on css. i don't want code on code behind file.


CSS does not have any programming constructs such as constants or if / else. There are however several extension to CSS that provide this:

  • xCSS it can nest child elements.

  • Then there's LESS, it's written in Ruby so you need that installed (as far as I understand, I haven't used it myself yet) but it's perfectly suitable for editing CSS in any environment. Check the "nested rules" section on the front page.

A simpler solution to your problem is to keep your CSS static, but change the CSS class of your element based on your condition.

See the following page:

http://www.w3schools.com/ASPNET/prop_webcontrol_style_cssclass.asp

e.g. the following button has its style set via the CssClass property:

<form runat="server">
  <asp:Button id="Button" CssClass="TestStyle" Text="Submit" runat="server"/>
</form> 

You can of course set this in code-behind based on some logic.


css doesn't support parameters. You can do some level of parameterisation with "less" (a dialect of css that requires pre-processing), but even then the question is vague. Other options would include:

  • coding the image directly per element
  • using something in the structure to choose, i.e. you could have a css class on something higher and use the css with patterns like .somethingHigher .theThing {...}

but without a more specific example, we're shooting pretty blind.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜