admin管理员组

文章数量:1427333

I have the code

<script type="text/javascript">
    function validateLength(objectSource, args) {
        var a = document.getElementById('<%=txtUserName.ClientID%>').value;
        args.IsValid = (a.length >= 5 && a.length <= 10);
    }
</script>

Which I have in my ASP.NET page.

This works fines. Will validate a textbox to make sure it is between a certain length.

However I am trying to tidy up my page's code. So decided to put this little function into a separate javascript file.

<script type="text/javascript" src="Scripts/MyJS.js">
</script>

Page loads fine and then when the validation es around it fails. I am guessing it cannot access the textbox( txtUserName).

My question is: How can I get this to work?

I get the error message

0x800a138f - Microsoft JScript runtime error: Unable to get value of the property 'value': object is null or undefined

when the javascript fires

I have the code

<script type="text/javascript">
    function validateLength(objectSource, args) {
        var a = document.getElementById('<%=txtUserName.ClientID%>').value;
        args.IsValid = (a.length >= 5 && a.length <= 10);
    }
</script>

Which I have in my ASP.NET page.

This works fines. Will validate a textbox to make sure it is between a certain length.

However I am trying to tidy up my page's code. So decided to put this little function into a separate javascript file.

<script type="text/javascript" src="Scripts/MyJS.js">
</script>

Page loads fine and then when the validation es around it fails. I am guessing it cannot access the textbox( txtUserName).

My question is: How can I get this to work?

I get the error message

0x800a138f - Microsoft JScript runtime error: Unable to get value of the property 'value': object is null or undefined

when the javascript fires

Share Improve this question edited Sep 10, 2015 at 15:01 Sam asked Sep 10, 2015 at 14:56 SamSam 9483 gold badges13 silver badges32 bronze badges 10
  • 4 <script type="text/javascript" src"Scripts/MyJS.js"> -> <script type="text/javascript" src="Scripts/MyJS.js"> – messerbill Commented Sep 10, 2015 at 14:57
  • yeah you forgot an = by src – johnny 5 Commented Sep 10, 2015 at 14:59
  • @messerbill that was me forgetting while typing the code here. In my actual code there is an = – Sam Commented Sep 10, 2015 at 15:00
  • document.getElementById('<%=txtUserName.ClientID%>') is not found / existent on your page. Have you spelled it correctly? – messerbill Commented Sep 10, 2015 at 15:05
  • @messerbill the code works fine when the script is in the page itself. But when loading from a .js file it cannot find it – Sam Commented Sep 10, 2015 at 15:06
 |  Show 5 more ments

2 Answers 2

Reset to default 4

When you include a javascript file, the part of code

<%=txtUserName.ClientID%>

isn't rendered.

You have to change the javascript function in two ways Fixing the name of the control

<script type="text/javascript">
    function validateLength(objectSource, args) {
        var a = document.getElementById('txtUserName').value;
        args.IsValid = (a.length >= 5 && a.length <= 10);
    }
</script>

or adding a parameter

<script type="text/javascript">
    function validateLength(objectSource, args, nameOfControl) {
        var a = document.getElementById(nameOfControl).value;
        args.IsValid = (a.length >= 5 && a.length <= 10);
    }
</script>

then you can call the function from your page and the parameter nameOfControl could be

<%=txtUserName.ClientID%>

for example

validateLength(objectSource, args, <%=txtUserName.ClientID%>)

The <%=txtUserName.ClientID%> runs in context of your page. That will not work in a separate .js file. Separate .js files won't run any asp code. You could do something like:

JS file:

function validateLength(objectSource, args) {
    var a = document.getElementById(window.myValidatorId).value;
    args.IsValid = (a.length >= 5 && a.length <= 10);
}

ASP

<script type="text/javascript">
    window.myValidatorId = '<%=txtUserName.ClientID%>';
</script>

Or perhaps read the id from a hidden input field on the page.

本文标签: cInserting Javascript into ASPNet pageStack Overflow