Thursday, March 22, 2012

Select List Box Created using Client Side code


<html>
<head>
<script language="JavaScript" type="text/javascript">
<!--
var ios = 0;
var aos = 0;
function insertOldSchool(theSel, newText, newValue)
{
  if (theSel.length == 0) {
    var newOpt1 = new Option(newText, newValue);
    theSel.options[0] = newOpt1;
    theSel.selectedIndex = 0;
  } else if (theSel.selectedIndex != -1) {
    var selText = new Array();
    var selValues = new Array();
    var selIsSel = new Array();
    var newCount = -1;
    var newSelected = -1;
    var i;
    for(i=0; i<theSel.length; i++)
    {
      newCount++;
      if (newCount == theSel.selectedIndex) {
        selText[newCount] = newText;
        selValues[newCount] = newValue;
        selIsSel[newCount] = false;
        newCount++;
        newSelected = newCount;
      }
      selText[newCount] = theSel.options[i].text;
      selValues[newCount] = theSel.options[i].value;
      selIsSel[newCount] = theSel.options[i].selected;
    }
    for(i=0; i<=newCount; i++)
    {
      var newOpt = new Option(selText[i], selValues[i]);
      theSel.options[i] = newOpt;
      theSel.options[i].selected = selIsSel[i];
    }
  }
}

function appendOldSchool(theSel, newText, newValue)
{
  if (theSel.length == 0) {
    var newOpt1 = new Option(newText, newValue);
    theSel.options[0] = newOpt1;
    theSel.selectedIndex = 0;
  } else if (theSel.selectedIndex != -1) {
    var selText = new Array();
    var selValues = new Array();
    var selIsSel = new Array();
    var newCount = -1;
    var newSelected = -1;
    var i;
    for(i=0; i<theSel.length; i++)
    {
      newCount++;
      selText[newCount] = theSel.options[i].text;
      selValues[newCount] = theSel.options[i].value;
      selIsSel[newCount] = theSel.options[i].selected;
     
      if (newCount == theSel.selectedIndex) {
        newCount++;
        selText[newCount] = newText;
        selValues[newCount] = newValue;
        selIsSel[newCount] = false;
        newSelected = newCount - 1;
      }
    }
    for(i=0; i<=newCount; i++)
    {
      var newOpt = new Option(selText[i], selValues[i]);
      theSel.options[i] = newOpt;
      theSel.options[i].selected = selIsSel[i];
    }
  }
}

function removeOldSchool(theSel)
{
  var selIndex = theSel.selectedIndex;
  if (selIndex != -1) {
    for(i=theSel.length-1; i>=0; i--)
    {
      if(theSel.options[i].selected)
      {
        theSel.options[i] = null;
      }
    }
    if (theSel.length > 0) {
      theSel.selectedIndex = selIndex == 0 ? 0 : selIndex - 1;
    }
  }
}
//-->
</script>
</head>
<body>

<form>
<input type="button" value="Insert"
 onclick="ios++; insertOldSchool(this.form.selos, 'Insert' + ios, 'insert' + ios);" />
<input type="button" value="Append"
 onclick="aos++; appendOldSchool(this.form.selos, 'Append' + aos, 'append' + aos);" />
<input type="button" value="Remove"
 onclick="removeOldSchool(this.form.selos);" />
<br />
<select name="selos" size="10" multiple="multiple">

</select>
</form>
</body>
</html>

Monday, March 19, 2012

JQuery Ajax method to call server side function without postback


Client Code:
<div>
        <h3>
            This is AutoSave demo!</h3>
        <div>
            <textarea id="ContentTextarea" cols="50" rows="10"><%=HttpContext.Current.Session["UserContent"]%>  "Balajiprasad"</textarea>
            <input type='text' value='<%=HttpContext.Current.Session["UserContent"]%>' ></label>
            <br />
            <button id="SaveContentButton">
                Save</button>
            <span id="autosaveUpdates"></span>
        </div>
    </div>

    <script type="text/javascript">
        function SaveDataViaAjax(autosaveMode) {
            $("#autosaveUpdates").html("Saving ... ");

            methodURL = "Default2.aspx/SaveContent";
            parameters = "{'content':'" + $("#ContentTextarea").val() + "', 'autosave':'" + autosaveMode + "'}";

            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: methodURL,
                data: parameters,
                dataType: "json",
                async: true,
                success: function (data) {
                    if (autosaveMode) {
                        $("#autosaveUpdates").html("Autosave at " + Date().toLocaleString());
                    }
                    else {
                        $("#autosaveUpdates").html("Data have been saved at " + Date().toLocaleString());
                    }
                },
                error: function (result) {
                    alert("Due to unexpected errors we were unable to load data" + result);
                }
            });
        };

        $(document).ready(function () {
            $("#SaveContentButton").click(function (event) {
                SaveDataViaAjax(true)
                event.preventDefault();
            });
        });
    </script>


Server Code:

    [WebMethod]
    public static void SaveContent(string content, bool autosave)
    {
        if (autosave)
        {
            HttpContext.Current.Session["UserContent"] = content + "Ajax hit";
        }
        else
        {
            // TODO save data to a database
            HttpContext.Current.Session["UserContent"] = String.Empty;
        }
    }

Friday, March 9, 2012

Mouse Zoom In and Mouse Zoom out code in Javascript


//Script for Mouse events.
<script language="javascript">
        window.onload = function () {

            if (window.addEventListener) {
 //Setting mouse event for the entire document, we also set the for particular event
                document.addEventListener('DOMMouseScroll', moveObject, false);
            }

            //for IE/OPERA etc
             document.onmousewheel = moveObject;
        }
        function moveObject(event) {
            var delta = 0;

            if (!event) event = window.event;

            // normalize the delta
            if (event.wheelDelta) {

                // IE and Opera
                delta = event.wheelDelta / 60;

            } else if (event.detail) {

                // W3C
                delta = -event.detail / 2;
            }

            var currPos = document.getElementById('scroll').offsetTop;

            //calculating the next position of the object
            var nextPos = parseInt(currPos) - (delta * 10);

            if (currPos > nextPos) {
                //code for zoom in
            }
            else {
                //code for zoom out
            }
        }

    </script>

     //HTML Code – Div for setting the Mouse event (up/down) dynamically
     <div id="scroll" style="position: absolute;display:none;">
    </div>

Thursday, March 8, 2012

SQL Query to get Database Size & Individual table size



--total database size
sp_spaceused

--individual table size
select object_name(id) [Table Name],
[Table Size] = convert (varchar, dpages * 8 / 1024) + 'MB'

from sysindexes where indid in (0,1)
order by dpages desc