Facebook
RSS

How to Use JavaScript With ASP.NET

-
Ricky


Because ASP.NET uses postback procedures when a user clicks a button, you can't simply insert Javascript functions into the web page's code. The Javascript must be declared in the code-behind in the application. Using C#, this example shows programmers how to create a confirmation window when users delete a record.

Instructions:
1) Create a button. A button is needed to link the Javascript code. The button is placed in between the page's form tags. For example, the code below places a new ASP button.

2)Insert the Javascript confirmation code in the Page_Load function. The Page_Load function is called at the beginning of every instance of the webpage. When the page is loaded in the user's browser, this function is called. Code that needs to be run during the web page's load process is placed in this function. Below is an example of Javascript loaded into the button created in Step 1. It creates a confirmation screen that verifies the user's deletion.
protected void Page_Load(object sender, EventArgs e){
btnDelete.Attributes.Add("onClick",
"return confirm('Do you want to delete this record?');");
}
3) Create Javascript functions. You may want to place a Javascript function on your page. In older web programming languages, the code is placed on the page and called when specified on the web page controls. However, with ASP.NET, the Javascript functions need to be registered before they will work. Here is an example of creating a Javascript function on an ASP.NET webpage.
using System.Text;
StringBuilder myString = new StringBuilder();
sb.Append("")
// Register the code with the ASP.NET page
Page.RegisterStartupScript("AlertScript", myString.ToString());
4) Test the code. Open the new webpage in your browser. If no syntax errors are present, the page will open and show a button with the text "Delete Record."
5)Click the new button. If the code was inserted properly, a confirmation screen should appear asking if you want to delete the record. If you hit "Cancel," it should cancel the operation. Clicking "OK" continues with the deletion process.





11 Responses so far.

  1. Anonymous says:

    Hi,

    Nice blog and it is simiar to your artice.
    Our Team of experienced Web Developers offer maintenance procedures, irrespective of the nature and the location factor of the PHP Portal Application Development to ensure smooth functioning of the activities within your organization and giving support projects.

  2. This comment has been removed by the author.
  3. Unknown says:

    Nice blog!

    Offshore software development services : http://www.bigscal.com/

  4. This comment has been removed by the author.
  5. nilashri says:

    I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favourites blog site list and will be checking back soon.

    Data Science course in kalyan nagar | Data Science course in OMR
    Data Science course in chennai | Data science course in velachery
    Data science course in jaya nagar | Data science training in tambaram

  6. Unknown says:

    Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information. 
    java training in omr | oracle training in chennai

    java training in annanagar | java training in chennai

  7. Unknown says:

    Appreciating the persistence you put into your blog and detailed information you provide
    python training Course in chennai | python training in Bangalore | Python training institute in kalyan nagar

  8. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
    Best PHP Training Institute in Chennai|PHP Course in chennai

    Best .Net Training Institute in Chennai

    Dotnet Training in Chennai
    Dotnet Training in Chennai
    Dotnet Training in Chennai

  9. varsha says:

    After reading this web site I am very satisfied simply because this site is providing comprehensive knowledge for you to audience.
    Thank you to the perform ..
    AWS training in chennai | AWS training in annanagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

Leave a Reply