default button for asp.net user control (.net 1.1)

  406                 control.BasePage.RegisterStartupScript(“DefaultButtonForLogin”, String.Format(@”

  407 <script language=””javascript””>

  408 <!–

  409 function handler(e)

  410 {{

  411     if ((e && ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13))) || (event && event.keyCode == 13))

  412     {{

  413         document.forms[0].action = ‘{0}’;

  414         {1};

  415         return false;

  416     }}

  417 }}

  418 if (!!document.forms[0][‘{2}’])

  419     document.forms[0][‘{2}’].onkeydown = handler;

  420 if (!!document.forms[0][‘{3}’])

  421     document.forms[0][‘{3}’].onkeydown = handler;

  422 //–>

  423 </script>”,

  424                     secureURL,

  425                     control.BasePage.GetPostBackClientEvent(control.LoginButton, “”),

  426                     control.PasswordTextBox.ClientID,

  427                     control.EmailTextBox.ClientID));

특히 if (!!document.forms[0][‘{2}’]) 부분이 중요하다. !!는 truthy or falsy를 해주는 표현인데, asp.net control들이 visible=false 일 경우, 서버단에서는 control이 null이 아니지만, rendering이 되지않기 때문에, javascript에서는 null이거나 undefined 에러가 나는 경우가 많다. 그러므로, 반드시 null 또는 undefined 체크를 해줄 것.

Leave a comment