Translate

Wednesday, August 29, 2012

How to open (view) component services manager

To view the Com+ components in your windows 7 machine go through the following steps

1>Click Start

2>In the Search programs and files box type in comexp.msc





3>Hit enter












4>Expand Component services

5>Expand Computers

6>Expand My computer.

7>Expand COM+ Applications


For windows server 2003 or windows XP

 run-->dcomcnfg

Tuesday, August 14, 2012

mvc3 routes tutorial ( An introduction to MVC routes)


Routing overview





















When a person types in something like http://www.asp.net/MVC/MVC3  into his browser window, what is shown above is a summary of what happens. The request goes to the webserver, where the application sends the request to an action method in a controller. The action method does some logic and passes some data into a view. That view uses that data and creates an HTML output which is returned to the user.   Now the question naturally arises, how does the application decide which action method to send the request to?  That is where routing comes in. The routing code in the global.asax.cs page of your application is where you decide which controller/action to send the request to. 
       

When you create an asp.net mvc application by default this line of code gets added in the Global.asax.cs of your application


            routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }

  

If you remove that line of code and have no other routing added, then all calls to your application would result in a 404 error. Lets dissect the above line of code and see what the parameters to the Maproute method mean.

The route name
"Default"
This is just a name for your route. You can call the route anything. If you want you can also call it hulk :)


The route mapping
"{controller}/{action}/{id}"
What this means is that the first segment after the url, will map to the controller name, the second segment maps to the action name and the third segment to a parameter named id. A segment is demarcated by the forward slash   (/segment1/segment2)


















If we try to access a url of the form http://localhost:51993/Home/Index/2.

The action method that accepts it, can access the parameter id, if the action method has the signature

public ActionResult Index(int? id)

Three things to note here. 
  • The action method accepts the parameters in the order they are in the url.
  • The action method parameter has to have the same name as defined in the mapping
  • Since the parameter is optional (explained below), the int in the action method has to be nullable. 
  • {} is a placeholder.  (More on this in points to note section below)


The Default values
new { controller = "Home", action = "Index", id = UrlParameter.Optional }

Here we are setting default values for each segment. If the route segments are missing, it will assign these values to those segments. For instance, If I type in the url
http://localhost:51993/Account
 Since the segment for action is missing, it will get set to index. We also say id=UrlParameter.Optional which means its ok to skip the id parameter. So due to the default values, the applications ends up looking for
http://localhost:51993/Account/Index


Some points to note

  • If you try to reach a url that has more or less segments than what is defined in the mapping, you will get a 404 error.
  • If you have multiple route mappings defined, then the application will pick up the very first one that matches. Hence always define routes from most specific to most general. 
  • Suppose I change the default routing to 
routes.MapRoute(
                "Default"
                "Home/{action}/{id}"
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
            );

Note here that instead of  {controller} (inside the placeholder{} )I have hard coded Home. 
If this is the case, a request to the url http://localhost:51993/Account will throw a 404 error.

The reason being, the application looks for urls of the form 
http://localhost:51993/home/

If the application does get a request of the above form, it will map the parts after that to the action and id respectively.

  • Generally you need to define you own route mappings only if your action method has more than one parameter, or it accepts parameters of the non native types (an instance of a class for example).
Route Debugging

Use this tool for route debugging

Sunday, August 12, 2012

Jquery attribute selectors



Operator
Description


=
Equal to (Value should match)
!=
Not equal to
^=
Starts with
~=
Contains
|=
Equal to or begins with followed by a hyphen(-)

Go to http://codylindley.com/jqueryselectors/ to play around with jquery selectors.

asp.net mvc 3 html helper examples


How to create a form using HTML helper

Markup in the View:

@using (Html.BeginForm("PostData""Home"FormMethod.Post, new { id="myForm",whateverParameter="foo"}))

}

HTML Output:

<form action="/Home/PostData" id="myForm" method="post" whateverParameter="foo">

 </form>


 How to Create a text box using HTML helper
Markup in the View:
@Html.TextBox("clientID","Hello World")

HTML Output:

 <input id="clientID" name="clientID" value="Hello World" type="text" />

How to create a textbox for password using HTML helper

Markup in the view
@Html.Password("myClientID");


HTML output
<input id="myClientID" name="myClientID" type="password" />

How to create a Text Area using HTML helper

Markup in the View:
  @Html.TextArea("clientID","hello  world",10,20,null)

HTML Output:

<textarea cols="20" id="clientID" name="clientID" rows="10">hello  world</textarea>




