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.
Related
I have recently been working on Kentico sites, and the following issue has occurred recently, and never thought this won't work.
I have got a template on Kentico 8, and I want to display the content stored on 'ContentText' field of current document type.
So for this I am using the following code:
<cms:CMSDocumentValue runat="server" AttributeName="ContentText" FormattingString="{0}" />
which is working absolutely fine. However if I go and use a macro, as following:
<%# CurrentDocument.GetStringValue("ContentText", String.Empty) %>
it wouldn't pull the content stored on that field at all.
Do anyone know where I am going wrong? I am pretty sure the syntax is correct.
The reason why I want to use the macro is because I may be using this to check whether the value is null or not, so I can change the visibility of a placeholder.
Hope someone can help me on this.
Thank you.
Kentico macros (K#) are not getting resolved automatically in page template markup. Also, K# is not allowed in data-binding expressions (<%# ... %>). Generally, in page templates you can resolve macros using the following code:
<%# Import Namespace="CMS.MacroEngine" %>
<%= MacroContext.CurrentResolver.ResolveMacros("{% here comes your macro expression %}")%>
If I understand your question correctly you want to hide some asp:PlaceHolder control in the page according to value of current page field. This code might help you rather than invoking macro:
<%# Import Namespace="CMS.DocumentEngine" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
yourPlaceHolderControl.Visible = !String.IsNullOrEmpty(DocumentContext.CurrentDocument.GetStringValue("Intro", String.Empty));
}
</script>
I have a AjaxCall.aspx page that will be called another page by .ajax() using jquery.
At AjaxCall.aspx I had remove all the html tag and left only
<%# Page Language="C#" AutoEventWireup="true" CodeFile="AjaxCall.aspx.cs" Inherits="_AjaxCall" %>
and at the code behind it was a simple on the pageload event.
protected void Page_Load(object sender, EventArgs e)
{
Response.write("{ \"Testing\": \"Hello World!\" }");
}
I monitor the ajax request in Chrome developer tools in Network and I notice that every time I make a ajax request to AjaxCall.aspx page, it requested twice in a row. I later went into debug mode to check and discover that the pageload event fired twice.
I did some digging and found that an empty img src has something to do with this, but the problem is I have no any html tag in the AjaxCall.aspx page!
I later tried to add back the HTML body and form tag, and try to call that page again, and good news is it load once, but as soon as I add those html tag back to AjaxCall.aspx, it load twice again.
I can't have those html tag at my AjaxCall.aspx because it suppose to return a json format data when it is requested, having those html tag will cause error on whatever page that called it. At the same time I don't want it to continue load twice on every call I made. So is there anyway to overcome this problem?
If you want to deliver JSON response with application page (.aspx and code behind), you have to flush your response and close it in order to stop the process.
If you don't do that, the page .aspx will be return, may be with some HTML tag generating multiple request (like img with bad src)
Try this:
protected void Page_Load(object sender, EventArgs e)
{
Response.write("{ \"Testing\": \"Hello World!\" }");
Response.Flush();
Response.End();
}
Moreover, you can also use before your Response.write a Response.Clear in order to clear the optionnal content that could have been injected by any code, just to be sure to send only this content.
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.
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 need to understand that whether page is being called inside iframe or not at code behind. Is this possible?
I need to determine it in master page code behind.
asp.net 4.0, C#
In general, no.
Of course you can emit client script that detects iframe and reloads the page with e.g. a querystring.
It's not possible. However there's a workaround this. You can use querystring and check on page load if that querystring contains value, for example:
<iframe src="Default.aspx?iframe=true" />
In your Default.aspx.cs file:
protected void Page_Load(object sender, EventArgs e)
{
if(!string.IsNullOrEmpty(Request.QueryString["iframe"]))
{
if(Convert.ToBoolean(Request.QueryString["iframe"])
{
// this page is loaded in an iframe
}
}
}