Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Tuesday, October 20, 2015

AngularJS for .Net Developers Part 1

AngularJS is a popular client side MVC and MVVM Javascript library used for creating Single Page Application.

Starting an Empty Web Site Project in Visual Studio 2015 and adding Nuget packages for AngularJS Core and Routing.


Though AngularJs can be developed using any Text Editor I prefer using Visual Studio which also provides intellisense for AngularJS.


<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script src="Scripts/angular.js"></script>
</head>
<body>

    <div ng-app="">
        Name <input type="text" ng-model="Name"/>
        <br/>
        Hello {{Name}}
    </div>

</body>
</html>



AngularJS comprises of Directives which can be defined as  Html Atrributes. ng-model here behind the scene creates a property in the ViewModel. 
{{}} is equivalent to <% %> in Asp .Net Web Forms.

As you type inside the textbox, the Hello message gets updated,


ng-repeat Directive
ng-repeat is equivalent to foreach in C#, ie. iterating a colleciton.

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script src="Scripts/angular.js"></script>
</head>
<body>
    <div ng-app="" ng-init="names=['Shalvin P D','Ajay','Rajeev']">
        <ul>
            <li ng-repeat="x in names">
                {{ x }}
            </li>
        </ul>
    </div>
</body>
</html>


Here I am using ng-init for initilizing the data.

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script src="Scripts/angular.js"></script>
</head>
<body>
    <div ng-app="" ng-init="names=[
{name:'Shalvin',location:'Kochi'},
{name:'Rajeev',location:'Kollam'},
{name:'Ajesh',location:'Alappey'}]">

        <ul>
            <li ng-repeat="x in names">
                {{ x.name + ' - ' + x.location }}
            </li>
        </ul>
    </div>
</body>
</html>




Here I am using a complex array comprising of name and location.



$scope Service

In AngularJS whatever starts with $ is  a service. $scope service is equivalent to ViewBag in Asp .Net MVC. It is the glue between Controller and View. $scope supports two way data binding.

Modules
Modules are equivalent to namespace in .Net.






<!doctype html>

<html ng-app="Contacts">
<head>
    <title>Contact Management</title>
   
    <script src="Scripts/angular.js"></script>
    
    <script>
      
      var parking = angular.module("Contacts", []);
     
      parking.controller("HomeCtrl", function ($scope) {
       
        $scope.Contacts = [
          {Name: 'Shalvin'},
          {Name: 'Sooraj'},
          {Name: 'Mahesh'}
        ];
        
      });
    </script>
</head>

<body ng-controller="HomeCtrl">
    <h3>Modules and Controllers - Shalvin</h3>
    <table>
        <thead>
            <tr>
                <th>Name</th>
            </tr>
        </thead>
        <tbody>
               <tr ng-repeat="n in Contacts">
                   <td>{{n.Name}}</td>
            </tr>
        </tbody>
    </table>
 </body>
</html>



Here I am defining a Module named Contacts which contains a controller named HomeCtrl. Inside the controller I an array of Names and passing it to View.



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