简体   繁体   中英

ASP.NET and Javascript Error

I am upgrading a project from ASP.NET 1.1 to ASP.NET 2.0. In my aspx page, I have a hidden field, like this:

<input type="hidden" name="__TabControlAction" />

And I have the following javascript function:

function __tabStripPostBack(key) {
var theform;

if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
 theform = document.forms["Form1"];
}
else {
    theform = document.Form1;
}
    theform.__TabControlAction.value='Click';
    theform.__TabControlKey.value=key;
    theform.submit();
}

In ASP.NET 1.1, this code works fine. However, now that I upgraded to ASP.NET 2.0, I get "__TabControlAction is null or not an object" error. For whatever reason, it seems the javascript can't find the hidden field, even though its there. Anyone have any ideas?

I think the name of the form should be "aspnetForm", not "Form1". You should be able to directly refer to it since this bit of javascript is injected on every form with a runat="server" tag.

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['aspnetForm'];
if (!theForm) {
    theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>

Try changing your code to this:

function __tabStripPostBack(key) {
    theForm.__TabControlAction.value='Click';
    theForm.__TabControlKey.value=key;
    theForm.submit();
}

Is theform still defined? You might need to try this when you declare theform :

 theform = document.forms['aspnetForm'];

In other words, check your generated HTML to see what the name and id attributes of your <form> tag are.

Rather than referencing this through the form, can you give your input element an id and use document.getElementById('yournewid'); ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM