Friday, June 6, 2008

JavaScript in Asp .Net

For effectively working with Ajax .Net you are expected to have knowledge of JavaScript.

Let's see how to work with JavaScript in Xml.

alert
First lets review JavaScript and Html Controls.
Start a new Asp .net website .
Go to Html tag in Toolbox.

Drag and drop and drop an html input (Button). Set appropriate values for ID and Value. Double click on the Button.

You will be taken to the script section of the page.



















Type the alert javascript command as shown in the highlighted portion in the figure and run the application.

Click the Button and you will receive an alert as shown in the figure.



















prompt and Memory VariableJavaScript doesn't have data types.
We are going to prompt the user to type their name and later the user will be greeted with the user name they typed.














Run the application and Click on the Button. You will receive a prompt as shown in the figure below.





















Changing the Document Back Color
function btnRed_onclick() {
document.bgColor = "Red";
}

JavaScript and Asp .Net server ControlsHaving seen working with Html controls lets switch our focus to Server Controls.

Add an Asp .Net Button control. Go the the the source view of the Asp .Net page.
Now lets add the script section manually and a function called hello.
For wiring up the event handler use OnClientClick as highlighted in the figure.



















The drawback with using Asp.Net Button is it will ultimately result in a post back.

Working with dates
function Button1_onclick() {
var d = new Date();
alert(d);
}

Confirmfunction Button1_onclick() {
var r=confirm("Press a button");
if (r==true)
{
document.write("You pressed OK!");
}
else
{
document.write("You pressed Cancel!");
}
}

Functions

One speciality of JavaScript functions is that no error is generated when caller's parameter list does  not
match.  All parameters available  through built in arguments[].

<script language="javascript" type="text/javascript">
        function Button1_onclick() {
            var sum = Add(7, 4);
            alert(sum);
            var sum1 = Add(7, 8, 5);
            alert(sum1);
        }

        function Add()
        {
        var sum = 0;
        for (var i = 0; i < arguments.length; i++) {
            sum += arguments[i];
        }
        return sum;
        }
    </script>



Object in JavaScript
var Blog = { name: "Shalvin", blog: "ShalvinPD.blogspot.com" };

Blog.Specialization = ".Net";
Blog.Location = "Kochi";

alert(Blog.name + "'s blog is " + Blog.blog +
" specializing in " +
Blog.Specialization + " located at " + Blog.Location);


Related Blogs
http://javascript.crockford.com/survey.html

1 comment:

  1. hai sir

    its amazing, pls blog lot of javascript examples. this is very useful to real asp programming. also blog popup control.

    madhu, dubai

    ReplyDelete