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;
}
Related
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’m new to JavaScript. Basically I want a function in JavaScript which will show me an email preview in a web browser. I’ve stored email’s template, body and contents.
SQL:
SELECT Name, template, body, contents FROM Email
WHERE EmailID = 1
C#:
I have a LinkButton (ID="lnkViewDoc") on asp.net page and the code behind is:
lnkViewDoc.Attributes.Add("onclick", "return preview_email();");
JavaScript:
I need a function please which will pick values from the class fields and show it in a web browser. thanks
function preview_email() {
..................
window.open() //Something
}
Contents:
<!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>
<center>
..................
</center>
</body>
Body:
<div style="text-align: left"> Dear .......,
...........................................
</div>
<div style="text-align: left"> </div>
Take a look at the front-end server tags (bee stings). The ones I thing you're looking for are
<%=... %>
This is basically equivalent to Response.Write().
Suppose you have a .NET object named "email".
In code behind write:
lnkViewDoc.Attributes.Add("onclick", "return preview_email(" + email.EmailID + ");");
In javascript:
function preview_email(emailid) {
..................
window.open("previewEmail.aspx?emailid=" + emailid, ... more parameters)
}
If you're not afraid of using jquery in your javascript newbie days, you can try to use Jquery Dialog like this:
$("<div></div>").load("previewEmail.ascx?emailid=5").dialog({autoOpen:true});
or something similar,
you may want to consider just opening that same dialog with a iframe linking to the aspx, because your email will contain disturbing html elements like body tags
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 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.
How can I display an html textbox with value obtained from code behinh?
As long as your string value is not private, you should be able to do something like this
<input type="text" value="<%= YourStringValue %>" />
You could use an actual <asp:Textbox /> and set it's "text" value directly from the code behind. If you want to directly inject text into a "normal" html textbox (or anywhere else for that matter), you can use <%= SomeValue %>. Yet another way is to include the "runat=server" attribute on standard html elements, allowing you to manipulate them from the codebehind.
Normally I'd just go for the built-in ASP textbox control so I don't have to worry about persisting values/wiring up viewstate/etc. Injecting dynamic content into plain html elements tends to be an edge case requirement...
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//assign text value here
txt1.Value = "hello world!";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="txt1" runat="server" type="text" />
</div>
</form>
</body>
</html>