Technology Programming

How to Use Javascript With ASP.NET

    • 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.
      <form runat="server">
      <asp:button runat="server" Text="Delete Record" />
      </form>

    • 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("<script language='javascript'>");
      sb.Append("document.getElementById('");
      sb.Append(btnId);
      sb.Append("); alert(' You opened a webpage!'); ");
      sb.Append("</script>")
      // 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.

Related posts "Technology : Programming"

The Importance Of Having a WordPress Business Theme

Programming

Website Design Is Necessary For Your Website

Programming

The Most effective On line Paid Survey Evaluation

Programming

Adelaide SEO - Links And Keywords, How Should They Be Used

Programming

C Programming Compilers for Microcontrollers

Programming

How Should A DJ Make Music Logo That Is Distinct And Cool?

Programming

Call to Action Concepts for Small Businesses

Programming

Microsoft Access Databases in Office 365

Programming

Why web design is crucial for producing world class websites

Programming

Leave a Comment