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.
Related
I have some long code on server side that runs on Page_Load(). My problem is that users don't see nothing until this code is over
I want to show them some loading picture so that they will know that something is happening. I tried to put some JavaScript alert() on page but it didn't helped. I tried to changed html from server-side but of course it only shows the changes after job is done.
protected void Page_Load(object sender, EventArgs e)
{
// show user a loading image
// start heavy coding
}
You can use AJAX or UpdatePanel to separate loading and user-visualizing processes.
In this case you just need pageLoad() - js function, which will call proper, for example, web-service-method and shows loading image. OnSuccess\OnError events if ajax call will help you to handle the result of this call
i have a web page in c#, in the codebehind i generate a url, on the aspx page i want to update an iframe to show this url.
after looking a while for the means to do this, i found i can register javascript to force the refresh of the iframe, but now i am experiencing a trouble.
no matter what i try, the url seems to never change, remaining at the on load defined url.
let me show you some of the code so you can see what i am doing and maybe help me with thi issue.
i have this string, who handles the url i want to go
public String currentMap = "google.com";
this is the function who registers the javascript
protected void Page_Load(object sender, EventArgs e)
{
UtilityClass utility = new UtilityClass();
this.Page.ClientScript.RegisterStartupScript(
this.GetType(),
"StartupScript",
"Sys.Application.add_load(MyLoad);",
true);}
this registers the javascript function, this function is suppoused to load a modified url, like this
<script type="text/javascript">
function MyLoad(sender) {
$get('maps').innerHTML += "<%= currentMap %>";
}</script>
while i can see how i the inner html updates (you can see i use the =+ operator, because i wanted to see if i was adding something to the page), but the value for currentMap is always the same, and the iframe does not gets updated.
i launch a function, when i click an object in a gridview, this function does something like this.
currentMap = "<iframe src=\"somepages.html" id=\"viewerframe\" width=\"100%\" height=\"450\"></iframe>"\"";
i can see the iframe updating, but the value remains at http://google.com (a test value hard coded).
how can i update the div so it shows the correct url in the iframe?
thank you for the help
To update the src of an iframe you can use this code:
//Set upload frame source
var uploadUrl = '/Profile/upload.aspx?id=' + $.url().param('id').replace('/', '');
$('#uploadFrame').prop('src', uploadUrl);
I have encountered an unexpected behaviour and/or bug in the .net postback system.
I have a page that uses a master page to provide common elements, with form inputs split between the child and master pages. The form submit button is located on the master page, and I am attempting to process postback on the masterpage.
Any time I attempt to submit data where the form contains any non empty values and the url contains parameters, the page fails to process correctly. This does not occur if the page is submitted under either condition by itself.
The form postback method is post.
The page fails to load and in firefox returns the no element found error.
I have checked for correct class names ect and I do have empty attributes in non form elements, but as the page loads correctly at first I don't think that is relevant. I have also checked for infinately looping code.
This is the current postback handling code:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
save_page();
}
page_render();
}
//save
private void save_page()
{
dev_text.Text = "save in progress";
}
Setting text in an HTML element on the server will only be seen on the browser when the HTML is sent to the browser. Normally this happens once the entire processing of the page has completed... so normally quite some time after the user initiated the post-back.
Instead of setting the text on the server, consider setting the text directly on the browser at the moment of submission. Something like...
function setSavingText(){
// Vanilla javascript...
document.getElementById("<%=dev_text.ClientId%>").innerHTML = "save in progress";
// JQuery...
$("#<%=dev_text.ClientId%>").text("save in progress");
}
<asp:Button runat="server" ... OnClientClick="setSavingText();" />
The above function contains both a line for vanilla (normal) javascript, and one for the jQuery library. You only need one of them.
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
}
}
}
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.