I have done enough research and have decided to move the JavaScript to the bottom of the page.
Here is why i should do it http://developer.yahoo.com/performance/rules.html#js_bottom
I wrote the code below, this code only can add the JavaScript within the header.
How do I add the JavaScript before the </body> of the page? I am thinking about using
The code below will adding the JavaScript to the header not the body.
public static string AddJavascript(string filename, Page ThisPage)
{
HtmlGenericControl js = new HtmlGenericControl("script");
js.Attributes["type"] = "text/javascript";
js.Attributes["src"] = filename;
ThisPage.Header.Controls.Add(js);
//Add to bottom of the page.
return null;
}
protected void Page_Load(object sender, EventArgs e)
{
AddJavascript("myjd.js", Page);
}
I like to produce something like below this dynamically. I have several JavaScript files so i really like to keep them separate.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
</title>
</head>
<body>
<div>
my content here
</div>
<!---javascript should go below here-->
<script type="text/javascript" src="myjd.js">
</script>
</body>
</html>
Use ThisPage.ClientScript.RegisterStartupScript(this.GetType(), "myjd", "[script]", true) to register it at the end (just before closing of form tag).
You could create a placeholder at the bottom of your page and then use you code to add the js to the placeholder.
Related
I am using the OnClick of the asp:Button tag to compute a report which I would like to display as an innerHTML of a div tag. I would like to use the Response.WriteLine method to return this html file. Does ASP.NET webforms provide a way of capturing this response?
Am I forced to use JavaScript instead? I am using C# on the Server side.
The Web Forms way of doing this is to use a control and set its InnerHTML from the code behind. Here's a self contained example:
<%#Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected void GenerateReport(object sender, EventArgs e)
{
var reportHTML = "<p>This is a <span style='font-weight: bold;'>report!</span></p>";
ReportDiv.InnerHtml = reportHTML;
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
</head>
<body>
<form runat="server">
<asp:Button runat="server" OnClick="GenerateReport" Text="Generate Report" />
<div runat="server" id="ReportDiv"></div>
</form>
</body>
</html>
I like to to ask that is there a way to load an ascx control from string? To be more precise: I like to make/store a new .ascx in sql then load it to the website.
Yes, you should be able to use the ParseControl function to do what you want:
http://msdn.microsoft.com/en-us/library/kz3ffe28.aspx
Example from MSDN:
<%# Page language="c#" Debug="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ASP.NET Example</title>
<script runat="server">
// System.Web.UI.TemplateControl.ParserControl;
// The following example demonstrates the method 'ParseControl' of class TemplateControl.
// Since TemplateControl is abstract, this sample has been written using 'Page' class which derives from
// 'TemplateControl' class.
// A button object is created by passing a string to contstruct a button using ASP syntax, to the
// 'ParseControl' method. This button is added as one of the child controls of the page and displayed.
void Page_Load(object sender, System.EventArgs e)
{
Control c = ParseControl("<asp:button text='Click here!' runat='server' />");
myPlaceholder.Controls.Add(c);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:placeholder id ="myPlaceholder" runat="server" />
</form>
</body>
</html>
I have a dnn module, and I have a requirement where I need to inject some HTML elements in the body tag of the page.
I tried:
HtmlGenericControl divControl = new HtmlGenericControl("DIV");
divControl.ID = "divControl";
this.Page.Controls.AddAt(0, divControl);
the code above cause the HTML to look like this:
<div id="xxxxxx_divControl"></div><!DOCTYPE html>
<html lang="en-US">
<head id="Head">...
There is no body property on the Page, so is there a way to make the div render in the body tag? (like shown below)
<!DOCTYPE html>
<html lang="en-US">
<head id="Head">...
</head>
<body>
<div id="xxxxxx_divControl"></div>
....
</body>
</html>
You don't want to add it to the Page controls, but to the controls of a container within the page. Use a PlaceHolder for this. Just add the PlaceHolder control to your page where you want to inject content:
<asp:PlaceHolder runat="server" ID="somePlaceHolder" />
Then you would reference that in your code-behind:
HtmlGenericControl divControl = new HtmlGenericControl("DIV");
divControl.ID = "divControl";
this.somePlaceHolder.Controls.AddAt(0, divControl);
You can add runat="server" and an ID to the body tag. Then add your control to the body's control collection. But, depending on your Page Lifecycle, it may not end up where you want it to be, so a Placeholder is still the preferred way.
Easy question perhaps.
Okay, I have a post to my page and need to respond with one string.
in php, you could simply do something like this:
<?php
die ("test");
then you can place this page on a webserver and access it like this:
localhost/test.php
so, I need to do exact same thing in c#.
When I try to respond with:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("test");
Response.End();
}
I'm getting: "<html><head><style type="text/css"></style></head><body>test</body></html>"
as a response.
How can I make asp.net to just return exact response, without html?
I know that I probably missing some basic knowledge, but cannot find anything online.
You can clear the previous response buffer and write your new output.
Response.Clear(); // clear response buffer
Response.Write("test"); // write your new text
Response.End(); // end the response so it is sent to the client
Make sure in your *.aspx file, at the top you have AutoEventWireup="true", if it's false (or not there?) your Page_Load event handler will not be called.
Also, make sure you compiled your page.
Another suggestion is to use a Generic Handler (ie *.ashx), these do not use the typical webforms lifecycle and might be better suited to what you're doing.
I think you're looking for :
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/plain";
Response.Write("test");
Response.End();
}
For me it only Generates actual text in response.write(); statement. I am uploading the complete code for clarity.
Visual Studio: 2010
Code Behind:
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("I CAN ONLY SEE THIS NO OTHER HTML TAG IS INCLUDED");
Response.End();
}
}
HTML CODE
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
OUTPUT & HTML Source:
I CAN ONLY SEE THIS NO OTHER HTML TAG IS INCLUDED
I am getting the desired result. I have tried this code with Master-Page also i get the same result.
Please make sure your AutoEventWireup="true" if i turn this false then HTML SOURCE changes to this
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title></head>
<body>
<form method="post" action="Default2.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZGivF0fgbeE6VebNR51MYSu3yJdsZ9DwEtIPDBVRf4Vy" />
</div>
<div>
</div>
</form>
</body>
</html>
As the previous answer suggests above, you need AutoEventWireup="true" as well as Response.End() in code behind.
I would like to add two additional xml namespaces to the <HTML> element in asp.net:
take:
<html xmlns="http://www.w3.org/1999/xhtml" >
to make (adding facebook open graph namespaces):
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:og="http://ogp.me/ns#"
xmlns:fb="http://www.facebook.com/2008/fbml">
How would I access the <HTML> element in code behind and add a namespace?
You can do it just like any other element. In your aspx just mark the html tag to be runat server:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" id="html_tag" runat="server">
And in your code just add the attributes:
protected void Page_Load(object sender, EventArgs e)
{
html_tag.Attributes.Add("xmlns:og","http://ogp.me/ns#");
html_tag.Attributes.Add("xmlns:fb", "http://www.facebook.com/2008/fbmls");
}
This of course does not have to be done via code and can just be placed in your aspx unless you wanted to only include those attributes in certain conditions.
You can do something like as below:
<html <%= GetTags() %> >
The GetTags function will be defined in your code behind file and should return a string which will be placed in html tag, so you can return the "tags" as string and it will appear in HTML tag.
But I am not getting your point of doing this from code behind. Why not just do it in the aspx itself?
I hope it is helpful. How to set "enabled/disabled" for a button:
<button type="button" class="btn btn-primary btn-md remove-button" <%=GetDisabledGtmlAttribute(IsUIDisabled) %>>>Remove</button>
C#:
protected string GetDisabledGtmlAttribute(bool isUIDisabled)
{
if (isUIDisabled)
{
return "disabled";
}
return string.Empty;
}