All about Javascript from server side

March 10, 2009

These are just my notes on Javascript:

  • When AJAX is involved, use ScriptManager.RegisterStartupScript, instead of Page.ClientScript.RegisterStartupScript.
  • The specialty of RegisterStartupScript (compared to RegisterClientScriptBlock) is that it places the code at the very end of the page (before the </form> tag.
  • To check if we have already inserted a block of Javascript:
    if (!Page.ClientScript.IsStartupScriptRegistered(“myCode”))

    {
    Page.ClientScript.RegisterStartupScript(this.GetType(),
    “myCode”, script);
    }
  • Add a js include file:
    Page.ClientScript.RegisterClientScriptInclude(“myscript”, ResolveUrl(“ScriptsMyScript.js”))
  • Insert client-side form validation:
    form1.Attributes.Add(“onsubmit”, “return checkBeforeSubmit();”);
    You can also do onClientClick=”return checkBeforeSubmit();” Returning false will cancel the submission. A very basic one:

    <asp:Button ID=”btnDelete” runat=”server” Text=”Delete” OnClientClick=”return confirm(‘Are you sure?’);”/>



Follow

Get every new post delivered to your Inbox.