Set document background image in code - asp.net

I can use VS designer to open the document properties and change the Background property. But I can't seem to find a way to change it dynamically when the page loads. Can this be done in codebehind or this must be done in JavaScript only?

You can use the background and, more specifically, the background-image CSS properties.
http://www.w3schools.com/cssref/pr_background-image.asp
You can either generate the relevant HTML output on the server (using ASP.NET code) or in the browser, using JavaScript. Your HTML will look something like this:
...
<body style="background-image:url('...')">
...
</body>
...

You can try this on the markup:
<head runat="server">
<title></title>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</head>
<body class="bodyStyle">
And this in the codebehind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
String ImgUrl = "/Images/image.png";
Literal1.Text = "<style>.bodyStyle {background-image: url('" + ImgUrl + "');}</style>";
}
}
You can programmatically change the image url as needed

Related

How set CSS atyle attributes of controls via request.form["x"] method in aspx code behind?

I want set CSS style attributes of some controls (like input text) for example by Request.Form["x"] method in aspx page .cs code ,runat="server" is set for that controls and them is known in intellisense of .cs and them didn't find by form1.FindControl("x") (for some my reason that depend on jQuery issue) but i want set their CSS style in another syntax.
Thanks for any suggestion
Instead of using a client html control, use a server side control with a css class name. Then modify the definition of the CSS class in the code behind.
In your aspx:
<style type="text/css" runat="server" id="htmlCss"></style>
<asp:TextBox ID="txtField" runat="server" CssClass="myCssClass"></asp:TextBox>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
string css = #".myCssClass { background-color:yellow; }";
htmlCss.InnerHtml = css;
}
All the control needs to know is the styles are in "myCssClass". In Page Load you decide what the styles are going to be.

Change asp.net text without compiling the whole website

I have a webpage that I would like to change it copyright text.
The problem is I don't want to compile the whole website.
So in the page.aspx we have:
<div id="CopyrightDIV">
<span id="copyright" runat="server"></span>
</div>
The code behind sets the text on pageload.
How can I set the page.aspx so that it will show the text I set on it and not the text that is given by the page.cs?
Thanks
Since the final HTML will be rendered to the browser, we can always manipulate that, you can change the text in JavaScript like this:-
<script type="text/javascript">
$(function () {
$('#<%=copyright.ClientId%>').text("Ranch");
});
</script>
Considering you have imported JQuery library.
Ok, how about pasting a script block directly inline into the page? That way you can hook into the page lifecycle and change the span text at the last possible moment?
<script language="c#" runat="server">
public void Page_Render(object sender, EventArgs e)
{
((HtmlGenericControl)this.Page.FindControl("copyright")).InnerText = "Your text here";
}
</script>
Here's a rundown of the page lifecycle model. So long as the span was originally being set in the page load or init then you should be able to override this at a latter stage.
http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.85%29.aspx

How to use a variable(string) in img src tag?

I have a code like this:
string x="www.abc.com/1.jpg";
I should use that string x in img src tag...
I am writing this code in source file of aspx page.
Here is the tag you need to write
<img src=<%=x%> />
I got the above (html) example to work:
<img src=<%=url% /</div
when I had the following script entry in the aspx page:
<script runat="server"
public string url;
protected void Page_Load(object sender, EventArgs e)
{
url = "images/small/image1.png";
}
</script
I am not able to have the aspx:image work though:
<asp:Image ID="Image1" runat="server" Height="210px" Width="252px" ImageUrl="<%=url%" />
I have tried variations of the variable within ImageUrl but it keeps coming up blank or gives an error.
It obviously needs needs a different variable construct.
Let me know who has figured this approach out

add stuff to Master page's <html> tags from child page in asp.net?

I am trying to add xfbml code in one of my child page and i realize that i have to add this line:
xmlns:fb="http://www.facebook.com/2008/fbml"
to HTML section of the page like this:
<html xmlns:fb="http://www.facebook.com/2008/fbml">
But I dont want to add it to the master page because i am using like button on only ONE child page.. is there a way I can tell asp.net to add this line when that particular child page loads?
thanks.
Yes, just do the following:
First add a public property to your master page:
public partial class Master : System.Web.UI.MasterPage
{
public bool FacebookHtml;
Then put an inline check in your Master's aspx:
<html <%= FacebookHtml ? "http://www.facebook.com/2008/fbml" : "xmlns='http://www.w3.org/1999/xhtml'" %>>
Then in your content page just set it to true (remember to include the MasterType as well in your content page):
protected void Page_Load(object sender, EventArgs e)
{
Master.FacebookHtml = true;
}
On the front end, do something like this:
<html xmlns="http://www.w3.org/1999/xhtml" <%= FBNamespace() %>>
In the backend on the masterpage, make a public property:
Private _fbNamespace As String = ""
Public Property FBNameSpace() As String
Get
Return _fbNamespace
End Get
Set(ByVal value As String)
_fbNamespace = value
End Set
End Property
And finally in the child page:
CType(Page.Master, MasterPage).FBNameSpace = "xmlns:fb=""http://www.facebook.com/2008/fbml"""
One approach could be to make your HTML tag a server tag.
<head id="headtag" runat="server">
Your child page could then execute the code:
var headtag = Master.FindControl("headtag") as HtmlGenericControl;
headtag.Attributes["xmlns:fb"] = "http://www.facebook.com/2008/fbml";
The only side effect I can see in the markup is that you wind up with the extra id attribute in the rendered HTML; however, I suspect that won't harm anybody.

Start javascript from asp.net page

Hi I have a usercontrol which includes some JavaScript, if I add the control to a standard web page I can start the JavaScript in the body tag, like this
<body onLoad="Start()">
The problem is that I need to add the control to a webpage which is inside a masterpage, how do I then start the script when a page inside a masterpage doesn't have a body tag.
You can register it using:
Page.ClientScript.RegisterStartupScript()
For your case, it would be this:
Page.ClientScript.RegisterStartupScript(GetType(), "MyScript", "Start()", true);
Include jQuery and put your initialization code inside $(document).ready().
If you want to strictly use .NET functionality, and your page is properly marked up with runat="server", you could use Page.ClientScript.RegisterStartupScript() as well.
You can change the body tag to runat server and give it an id.
<body id="masterBody" runat="server">
Then on page load:
public void Page_Load(Object sender, EventArgs e)
{
HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("masterBody");
body.Attributes.Add("onload", "Start()");
}
Try binding to the DOM document.ready event to load your content after the document has completed rendering

Resources