adsense

Friday, January 27, 2017

asp.net mvc disable submit when ENTER key is pressed

When you create a view with a submit button, the default behavior is submit will be triggered on KEY PRESS events.

If your view has a BeginForm, following code will stop submitting on KEY PRESS.

@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { onkeydown = "return event.keyCode!=13" }))
{
    //code
}


Alternatively you can use jQuery to resolve the same issue as follows

<script>

 $(function () {
         $(document).on("keydown", "input", function(e) {
             if (e.which==13) e.preventDefault();
        });
  });

</script>


Friday, January 20, 2017

Rotate Image OnClick

An image can be easily rotated using JavaScript or jQuery. I ll share both ways in this post.

HTML

 <img src="Pic.jpg" id="image" onclick="rotate90(this)"> 
 
JavaScript

 <script>
    var angle=90;
 
    function rotate90(ele){
  
     if(angle>360)//reset angle
                angle=90;
 
     ele.style.webkitTransform="rotate("+angle+"deg)";//Mozilla
     ele.style.msTransform ="rotate("+delta+"deg)"; //IE
 
     /*optionally set other vendor specific transformation
       ele.style.MozTransform = rotate("+delta+"deg)";
       ele.style.OTransform = rotate("+delta+"deg)";
    */
 
        angle+=90;
    }
 </script>
 
jQuery
 
 Use jQuery Rotate plugin to do the actual rotation 

 <script>
    var angle=90;
 
    function rotate90(ele){
  
       if(angle>360)//reset angle
                angle=90;
 
       $(this).rotate(angle);
 
        angle+=90;
    }
 </script>
 
Cheers
Samitha

Wednesday, January 11, 2017

asp.net MVC Make DIV redirect

 Suppose you have a DIV element on a page, such that when clicked should behave like a normal Hyperlink. This can be easily achieved using jQuery.

HTML

<div class="logo" data-action ="@Url.Action("ActionName", "ControllerName")" style="cursor: pointer;" >                   
  DivText       
</div>
 
JavaScript

<script>
   $(function() {

     $('.logo').click(function () {
                var url = $(this).attr('data-action');

                if (url !== undefined)
                    window.location.href = url;
            });
});

</script>