All about Javascript from server side

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?’);”/>


Advertisements
This entry was posted in ASP.NET and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s