Wednesday, March 29, 2017

Set [Enter] key as default action to cause associated button to click

In the example below, each textbox has its own default button. While focus is in a text box, pressing [Enter] key will cause the associated button to click so that you do not need to click the associated button separately.



<!doctype html>
<html>
<head><title>Set Enter Key as default Click</title>
<style type="text/css">
 body { font-family: Verdana, Tahoma; line-height: 1.7em; font-size: 0.85em; }
</style>
<script
  src="https://code.jquery.com/jquery-1.12.4.min.js"
  integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
  crossorigin="anonymous"></script>
</head>
<body>
 <h1>Enter Key Capture</h1>
 <div>
  <input type="text" id="txt1" />
  <input type="button" id="btn1" class="defaultButton" value="Button1" />
 </div>
 <div>
  <input type="text" id="txt2" />
  <input type="button" id="btn2" class="defaultButton" value="Button2" />
 </div>
</body>
<script type="text/javascript">
 $( document ).ready( function() {
  
  $( 'input[type=text]' ).bind({
   keypress: function( event ){
    if( checkIfEnterKeyPressed() ){
     
     var message = "Enter key has been pressed while in " + 
         $(this).attr("id") + ".";
     
     $(this).siblings(".defaultButton").bind(
      "click", 
      { msg: message }, 
      function( event ){
       alert( event.data.msg + "\n" + $(this).val() + " clicked." );
      }
     ).click();
    }
   }
  });
 
  var checkIfEnterKeyPressed = function(){
   var keycode = ( event.keyCode ? event.keyCode : event.which );
   return ( keycode == '13' ? true : false );
  };
 });

</script>
</html>

Good old popup dialog

Good old popup dialog example: parent page collects data from child popup dialog.


parent.html
<!doctype html>
<html><head><title>parent</title>
</head>
<body>
Name: <input type="text" id="txtName" readonly="readonly" />
<input type="button" value="Open Popup" onclick="openPopup()" />
</body>
<script type="text/javascript">
 var openPopup = function(){
  popup = window.open( "popup.html", "Select Name", "width=300,height=100;" );
  popup.focus();
 };
</script>
<html>


popup.html
<select name="ddlNames" id="ddlNames">
 <option value="Ken">Ken</option>
 <option value="Steve">Steve</option>
 <option value="Sam">Sam</option>
 <option value="Kirk">Kirk</option>
</select
<br /><br />
<input type="button" value="Make Selection" onclick="makeSelection()" />
<script type="text/javascript">
 var makeSelection = function(){
  if( window.opener != null && !window.opener.closed ){
   var nameSelected = document.getElementById( "ddlNames" ).value;
   var txtName = window.opener.document.getElementById( "txtName" );
   txtName.value = nameSelected;   
  } 
  window.close();
 };
</script>

Linq Example of Group-By

Group-By Example in Linq.

var groupByResult = 
    from p in db.Person
    join s in db.School
    on p.SchoolID = s.SchoolID
    group new { p, s } by new { p.SchoolID, s.SchoolName }
    into grp
    select new
    {
        Count     = grp.Count(),
        SchoolID  = grp.Key.SchoolID,
        SpaceName = grp.Key.SchoolName
    }