How would I go about populating a database value to my html body background ?
Simply put, the HTML code is something like:
<body background="<%=session("userLogo")%>">
Update
Clarification of my question: Where in the code-behind, should I make the database call to populate the session("userLogo") value ?
Use this in your aspx:
body { background-image: url(<%= session("userLogo") %>); }
You can also put this in a css file (but then it has be an embedded resource and do remember to do performSubstitution = true)
Use <%= %>
<body background="<%= /* Code to retrieve value */ %>">
Related
i want to render css in page conditionally based on cookie. at my server side i detect cookie and store the cookie value in variable and now in aspx page i want to render css with the help of if else logic based on cookie value stored in variable at code behind.
suppose in my .cs code behind i store the cookie value like strCountryCookie="GB" and in my aspx page i am trying to render css using if else logic based on cookie value stored in variable.
so here is the way i am trying.
<%
if(strCountryCookie=="DE")
{ %>
#acloginpod {
width:380px;
background:#ebebeb url(../images/acloginpodbg.gif) repeat-x;
border:1px solid #d3d3d3;
-webkit-border-radius:7px;
-moz-border-radius:7px;
}
<% } else { %>
it is showing error. so i am not being able to figure out how to render it based on cookie value using if else logic. so please guide me with concept. thanks
Like others said, you can't use server-side code in CSS. What you did is almost correct, if you make sure the string is accessible from the code behind:
protected string strCountryCookie = "GB";
and then fix your statement
<head runat="server">
<title>Test</title>
<% if (strCountryCookie == "GB")
{ %>
<style type="text/css">
#acloginpod {
width:380px;
background:#ebebeb url(../images/acloginpodbg.gif) repeat-x;
border:1px solid #d3d3d3;
-webkit-border-radius:7px;
-moz-border-radius:7px;
}
</style>
<%} %>
Although this will get rather ugly quick... especially if you add a bunch of countries.
Another option is to put all the custom styles into its own style sheet and then dynamically load up the style sheet based on the cookie. You get the benefit of the style sheet being cached in this case:
<link id="_countryStyleSheet" rel="stylesheet" type="text/css" runat="server" />
And then load the style sheet in your code behind:
_countryStyleSheet.Href = String.Format("~/styles/{0}.css", strCountryCookie);
In this example, the style sheet would be named GB.css, etc.
You can't use any code in CSS files.
you can put your css into a separate file and then include it or not in the header based on your condition.
approach that I prefer in such cases is to include ALL css markup on each page, but rather assign my elements with different classes based on condition. For you that would mean to use a class .acloginpod instead of id and then assign element with this class only when country is 'DE'.
or if you want different styles for different countries then define element class as following:
">
and in your css define different classes like mydiv-DE or mydiv-GB
I have a user control defined like this
<%# Control .....
<Test:MyCustomControl id="xxx" runat="server>
</Test:MyCustomControl>
I would like to use this control on a page like
<Tag:MyControl runat="server">
<div>
my html
</div>
</Tag...
In my custom control codebehind I would like to read the inner html and set it to a property of Test:MyCustomcontrol
Currently I am getting an error saying that "...does not have property div"
How can I do this?
Note: For clarification the inner html can be an arbitrary html, so I need a way to read anything that user has typed in the page.
you can expose the div(running on the server) as a property from your UserControl
on the usercontrol html:
<div id="dvSomething" runat="server"></div>
on ur usercontrol codebehind ".cs file":
public System.Web.UI.HtmlControls.HtmlGenericControl TheDiv
{
get
{
return dvSomething;
}
set {
dvSomething = value;
}
}
on the page that contains the usercontrol:
WebUserControl11.TheDiv.InnerHtml = "addin something to div from page";
good luck
I will keep the other question open in case someone need a different solution:
I'm not sure about your requeriments like but here are two solutions, I hope this is what you want:
one adding control(you can add any by the way, not only textbox) and other pure html as per you described
TextBox txtAdd = new TextBox();
txtAdd.Style.Add("width", "200px");
WebUserControl11.Controls.Add(txtAdd);
TextBox txtRead = (TextBox)WebUserControl11.Controls[1];
((System.Web.UI.HtmlControls.HtmlGenericControl)WebUserControl11.Controls[0]).InnerHtml = "<b>something</b>";
string currentHtml = ((System.Web.UI.HtmlControls.HtmlGenericControl)WebUserControl11.Controls[0]).InnerHtml;
of course, the index will change depending how many elements you have on your user control
Hopefully this won't be a difficult question for someone to answer, but I am having a lot of trouble finding the solution online. I am trying to add some HTML to my asp.net page from the code behind (It's VB.net). I would like to add the HTML into the head section of my page but can only add to the body currently.
You can put code in the head, just like the body. For example:
<%= CallAMethodThatReturnsAStringOfHtml() %>
You could try creating a property in your code behind and add your html in the Page_Load method:
Public MyHtml As String
then in the head section of your HTML just use the literal notation:
<%= MyHtml %>
Have runat attribute on your head element and you will be able to access it
<head id="someHead" runat="server">
</head>
Now in your codebehind, you can set it like
someHead.InnerHtml="<script src='somelibrary.js' ></script>";
I made this way, and it worked:
on the .aspx file:
...
<%
Response.Write(GetDisclosureText());
%>
...
on the aspx.cs file:
protected string GetDisclosureText()
{
string disclosure = "";
// ...Apply custom logic ...
if (!string.IsNullOrEmpty(disclosure))
{
return disclosure;
}
return "Error getting Disclosure Text";
}
Note the only difference is that I call Response.Write, not just the function.
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.
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:
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.
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>";
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.
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.
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!
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;
This should be really easy but I can't figure out how to make it work...
I have an ASP.NET UserControl (.ascx) with the following property:
public string LabelCssClass
{
get
{
return _labelCssClass;
}
set
{
_labelCssClass = value;
}
}
I want to bind that property into the HTML of the UserControl at run time, using the <%# syntax. I imagine it must be something along these lines:
<td class="<%# Eval("LabelCssClass") %>" >
I've tried all different versions of Eval() and so on ... I'm not getting errors but the binding isn't working, and my breakpoints show that the property is not being accessed.
Whats the correct syntax? cheers
I think what you might want is this:
<td class="<%=LabelCssClass%>">
Kevin's answer is probably closer to what you are trying to achieve; however, you can successfully use the <%# %> syntax in the standard markup if you call DataBind() on the Page itself.