Results 1 to 38 of 38

Thread: [RESOLVED] Why is this simple code not working?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Resolved [RESOLVED] Why is this simple code not working?

    Code:
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    
        <script language="javascript" type="text/javascript">
        function validate()
        {
        if(document.getElementById.('TextBox1').value=="")
        {
        alert("Please enter a value");
        }
        }
        </script>
    
    </head>
    <body onload="loading()">
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <br />
                <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="validate()" />
            </div>
        </form>
    </body>
    </html>
    no alert box is displayed when i left the textbox blank and clicked the button

    please help

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Why is this simple code not working?

    Okay, first up, let's see if your method is getting called at all.

    Try this:

    Code:
        <script language="javascript" type="text/javascript">
        function validate()
        {
        alert("hi there");
        if(document.getElementById.('TextBox1').value=="")
        {
        alert("Please enter a value");
        }
        }
        </script>
    Does that result in an alert appearing?

    Are there any JavaScript errors on the page?

    Gary

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Why is this simple code not working?

    On another note, have you considered using the built in ASP.Net Validation controls to do this work? Seems to me like this would be a natural fit.

    Gary

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Why is this simple code not working?

    i think the syntax of this code is incorrect :
    Code:
    if(document.getElementById.('TextBox1').value=="")
    please help

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Why is this simple code not working?

    Code:
    <script language="javascript" type="text/javascript">
        function validate()
        {
        alert("hi there");
        if(document.getElementById.('TextBox1').value=="")
        {
        alert("Please enter a value");
        }
        }
        </script>
    no.......this code also does not shows any alert box...........

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Why is this simple code not working?

    Quote Originally Posted by gep13 View Post
    On another note, have you considered using the built in ASP.Net Validation controls to do this work? Seems to me like this would be a natural fit.

    Gary
    i agree with you gep

    but i am surprised why this bit of code is not working

    please help me to get it into work gep

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Why is this simple code not working?

    but this bit of java script works:
    Code:
    <&#37;@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    
        <script language="javascript" type="text/javascript">
        function loading()
        {
        alert("hi there");
        
        }
        </script>
    
    </head>
    <body onload="loading()">
        <form id="form1" runat="server">
            <div>
                
            </div>
        </form>
    </body>
    </html>

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Why is this simple code not working?

    Quote Originally Posted by HowTo View Post
    i think the syntax of this code is incorrect :
    Code:
    if(document.getElementById.('TextBox1').value=="")
    please help
    i tell this becaues after writing upto this:
    Code:
    if(document.getElementById.('TextBox1')
    when i clicked the "." then the value element is not shown by the intellisense

  9. #9
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Why is this simple code not working?

    Code:
    if(document.getElementById.('TextBox1').value=="")
    This should be

    Code:
    if(document.getElementById('TextBox1').value=='')
    Please mark you thread resolved using the Thread Tools as shown

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Why is this simple code not working?

    Code:
    if(document.getElementById('TextBox1').value=="")
    works

  11. #11
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Why is this simple code not working?

    Hey,

    Okay, having looked at your code a bit closer, and loaded up the JavaScript console in Chrome, I can see two problems with this:

    Code:
    <head runat="server">
        <title>Untitled Page</title>
    
        <script language="javascript" type="text/javascript">
        function validate()
        {
        if(document.getElementById.('TextBox1').value=="")
        {
        alert("Please enter a value");
        }
        }
        </script>
    
    </head>
    <body onload="loading()">
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <br />
                <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="validate()" />
            </div>
        </form>
    </body>
    Firstly, remove the loading function from the onload event, since it isn't defined, it is causing an error.

    Secondly, you have an extra . after getElementById.

    It should look like this:

    Code:
    <head id="Head1" runat="server">
        <title>Untitled Page</title>
    
        <script language="javascript" type="text/javascript">
        function validate()
        {
        if(document.getElementById('TextBox1').value=="")
        {
        alert("Please enter a value");
        }
        }
        </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <br />
                <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="validate()" />
            </div>
        </form>
    </body>
    Hope that helps!!

    Gary

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Why is this simple code not working?

    one more thing:
    Code:
    <&#37;@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    
        <script language="javascript" type="text/javascript">
        function validate()
        {
        if(document.getElementById('TextBox1').value=="")
        {
        alert("Please enter a value");
        }
        }
        </script>
    
    </head>
    <body >
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <br />
                <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="validate()" />
            </div>
        </form>
    </body>
    </html>
    if the alertbox is displayed then it means that the textbox is empty and so as soon as the user clicks the ok button of the page,then i done want the page to have a post back...........

    in the above code when the user clicks the ok button of the alert box then a postback takes place

    how to stop this postback?

  13. #13
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Why is this simple code not working?

    Return false if the test fails, else return true to postback

    Code:
     <script language="javascript" type="text/javascript">
        function validate()
        {
        if(document.getElementById('TextBox1').value=="")
        {
        alert("Please enter a value");
    return false;
        }
    return true;
        }
        </script>
    
    </head>
    <body >
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <br />
                <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return validate()" />
            </div>
        </form>
    Please mark you thread resolved using the Thread Tools as shown

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Why is this simple code not working?

    Code:
    <&#37;@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    
        <script language="javascript" type="text/javascript">
        function validate()
        {
        if(document.getElementById('TextBox1').value=="" && document.getElementById('TextBox2').value=="")
        {
        alert("Please enter the UserId and Password");
        return false;
        }
        else if(document.getElementById('TextBox1').value=="")
        {
        alert("Please enter the UserId");
        return false;
        }
        else if(document.getElementById('TextBox2').value=="")
        {
        alert("Please enter the Password");
        return false;
        }
        else
        {
        return true;
        }
        }
        </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Label ID="Label1" runat="server" Text="UserId :"></asp:Label>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <br />
                <asp:Label ID="Label2" runat="server" Text="Password :"></asp:Label>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                <br />
                <asp:Button ID="Button1" runat="server" Text="Submit" OnClientClick="return validate()" />
            </div>
        </form>
    </body>
    </html>
    please have a look at this code........is it ok

  15. #15
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Why is this simple code not working?

    Code:
    <script language="javascript" type="text/javascript">
        function validate()
        {
        if(document.getElementById('TextBox1').value=="" && document.getElementById('TextBox2').value=="")
        {
        alert("Please enter the UserId and Password");
        return false;
        }
        else if(document.getElementById('TextBox1').value=="")
        {
        alert("Please enter the UserId");
        return false;
        }
        else if(document.getElementById('TextBox2').value=="")
        {
        alert("Please enter the Password");
        return false;
        }
       
        return true;
       
        }
        </script>
    Last edited by danasegarane; Jun 16th, 2010 at 09:35 AM.
    Please mark you thread resolved using the Thread Tools as shown

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Why is this simple code not working?

    Code:
    <&#37;@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    
        <script language="javascript" type="text/javascript">
        function reset()
        {
        document.getElementById('TextBox1').value=""
        document.getElementById('TextBox2').value=""
        return false;
        }
        </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Label ID="Label1" runat="server" Text="UserId :"></asp:Label>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <br />
                <asp:Label ID="Label2" runat="server" Text="Password :"></asp:Label>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                <br />
                <asp:Button ID="Button2" runat="server" Text="Reset" OnClientClick="return reset()" />
            </div>
        </form>
    </body>
    </html>
    but when i click the button2 in the above code then the two textbox values are set to empty but there is a postback happening........how to stop this?

  17. #17
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Why is this simple code not working?

    Set the UsesubmitBehaviour=False for the button..
    Please mark you thread resolved using the Thread Tools as shown

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Why is this simple code not working?

    still i cant understand the meaning of this line:
    Code:
    return false;
    whats the difference between
    Code:
    event.returnValue=false;
    and
    Code:
    return false;
    please help

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Why is this simple code not working?

    ok i had a look at this:
    http://www.webdevelopersjournal.com/...jsevents2.html

    now i am confused about this bit of code for the reset button:
    Code:
    function reset()
        {
        document.getElementById('TextBox1').value=""
        document.getElementById('TextBox2').value=""
        return false;
        }

    if i dont Set the UsesubmitBehaviour=False for the button ; then why there is a postback after resetting the textbox?

    please please help

  20. #20
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Why is this simple code not working?

    Hey,

    I have to stress again, why are you going to the extreme of doing this, when all this, and more is done for you in the Validation Controls that ship with ASP.Net?!?

    Gary

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Why is this simple code not working?

    just for learning purpose gep........please help me to clear my concept

  22. #22
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Why is this simple code not working?

    Bottom line is, if you are using the OnClientClick property of the button, and you are trying to prevent a PostBack from happening, then you need to have your JavaScript function return false. If it doesn't, then the PostBack will occur.

    Part of the issue here, I suspect, is the fact that you have JavaScript errors in your function. Namely, the lines are not terminating with a ;.

    What browser are you using for debugging your application? These JavaScript errors should be getting shown to you.

    Gary

  23. #23

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Why is this simple code not working?

    ok you suggested me to terminate the lines with a ; and i did this:

    Code:
    function reset()
        {
        document.getElementById('TextBox1').value="";
        document.getElementById('TextBox2').value="";
        return false;
        }
    
      <asp:Button ID="Button2" runat="server" Text="Reset" OnClientClick="return reset()" />
    still the button2 click causes the postback

  24. #24

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Why is this simple code not working?

    Quote Originally Posted by gep13 View Post
    What browser are you using for debugging your application?
    Gary
    IE7

    Quote Originally Posted by gep13 View Post
    These JavaScript errors should be getting shown to you.
    Gary
    how to see the errors?

  25. #25
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Why is this simple code not working?

    Quote Originally Posted by HowTo View Post
    how to see the errors?
    http://www.testingreflections.com/node/view/4009

  26. #26
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Why is this simple code not working?

    Quote Originally Posted by HowTo View Post
    still the button2 click causes the postback
    This:

    Code:
    <head runat="server">
        <title></title>
        <script language="javascript" type="text/javascript">
            function HowToreset() {
                return false;
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Reset" OnClientClick="return HowToreset()" />
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </div>
        </form>
    </body>
    Does not cause a PostBack.

    i.e. watch the names that you are using for your functions.

    Gary

  27. #27
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Why is this simple code not working?

    Also, you might want to think about using a standing html input element, rather than a server button, if you are not actually doing anything on the server side.

    Gary

  28. #28

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Why is this simple code not working?

    Quote Originally Posted by gep13 View Post
    watch the names that you are using for your functions.

    Gary
    i cant follow what you tried to mean with the function names

  29. #29
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Why is this simple code not working?

    If you are are you simply want to Reset the values then Consider using the HTML Reset button

    Code:
    <input id="Reset1" type="reset"
                    value="reset" />
    Please mark you thread resolved using the Thread Tools as shown

  30. #30
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Why is this simple code not working?

    Just Found the Problem. The Function name was causing the Problem..

    Change the function name...
    It works


    Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Untitled Page</title>
    
          <script language="javascript" type="text/javascript">
        function resets()
        {
        document.getElementById('TextBox1').value="";
        document.getElementById('TextBox2').value="";
        return true;
        }
        </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Label ID="Label1" runat="server" Text="UserId :"></asp:Label>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <br />
                <asp:Label ID="Label2" runat="server" Text="Password :"></asp:Label>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                <br />
                <asp:Button ID="Button2" runat="server" Text="Reset" OnClientClick="return resets();" />
            </div>
        </form>
      
    </body>
    </html>
    Please mark you thread resolved using the Thread Tools as shown

  31. #31
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Why is this simple code not working?

    Quote Originally Posted by danasegarane View Post
    Just Found the Problem. The Function name was causing the Problem..

    Change the function name...
    It works
    That's what I said

    Gary

  32. #32
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Why is this simple code not working?

    Quote Originally Posted by HowTo View Post
    i cant follow what you tried to mean with the function names
    You have a JavaScript function named "reset". This must be some form of reserved word in JavaScript, as when you use it, it does not function the way it should. This was shown in your other thread where you were using a function named "open".

    In my case, I renamed your function to "HowToreset", and Dana renamed it to be "resets", in both cases, this made the function start working as expected.

    Gary

  33. #33
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Why is this simple code not working?

    Quote Originally Posted by gep13 View Post
    That's what I said

    Gary
    Let's wait for the reply
    Please mark you thread resolved using the Thread Tools as shown

  34. #34

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Why is this simple code not working?

    Now it works

  35. #35

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Why is this simple code not working?

    i need a suggestion:I am working hard to learn javascript since after this i want to go for the ajax........so is it a good idea to learn javascript before going for the ajax?

  36. #36
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Why is this simple code not working?

    Yes, I would say that it is essential, yes.

    You will like need to become familiar in both JavaScript and jQuery.

    Gary

  37. #37

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: Why is this simple code not working?

    what is jQuery all about?how to use it in asp.net?

  38. #38

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width