Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fire Javascript function OnLoad but not postbacks... 1

Status
Not open for further replies.

checkai

Programmer
Jan 17, 2003
1,629
US
I have a javascript function that grabs values from it's parent page and puts them in text boxes...I need that code to only run onload but not on any postbacks...right now it runs on all postbacks because the function call is in the body onload event....

dlc
 
You could still call it in the body onload and have it check a hidden field that has it's value set on the first postback. If the field is set the script does nothing.
Marty
 
This works for me. Instead of using the JavaScript "onload" in the body tag, register the script server-side, only if the page is not loading via PostBack:

Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace startupscript
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Button Button1;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if (!IsPostBack)
			{
				Page.RegisterStartupScript("startup",@"<script>alert('I run once.')</script>");
			}
		}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void Button1_Click(object sender, System.EventArgs e)
		{
			// submit button for the form.
		}
	}
}

If this post was helpful, consider clicking some ads. My personal site is also advertiser supported.



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
don't know why i didn't think of that...all my ideas were very complex and unnecessary...

thanks...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top