Thursday 20 March 2014

How registerstartupscript works in asp.net?

It adds a script block into the page at the end of page i.e. just before the form closing tag. It is having advantage over other methods where if the script requires any elements to be available on the page then as the script is already loaded/rendered, the elements can be referenced and script will not throw any Javscript error.
There are two overloaded methods:

RegisterStartupScript(Type type,string key,string script) RegisterStartupScript(Type type,string key,string script, bool addScriptTags)

We can check if it is already been added somewhere else in our code, IsStartupScriptRegistered
How to use it.


ClientScriptManager csManager = Page.ClientScript;
  Type vartype = this.GetType();
  if (!csManager.IsStartupScriptRegistered (vartype ,"buttonSubmit"))
  {
   StringBuilder msgText = new StringBuilder();
   msgText.Append((" ")); //Createing the string message
   csManager.RegisterStartupScript(vartype, "buttonSubmit", msgText.ToString());
  }

No comments:

Post a Comment