I know you can't use asp net server tags in an external javascript file. This is a bit of pain, because it forces you to declare your variables that need ClientID in the aspx page and then you refer to them in the external javascript file. Not very clean. Currently I use script manager's composite script to register my scripts... It would be nice if I could have the script injected and the server tags processed as if it was part of the page. Is this possible?
I know there is RegisterClientScript but this doesn't seem to honor the script tags either. I'm wondering if there is a solution someone has come up with to just pull the contents of the javascript file and shove them into the aspx page before it's processed so that the server tags can be processed. I've looked all over the web and don't see any good solution to this beyond using the server tags in the aspx page or generating the ids of controls, etc. server side and generating script.
I know you can't use asp net server tags in an external javascript
file
You can create an ASPX page to generate dynamic javascript
<%# Page Language="C#" AutoEventWireup="false"
CodeFile="script.aspx.cs" Inherits="scripts_script"
EnableViewState="false" StyleSheetTheme="" %>
function test() {
testinfo.innerHTML = "<%= MyVariable %>";
}
Make sure to set StyleSheetTheme="" otherwise the runtime will insert a <head> which you don't want
And in the code behind set the ContentType to application/x-javascript
using System;
public partial class scripts_script
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.Response.ContentType = "application/x-javascript";
}
}
Now you can use this ASPX page as if it were a .js file.
Related
I have an aspx page that needs to be an embedded resource for a WCF project. An interfacing service serves the aspx/html/js files to the web browser via a Stream object. The problem I am having is getting the aspx page to display properly. I have tried setting the content type to "text/html", but when I do this, I get the following at the top of the page:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="Services.Interface.Views.WebForm" %>
Which is the first line of Index.aspx.
Since it's being served as an html file, the aspx "stuff" is ignored, I guess.
I've also tried setting the content type to "application/xhtml+xm" as some have suggested, but I get an error that says:
This page contains the following errors:
error on line 1 at column 2: StartTag: invalid element name
Below is a rendering of the page up to the first error.
I have tried taking out the ASP stuff from Index.aspx and changing it to an HTML file, and it works, but I need the ASP capabilities for accessing some settings in Web.config.
Is there a proper content type for aspx pages that I'm not aware of, or is something else going wrong? Thanks in advance
Is it possible to add <!DOCTYPE HTML> to an aspx page from the code behind file of a ascx page?
Adding doctype to the master page is not an option because it'll wreck the rest of our sharepoint sites.
I've tried to override the render method:
protected override void Render(HtmlTextWriter writer)
{
StringBuilder sb = new StringBuilder("<!DOCTYPE HTML>");
HtmlTextWriter textWriter = new HtmlTextWriter(new System.IO.StringWriter(sb));
//base.Render(writer);
base.Render(textWriter);
writer.Write(sb.ToString());
}
but apparently it doesn't help.
For me it worked this way:
First I added a literal on top of the page, first line, outside the <Form runat="server":
<asp:Literal runat="server" ID="litHTMLSchema"></asp:Literal>
Then from code-behind:
// HTML 5
litHTMLSchema.Text = #"<!DOCTYPE html>" + Environment.NewLine + #"<html>";
I don't think this is the best approach, but it works without any issues.
Since you are using sharepoint, you could create a custom webcontrol in code in a WSP Package Farm Solution.
Create a class Called DynamicDocTypeControl
public class DynamicDocTypeControl : System.Web.UI.WebControl *(check namespace for typos)
{
override Render(...) {
//add some conditional logic here for your dynamicness...
writer.Write("<!DOCTYPE HTML>");
}
}
Add an empty sharepoint element to your project and go to the properties window and use the safe control section in the property window to register your control as a safe control.
Build/Package the wsp and deploy it to the farm.
Then edit your master page in sharepoint designer and drop your control on it where the doctype should be rendered.
Putting it in the master page won't wreck your sites because you can make your render logic not render anything if it's not on an allowed page.
Just have some code on your aspx page that sets an HttpContext.Current.Item... value that the doctype control looks for to determine if it should render. As long as your aspx page sets the config flag before Render is called it will be there when render fires on the doc type control.
e.g.
<# Register TagPrefix="XYZ" Namespace="XYZ.Controls" Assembly="XYZ... (include fully qualified assembly name)" />
<XYZ:DynamicDocTypeControl />
I am using asp.net file upload
<asp:FileUpload ID="ImageUploader" runat="server"/>
how can i detect that asp:FileUpload has a file using Jquery?
I am doing this
$("#ctl00_MainContentPlaceHolder_UCUpdOrgProfile1_ImageUploader").change(function (e) {
alert("hello")
});
But i dont know either file is selected or not.
check -
if (document.getElementById('<%= ImageUploader.ClientID %>').files.length === 0)
{
// File upload do not have file
}
else {
// File upload has file
}
Valid answers, or since ASP 4+ just set ClientIDMode as a page property:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" ClientIDMode="Static" %>
The ClientID value is set to the value of the ID property. If the control is a naming container, the control is used as the top of the hierarchy of naming containers for any controls that it contains.
- reference
ie. this will force ASP to obey the ID as declared and not generate the "ctl00_" prefix or any other.
This is awesome as it is applied to the whole page and all asp controls contained on it, no extra code, no hacks just one awesome property.
So apply the same logic as #Microsoft_DN solution, but use static ID's.
So I'm trying to use C# to grab stored HTML in a database and then display it between a set HTML header and footer (I have a couple of these for different pages, so a masterpage would make life more complicated rather than less. I also come from a PHP background where this is sort of the way things work).
<!--#include virtual="header.html" -->
<% #Page Language="C#" Debug="true" %>
<% #Import Namespace="System.Data.Odbc" %>
<% #Import Namespace="System.Web.Configuration" %>
<script language="C#" runat="server">
void Page_Load(object sender,EventArgs e) {
//Gets the HTML Successfully
//Response.Write(fetched_html);
}
</script>
<!--#include virtual="footer.html" -->
Unfortunately with the current workflow the code generates content before the header or footer are put on the page, putting all the stored HTML at the top of the page before the header. I've tried a couple different Page_Events and none of them seem to work, so I need to know what to do.
Is there an equally simple way to reference and insert a simple html header file that would render before any of the Page_Events? I've tried Page_PreLoad and others to no success. Either the code is not fetched at all or I run into the same issue.
Use a MasterPage with a ContentPlaceholder. Then, on the content page, use a Literal and put the HTML in there.
See "The difference between literal and label".
Try this:
Assuming you have data holding some where in memory like datatable or dataset or collection.
To display a html text on the page we generally use <asp:Literal/>. So place a asp literal control on the page. It works in between <body> </body> tag.
Now render the text to the control using its Text property. e.g.
ltl.text = dt.rows[0]["htmlcol"].tostring();
To dynamically add text inside <head> </head>, u need HtmlMeta,Page etc class.
I have access to a server with aspx pages. I need to add a title, parapgraphs, etc to a page. The page currently only has the following line:
<%# Page Language="C#" AutoEventWireup="true" Inherits="Access.Login" %>
I do not have access to the CS files, just the DLL. Anyway, when I try to add any html to the document nothing changes. I am able to change the CSS, and if I remove the "inherits" then whatever HTML I have gets displayed, but when the "inherits" is there only the default page gets displayed and none of my additions.
Admittedly I am new to ASP and moreover I am not trying to become a guru just to add some HTML to a page, but any advice would be great, thanks!
Try putting your Page_Load embedded in the .aspx and add controls that way:
<%# Page Language="C#" AutoEventWireup="true" Inherits="Access.Login" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
Controls.Add(whatever);
}
}
</script>
<!-- Try this if the above does not work -->
<script runat="server">
new protected void Page_Load(object sender, EventArgs e) {
base.Page_Load(sender, e);
if (!Page.IsPostBack) {
Controls.Add(whatever);
}
}
</script>
Fundamentally, I'm afraid this is not possible. .NET is a single-inheritance language/framework. So when it says Inherits="Access.Login" that means you can only have it use Access.Login OR your code-behind, but not both.
That said, you could jump through some crazy hoops to accomplish your goal. Like create a brand new "wrapper" page, then in the code-behind fire off an http request to the page you want. Load the response, which will just be a really long string into a 3rd-party DOM parser, or if you're confident you're getting 100% valid XML back, use .NET's built-in XmlDocument or XDocument to parse the page, find your html elements, make your changes, then do a Response.Write with your modified content.
And that's a real-life example of going around your elbow to get to your...
I am not 100% certain this will work, but you could have a code-behind file inherit from Access.Login and use the new (override will not work if Page_Load isn't marked as virtual) keyword with Page_Load. Then you could use Inherits="YourAssembly.NewLogin".
The part I am not sure about is whether or not asp.net uses the page class or your subclass to call the Page_Load method. If page_Load was virtual, it wouldn't matter, but since it isn't the new will only be called if the page is cast into your subclass. It is worth a try though.