Thursday, October 25, 2012

JQuery Basics

To get value from text Box or Dropdown
$("#txtName").val();
$("#DropdownId").val()

To Set value : $("#txtName").val("Hi");

For Calling Dropdown change event:
$("#DropdownId").change(function(e)
        {  
            //Your Code
       }
);

For Button Click Event:
$(".btnSave").click(function ()
 {
   //Your Code

 }
);

For Disable Button                                 :  $(".btnSave").attr('disabled', 'disabled');
For Enable Button from Disable Mode : $(".btnSave").removeAttr('disabled');

For Calling Serverside method:

Server Side Method:
[WebMethod]
        public string GetResult(int Id = 0)
        {
           return result;
        }

Client Side Calling:
function Getvalue(NewId)
        {
             var result = null;
             var scriptUrl = "/Module/Page/GetResult?Id=" + NewId;            
             $.ajax({
                url: scriptUrl,
                type: 'get',
                dataType: 'html',
                async: false,
                success: function(data) {
                    result = data;
                }
             });
      }

Get Date and Time:
            var currentDateTime = new Date();
            var month = currentDateTime.getMonth() + 1
            var day = currentDateTime.getDate()
            var year = currentDateTime.getFullYear()
            var hours = currentDateTime.getHours()
            var minutes = currentDateTime.getMinutes()
            var currentDate = day + "/" + month + "/" + year;

Get Query String Value:

function querystring(key)
{
   var re=new RegExp('(?:\\?|&)'+key+'=(.*?)(?=&|$)','gi');
   var r=[], m;
   while ((m=re.exec(document.location.search)) != null) r.push(m[1]);
   return r;
}

calling : var Queryvalue =querystring('id'); 

To view the Json Object:

JSON.stringify(jsonobject);

To Merge another json object:

var jsonobject1=Yourobject1;
var jsonobject2=Yourobject2;
jsonobject1.push.apply(jsonobject1,jsonobject2);






 

Wednesday, April 18, 2012

Checkbox List Select/Deselect using JavaScript

function Select(Select)
          {
            for (var n=0; n < document.forms[0].length; n++)
                if (document.forms[0].elements[n].type=='checkbox')
                    document.forms[0].elements[n].checked=Select;            
            return false;
           }

<a href="javascript:void(0)" style="font-weight: bold; text-decoration: none; color: Blue;"
                                                                        onclick="javascript:Select(true)">Check all</a> | <a href="javascript:void(0)" style="font-weight: bold;
                                                                            text-decoration: none; color: Blue;" onclick="javascript:Select(false)">Un-check
                                                                            all</a>

Creating/Expiring Cookies

Creating Cookie :
-------------------
      
        HttpCookie myCookie = new HttpCookie("UserSettings");
        myCookie["UserID"] = strUserID;
        myCookie["Name"] = strName;
        myCookie.Expires = DateTime.Now.AddDays(1d);
        Response.Cookies.Add(myCookie);

Geting Cookie Values :
------------------------
if (Request.Cookies["UserSettings"] != null)
            {               
                if (Request.Cookies["UserSettings"]["UserID"] != null)
                    Session["UserID"] = Request.Cookies["UserSettings"]["UserID"];

                if (Request.Cookies["UserSettings"]["RewritePhyName"] != null)
                    Session["Name"] = Request.Cookies["UserSettings"]["Name"];
            } 

Expiring Cookie :
---------------------

if (Request.Cookies["UserSettings"] != null)
                {
                    HttpCookie myCookie = new HttpCookie("UserSettings");
                    myCookie.Expires = DateTime.Now.AddDays(-1d);
                    Response.Cookies.Add(myCookie);
                }