ASP.NET jQuery: Trying to select textbox in gridview: edititemtemplate

ASP.NET jQuery: Trying to select textbox in gridview: edititemtemplate

ASP.NET jQuery: Trying to select textbox in gridview: edititemtemplate

I'm trying to run a jQuery function when a key is pressed in that textbox.  While the code worked great when I applied this to a formview, it's not working with a gridview.
  1. $(document).ready(function () {
  2.             var MaxLength = 500;
  3.             $(#'<%=gvViewNotes.FindControl("txtNotes_e").ClientID%>').keypress(function (e) {
  4.                 alert("hi");
  5.             });            
  6.         });
When the page loads, I get a "NullReferenceException" by Visual Studio when I run the page in debug mode.

My theory is that I'm getting the error because the jQuery function is calling before the gridview is databound.

I made sure that the ClientID is static beforehand.  When I take out this code and run the page, I am able to inspect the element and get this information: 
  • Name= 'ct100$ContentPlaceholder1$gvViewNotes$ct102$txtNotes_e'
  • ID = 'txtNotes_e'
Does anyone have any idea how I can adjust this code so it won't cause a null reference error? thanks.