Link to home
Start Free TrialLog in
Avatar of onebite2
onebite2

asked on

C#, Javascript with Visual Studio 2005

I'm getting a error on Javascript. I'm not sure what's wrong.
Here is the error message.

"Microsoft JScript runtime error:Object expected"  on  If condition using javascript.

I get the error right away when i Hit F5.
<script language="javascript" type="text/javascript">
 
function Button1_onclick() {
 
     var StrText = document.getElementById("TextBox1");
     var myString = StrText.value;
     var myString1 = myString.length;
     alert (myString1)
     If (myString1 > 0) 
     {
        alert ('Valid Path');
     }   
     else
     {
        alert ('Invalid Path');
     }
}
 
Under the form tag i have 
<input id="Button1" type="button" value="button" language="javascript" onclick="Button1_onclick()" />

Open in new window

Avatar of hielo
hielo
Flag of Wallis and Futuna image

Your text field has id="Button1", NOT id="TextBox1", so this:
document.getElementById("TextBox1");
is returning null. Change it to:
document.getElementById("Button1");
Avatar of onebite2
onebite2

ASKER

There is textbox too..

<form id="form1" runat="server" action=Default.aspx method=post>
 
<h3>GridView EmptyDataTemplate Example</h3>
 
<asp:GridView ID="GridView1" runat="server" Height="381px" Width="836px">
 
</asp:GridView>
 
<asp:Label ID="Label1" runat="server" Text="Label">Script Location: </asp:Label>
 
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 
<input id="Button1" type="button" value="button" language="javascript" onclick="Button1_onclick()" />

Open in new window

I would change

alert (myString1)


to

alert (StrText + ' - ' + StrText.id + ' - ' + StrText.value);

that will print out

HTMLElement - TextBox1 - {whatever The Value Is}

if you get "null - undefined - undefined" or something like that, then you have a DOM error, such as the textbox is not rendering on the page that that exact ID.
Not sure why would i change myString1.
I get a syntax error first with Yes & No option.
If i choose No then "Microsoft JScript runtime error:Object expected".
Instead of:
var StrText = document.getElementById("TextBox1")

try:
var StrText = document.getElementById("<%=TextBox1.ClientID%>")
Again not sure why should that...but i would give it try though
I double checked everything StrText, myString all are getting values there is absolutely no problem with my variables. There is something wrong with If condition syntax.

Problem is with the If condition. I get a syntax error on my If condition.
Please look at my IF condition and tell me what's wrong.

If (myString1 > 0)
{
      alert ('Valid Path');
}  
else
{
      alert ('Invalid Path');
}
yes but the 'if' condition references a variable, hence us giving suggestions on troubleshooting/validating the variables

Typically an "object expected" error happens when you try to run an object/element method against something that is not an object/element, which is why we are suspecting an issue with your document.getElementById line of code

Keep in mind that error reporting/code debuggers are not perfect. They will usually tell you which line the error finally came forth, but not where the error originated and needs to be solved.
Please forget about the "Microsoft JScript runtime error:Object expected" for a second.
I get syntax error on the page. I tried everything you guys suggested and that didn't make any difference.

thanks
Cindy
I think we need more information in order to help you. All the previous responders have dissected every possible part of your current method you showed us that could be the problem, and you have stated all of these come back fine or are not the problem.

Additionally, your OP says "I get the error right away when I hit F5". If that's true, then it's not an error in the Button1_onclick click event handler; as that would only show up when you click the button on the form.

And if it is happening when you click the button on the form after all, could you tell us what is being shown when the alert(MyString1) function is called?

One approach you can take to this, if you are pulling your hair out, is comment out the lines you *think* are causing the problem, run it again, and make sure you get no errors. Visual Studio 2005 is notorious for reporting javascript errors on incorrect line numbers, so you may be focusing on the wrong function entirely here.
May be its my fault. I didn't explain this issue in the correct way.

The moment i Hit F5, I get a error "Syntax Error" So it really doesn't matter if you hit the button or not button you would still get that error message if there is really a syntax error.
Now when i try to comment the else part in the IF condition then i don't get syntax error any more but then i get other error "Microsoft JScript runtime error:Object expected".
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default"  %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html>
  <head runat="server">
    <title>GridView EmptyDataTemplate Example</title>
<script language="javascript" type="text/javascript">
 
function Button1_onclick() {
 
     var StrText = document.getElementById("TextBox1");
     var myString = StrText.value;
     var myString1 = myString.length;
     alert (myString1)
     alert (StrText + ' - ' + StrText.id + ' - ' + StrText.value)
 
//What is wrong with IF Else condition below.
 
     If (myString1 > 0) {
        alert ('Valid Path');
     }   
     else//If i comment these 4 lines from here then i don't get syntax //error but then i get another error.
     {
        alert ('Invalid Path');
     }
}
 
 
// -->
</script>
</head>
<body>
    <form id="form1" runat="server" action=Default.aspx method=post>
 
      <h3>GridView EmptyDataTemplate Example</h3>
 
        <asp:GridView ID="GridView1" runat="server" Height="381px" Width="836px">
        </asp:GridView>
        <asp:Label ID="Label1" runat="server" Text="Label">Script Location: </asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <input id="Button1" type="button" value="button" language="javascript" onclick="Button1_onclick()" />
 
    </form>
  </body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of magicclaw
magicclaw
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What is the expected value of document.getElementById("TextBox1")? If you expect a numeric value then try:

if( !isNaN(myString1) && parseInt(myString1,10) > 0 )
{
alert("Valid Path");
}
else
{
alert("Invalid Path");
}

If you are expecting a string value, then use:
if( myString1.length > 0 )
{
alert("Valid Path");
}
else
{
alert("Invalid Path");
}


Once again, notice that the "if" is all in lower case.