Friday, June 28, 2013

How to solve JavaScript error: Unable to get value of the property '': object is null or undefined?

There are normally two reasons to cause this problem:
1. Lost reference of needed JavaScript library.
2. The reference JavaScript library is not loaded when your script run.

Thursday, June 27, 2013

How to set and get selected value in dropdown list by jQuery

Set selected value:
var selectstring = 'select option[value="' + selectedValue + '"]';
$(selectstring).attr('selected', true);

Get selected value:
$("#dropdownlistId").change(function () {
alert($("#dropdownlistId").val());
});

Wednesday, June 26, 2013

Javascript Cookie helper

function createCookie(name,value,days) {
 if (days) {
  var date = new Date();
  date.setTime(date.getTime()+(days*24*60*60*1000));
  var expires = "; expires="+date.toGMTString();
 }
 else var expires = "";
 document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
 var nameEQ = name + "=";
 var ca = document.cookie.split(';');
 for(var i=0;i < ca.length;i++) {
  var c = ca[i];
  while (c.charAt(0)==' ') c = c.substring(1,c.length);
  if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
 }
 return null;
}

function eraseCookie(name) {
 createCookie(name,"",-1);
}


Reference:
http://www.quirksmode.org/js/cookies.html

What is PO file?

PO file is the file format to save message for internationalization and localization (I18n, L10n)

It is from a open source project: gettext
http://www.gnu.org/software/gettext/

The format like:
# translator-comments
#. extracted-comments
#: reference...
#, flag...
#| msgid previous-untranslated-string
msgid untranslated-string
msgstr translated-string

Example:
#: lib/error.c:116
msgid "Unknown system error"
msgstr "Error desconegut del sistema"


It is very friendly for developer and translator working together, and also make code more readable. To ASP.NET developer is another choice than resource file.

Monday, June 24, 2013

What's the class for Razor View in ASP.NET MVC

System.Web.Mvc.WebViewPage
http://msdn.microsoft.com/en-us/library/gg402107(v=vs.98).aspx

Could change this setting from
to customized one
in
~\Views\Web.config

URL encoding

http://blog.lunatech.com/2009/02/03/what-every-web-developer-must-know-about-url-encoding#URLgrammar

Monday, June 17, 2013

What is closure in Javascript by exmaple code?

var variable = "top-level";
function parentFunction() {
  var variable = "local";
  function childFunction() {
    print(variable);
  }
  childFunction();
}
parentFunction();

Closure:
A Javascript function is defined inside another function, its local environment will be based on the local environment that surrounds it instead of the top-level environment.

Reference:
http://eloquentjavascript.net/chapter3.html

Friday, June 14, 2013

How to export html table into Excel file in ASP.MVC?

        public ActionResult ExportToExcel(int id)
        {
            string sb = "html table tag in here";
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
            return File(buffer, "application/vnd.ms-excel");
        }

How to render a view into a string in ASP.NET MVC?


        protected string RenderPartialViewToString(string viewName, object model)
        {
            if (string.IsNullOrEmpty(viewName))
                viewName = ControllerContext.RouteData.GetRequiredString("action");

            ViewData.Model = model;

            using (StringWriter sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
                ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);

                return sw.GetStringBuilder().ToString();
            }
        }

How to set default value to a radio button in ASP.NET MVC 3

            Yes
            @Html.RadioButtonFor(model => model.TermsAndConditions, "True")
            No
            @Html.RadioButtonFor(model => model.TermsAndConditions, "False", new { Checked = "checked" })
             

How many number data type in Javascript?


Only one. All numbers in Javascript are 64bit (8 bytes) floating point numbers

Thursday, June 13, 2013

How to make content inside iframe 100% height without scrollbar?

<script language="javascript" type="text/javascript">
  function resizeIframe(obj) {
    obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
  }
</script>
<iframe src="http://google.com/" frameborder="0" scrolling="no" id="iframe" onload='javascript:resizeIframe
(this);' />

SignalR example code projects

Asp.Net SignalR Chat Room
http://www.codeproject.com/Articles/562023/Asp-Net-SignalR-Chat-Room

http://www.asp.net/signalr/overview/getting-started/tutorial-server-broadcast-with-aspnet-signalr

Wednesday, June 12, 2013

How to solve problem: Bootstrap not working with jQuery UI datepicker?

Need to make sure the references for javascript and css in following order:

    
    
    
    
    
    

How to get current Action and Controller name in ASP.NET MVC?


From ViewContext
        string action = ViewContext.RouteData.Values["action"].ToString();
        string controller = ViewContext.RouteData.Values["controller"].ToString();

Tuesday, June 11, 2013

Monday, June 10, 2013

How to make footer fixed to the bottom in Bootstrap?

By new class in Bootstrap 2.2.1: navbar-fixed-bottom

How to add Bootstrap into ASP.NET MVC 3?

http://www.codeproject.com/Articles/404633/Transform-ASP-NET-MVC3-Default-Template-with-Twitt

Download different themes from
http://bootswatch.com/

How to upgrade ASP.NET MVC 3 to MVC 4?

By nuget package:
PM> Install-Package UpgradeMvc3ToMvc4

https://nuget.org/packages/UpgradeMvc3ToMvc4