How do I call a Javascript function on the body load of a content page? As I can not use the body load of my master page's body onload in this.
If you're using ASP.NET Ajax, then you can add a load handler on your content page:
Sys.Application.add_load(contentPageLoadHandler);
function contentPageLoadHandler() {
// do stuff
}
Using this approach, every content page or even every control, can setup its own load handler. See this page for more information.
Alternatively, if you're using jQuery, you can achieve the same by using this approach (on your content page):
$(document).ready(function() {
// do stuff
});
See this page for more information.
You need to register a StartupScipt in your master page
Page.RegisterStartupScript("name", script);
It seems that you already handled the issue time ago, just to the googlers, in fact you can use the onload from your master page, the con' is that you'll have to put the same name of the javascript function you call on everypage, maybe empty in some cases...
Successful attempt by using window.onload:
window.onload=function(){
foo();
}
Related
I am generating HTML in the Page Load method in more than 1 page.
All those generated HTML are the same across all pages.
I found that it's a pain to have the same codes in every page because once I need to change something, I need to change them in all pages.
Example:
I have a div in each of those page:
<div id="Questions" runat="server"></div>
In the Page Load method of each page, I generate the same HTML.
Questions.InnerHTML = "<span>...etc...</span>";
So I decided to make a page that generates those contents, then load this page inside the div of the other pages, means, if I ever need to change, I only change this page.
I created a Handler, Questions.ashx. This handler generates that HTML and sends back a response.
Now to include it, I know I can use JQUERY's .load() function, but I would like to generate those HTML from server side.
What I've tried:
Questions.InnerHTML = LoadControl("~/Handlers/Questions.ashx").ToString();
But I received this error:
Type 'Questions' does not inherit from 'System.Web.UI.UserControl'.
"LoadControl" is for "User Controls", not HTTP Handlers..
You will probably be better off creating a User Control, which is an .ascx file. This can contain HTML, ASPX controls and code behind, and can be referenced by any ASPX page.
More Info here:
http://msdn.microsoft.com/en-us/library/y6wb1a0e(v=vs.100).aspx
In my web application I have a master aspx page in that i have a button click on that btn click i am calling a ascx web user control in that I am using a javascript through which I am trying to post the data in the same ascx, as I know we can post data in ascx page.. but I want to post how is that possible?
xhr.open('POST', '../test.ascx?Id=' + <%=Id%>, true); //Id is the property in ascx page
xhr.setRequestHeader('FILENAME', file.name);
xhr.send(file);
and onInit ..
if (!string.IsNullOrEmpty(Request.Headers["FileName"]))
{
string Name = Request.Headers["FileName"].ToString();
Stream Stream1 = Request.InputStream;
Add(Name, Stream1, Id);
}
You can't post directly to an ascx control. You should post to the aspx page that contains the control. The page will automatically create a tree that contains the controls you've statically declared on the page (in the markup). Dynamic controls require special care.
After that, usually you would parse the incoming query string or POST data in an early event handler, such as Page_Load(), and make whatever calls you need to against the indicated control.
You should try using some jquery plugin that will help you to give progress and handler other events while uploading file async.. a good article on it http://blogs.technet.com/b/sateesh-arveti/archive/2010/11/19/ajax-file-upload-using-jquery.aspx
while there are similar SO questions you should look at:
How can I upload files asynchronously?
Regards.
we can't post directly to an ascx control. so i used handler to solve this problem..
xhr.open('POST', '../Controls/test1.ashx?Id=' + '<%=Id%>', true);
I need to execute javascript before Page load in ASP.NET application.
My function returns user location, and I would like to pass value to server side and load data based on location.
The problem is javascript function executes after page load.
Here is my code:
$(function () {
getLocation();
});
in getLocation function I set hidden field value
$("#<%= HfLocation.ClientID %>").val(location);
in code behind I try to get value but it's always empty
protected void Page_Load(object sender, EventArgs e)
{
var location = HfLocation.Value;
}
I need to execute javascript before Page load in ASP.NET application
This requirements makes no sense. Remember how ASP.NET works:
A user request hits the web server
The web server dispatches the request to ASP.NET engine.
The ASP.NET engine instantiates the page and goes through the entire page lifecycle.
The page is rendered as HTML and is sent to the client
The client browser builds the DOM, runs client side javascript, ...
You see that it is impossible to have step 5 execute before step 3 (in which the Page_Load event executes).
I need to execute javascript before Page load in ASP.NET application.
In that case, you will need to make two page requests. You can't do it with a single page request, since Page_Load() runs before the HTML+JS is even sent to the client.
Instead, have a mini-page that makes the JS call and then either does a postback to the current page, or (probably better), loads a new page. You can pass the location data either through a regular (non-server) form, or perhaps by setting a cookie from JS on the client, or encode it into the query string.
The second page/request will then have the data you need when it's Page_Load() event fires.
Do an ajax callback using javascript to request the data.
I have a page that calls another page with some query string parameters. I want to return back to that page after clicking on a button.
I have to mention that I write that code in a user control and I don't know what page called that second page.
Is there something like Back button in browsers?
Simplest way use javascript on client side with
window.back();
For server side you need to save the url referer in page_load:
if(!Page.IsPostback)
{
ViewState["GoBackTo"] = Request.UrlReferrer;
}
and on a button click using Response.Redirect:
Response.Redirect( ViewState["GoBackTo"].ToString() );
edit: please note ppumkin's comment below!
You could look at Cross Page Posting.
Alternatively, if you are generating the link programatically you could include the returnUrl in the url e.g. http://localhost/secondpage.aspx?returnurl=firstpage.aspx
You can then read this querystring parameter in the secondpage and perform as redirect back once your work is done.
You can use the Request.UrlReferrer, but it is not necessarily sent from the client all the time:
Response.Redirect(Request.UrlReferrer.AbsoluteUri);
put this line of code on the page load event
Btn_Back.Attributes.Add("onClick", "javascript:history.back(); return false;");
I've got a master page that contains this code at the bottom of the page:
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(closeLoading);
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(parseData);
</script>
</form>
the parseData() function I'm creating inside the main page and I'm adding functions to it based on each page load. parseData IS called after every AJAX refresh, but it appears to only call the contents of the function from BEFORE the request. If I hit F5 to refresh the page again, it will properly call all of the newly added content in the parseData function.
Does this function cache that data? How can I make sure it calls the newly created contents of the parseData function?
According to the research I did, this is a problem with MS AJAX UpdatePanels and you are really better off not using them. I revised my JavaScript to pull data from hidden form fields instead so the JavaScript functions never changed and was able to get this working.