How to Create a radio button using HTML helper

Markup in the view :
@Html.RadioButton("myClientID", "Ferrari");


HTML Output:
<input id="myClientID" name="myClientID" value="Ferrari" type="radio" />

How to create a radio button list using HTML helper

The trick here is to give the same ID to all the radio buttons

Markup in the view

    @Html.RadioButton("myClientID", "Ferrari", true);
    @Html.RadioButton("myClientID", "Ford");
    @Html.RadioButton("myClientID", "Toyota");


HTML Output:
 
<input id="myClientID" name="myClientID" type="radio" value="Ferrari" checked="checked" />
<input id="myClientID" name="myClientID" type="radio" value="Ford" />
<input id="myClientID" name="myClientID" type="radio" value="Toyota" />



How to create a checkbox using HTML helper
Markup in the view
@Html.CheckBox("myClientID", true);


HTML output
<input checked="checked" id="myClientID" name="myClientID" value="true" type="checkbox" />


How to create a link to a controller using HTML helper


Markup in the view
@Html.ActionLink("Goes to the Logon Method in Account Controller", "LogOn", "Account");


HTML output
<a href="/Account/LogOn">Goes to the Logon Method in Account Controller</a>



How to create a dropdown list using HTML helper

Creating a drop down list is slightly more complicated than the others. To be able to create a dropdown list, you must have a method that returns an object of the type  System.Web.Mvc.SelectList.

Given is a example of how to return a SelectList which is ordered by name. The selected value is 3.


       public  SelectList myDropDownList()
       {
           Colors c1 = new Colors();
           c1.Name = "Black";
           c1.Value = "1";
           Colors c2 = new Colors();
           c2.Name = "Blue";
           c2.Value = "2";
           Colors c3 = new Colors();
           c3.Name = "Yellow";
           c3.Value = "3";

           List<Colors> li = new List<Colors>();

           li.Add(c1);
           li.Add(c2);
           li.Add(c3);

           SelectList sl = new SelectList(li.OrderBy(x => x.name), "Value", "Name", 3);

           return sl;

       }

This SelectList is bound to a dropdown as shown below

@Html.DropDownList("clientID", Model.myDropDownList())


HTML output
 
<select id="clientID" name="clientID">
        <option value="1">Black</option>
        <option value="2">Blue</option>
        <option selected="selected" value="3">Yellow</option>
    </select>

Thursday, August 9, 2012

check all checkboxes using jquery

I wrote this code that checks all checkboxes in a table.











<html>
<head>
    <title></title>
    <script src="Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
</head>
<body>
    <table id="mytable">
        <tr>
            <td>
                <input id="chkParent" type="checkbox" />
            </td>
            <td>
                Parent
            </td>
        </tr>
        <tr>
            <td>
                <input id="Checkbox2" type="checkbox" />
            </td>
            <td>
                1
            </td>
        </tr>
        <tr>
            <td>
                <input id="Checkbox3" type="checkbox" />
            </td>
            <td>
                2
            </td>
        </tr>
        <tr>
            <td>
                <input id="Checkbox4" type="checkbox" />
            </td>
            <td>
                3
            </td>
        </tr>
    </table>
    <script type="text/javascript">

        $(document).ready(function () {
       

            $('#chkParent').click(SetKids);

            function SetKids() {

                  var isChecked = this.checked; 


                $('#mytable').find(':checkbox').attr('checked', isChecked);
            }

        });

    </script>

</body>
</html>






You can copy paste this code into a text file and name the file with an htm extension. Where ever you decide to put the htm file, create a folder called Scripts. In that folder put this file.

How to read from a config file within the project

By default the config file an application reads is the config file in the project that has the entry point.  For instance if you have a console application that references a project A. And if project A has a config file. When you run the app, you will notice that even if you put settings in the config file, when you try to read, it always comes out as null. The reason is the application tries to read from a config file from the project that has the main method (entry point to the app).

The code below will help you read from the config file from within the project.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
 
namespace ClassLibrary1
{
    public class Class1
    {
 
        public static string ConfigRead(string locOfConfig)
        {
 
            //eg   locOfConfig =@"C:\SandBox\ConsoleApplication2\ClassLibrary1\App.config";
 
            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = locOfConfig;
 
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
 
            string x = config.AppSettings.Settings["test"].Value;
 
            return x;
        }
    }
 
 
}

 
 
}


Don't forget to add a reference to the System.Configuration dll within your project