adsense

Wednesday, November 21, 2018

Adding Web Optimization framework to ASP.Net Web forms

The ASP.Net Web Optimization Framework is used to optimize the ASP.NET web application performance by reducing the number of server requests and  the size of the requested resources.


Following article explains how to integrate the  ASP.Net Web Optimization Framework to to ASP.Net Web forms project. 

When the scripts are rendered in the master page a place holder is used to wrap the rendered scripts as shown below.

<head runat="server">
    <asp:PlaceHolder runat="server">
        <%: Scripts.Render("~/bundles/jquery") %>
        <%: Styles.Render("~/Content/bootstrap") %>
    </asp:PlaceHolder>
</head>

This ensures that bundles are rendered  properly.

Cheers,
Samitha

Wednesday, October 24, 2018

Find checkbox in span using jQuery

jQuery can be used to find and checkbox inside a span as shown below.


<span class="span1" >
        <input id="chkSelect" 
             checked="checked"
             type="checkbox" /></span>

<script type="text/javascript" >
        $(function () {
            $(".span1 input[type='checkbox']").click(function () {
                if ($(this).is(":checked")) {
                    alert("Checked");
                }
                else {
                    alert("UnChecked");
                }
            });
        });
   </script >


Tuesday, October 16, 2018

check connenction from javascript

We can use javascript to check the current connection and determine if we run in local host. Here I have included the checking of custom domain name as well,


if (window.location.hostname === "localhost" || location.hostname === "127.0.0.1" || window.location.hostname.indexOf('.local') >= 0) {
    alert("Running Locally");
}

Cheers,
Samitha

Tuesday, September 25, 2018

regex to validate HH:MM AM/PM

Following regex can be used to validate HH:MM AM/PM  time format.

^([0-1]?[0-9]|2[0-3]):[0-5][0-9] [APap][mM]$

e.g
Assuming the timper control has provided class "tod"  following code will validate time when user enters a value and tabbed out/focus out from the control.

Javascript
<script  type="text/javascript">

funciton pageLoad(){
  $(".tod").focusout(function (e) {
            if (e.target.value.trim() !== "") {
                var result = checkTime(e.target.value.trim());
                if (!result) {
                   //invalid time
                }
                else {
                   //clear validation
                }
            }
            else {
                 //clear validation
            }
        });
}

function checkTime(value) {
        var pattern = new RegExp("(^([0-1]?[0-9]|2[0-3]):[0-5][0-9] [APap][mM]$)");
        var result = pattern.test(value);
        return result;
    }

</script>



cheers,
Samitha

Thursday, September 6, 2018

NuGet Package restore failed

If you ever encounter this error  delete all the related packages will resolve this.

I will further explain this using an example.

Suppose you get this error for Microsoft.Bcl.Build.Tasks.dll as shown below.

NuGet Package restore failed for project MyProject.Application: The process cannot access the file 'C:\MySolution\packages\Microsoft.Bcl.Build.1.0.21\build/Microsoft.Bcl.Build.Tasks.dll' because it is being used by another process..

This can be resolved by deleting all the Microsoft.Bcl.* folders in the packages under the solution.

Regards,
Samitha

Friday, August 17, 2018

Find an element based on data- value

There are situations where we want to find element based on data- attributes. For instance we have to set validation message at run time for a given field.

In these situations we can use jQuery to easily find an element based on the  data- atttibutes as shown below.

 $('span[data-valmsg-for="' + fieldName + '"]');


cheers,
Samitha


Monday, August 13, 2018

jQuery checkbox OnChange

We can use jQuery to identify checkbox state change as shown below.

<script  type="text/javascript">

 funciton pageLoad() //can use either DOM Ready or pagLoad
{

   const chkSelectAll= $("#chkSelectAll");

   chkSelectAll.change(function(event) {
             var chkBox= event.target;
             if (chkBox.checked) {
               //Checkbox is checked
            } else {
              //Checkbox is unchecked
           }
});

}
</script>
Cheers,
Samitha