First off, why are you putting your table html element into your HeadContent container? Shouldn't you only be putting the script element in the HeadContent while putting the table in the MainContent container?
To fix your issue you need to modify the javascript line so it writes the controls ClientID.
Change this line...
var TestVar = document.getElementById('txtName').value;
To this...
var TestVar = document.getElementById('<%= txtName.ClientID %>').value;
If the control is tagged as runat="Server" then you can't reference the control's id in javascript as it is in the .aspx page because asp.net will pre-pend to the name based on the control's parent controls. For example, your id of 'txtName' id actually becomes something like 'MainContent_txtName' when it gets rendered to the response stream. To address this you do the above where you reference the ClientID in your javascript.