Tuesday, July 08, 2008

ASP: Inline scripting

At the top of a ASPX file, there will be a line which starts like this:
   <%@ Page Language="C#" ...
This tells you which language you can use for embedded scripting (i.e. functionality that is not in the code-behind file.
For example, the following embedded script is placed before the opening tag:
   <script runat="server">
      void change(object sender, EventArgs e)
      {
         TextBox1.Text = "Radjenovic";
      }
   </script>

The change() function can be then assigned to, e.g., the ontextchanged event handler for the TextBox1. If the AutoPostBack attribute is also set to True then the text in the TextBox1 will change to "Some other text" as soon as the textbox loses focus (i.e. the user clicks elsewhere, or presses the TAB key).

No comments: