javascript not working

Discussion in 'ASP .Net' started by tshad, May 8, 2009.

  1. tshad

    tshad Guest

    I have a control that I need to run some javascript on.

    This used to work in 1.1 and I am now on 3.5 and it doesn't seem to work.

    If I do:

    **********************************************
    <script type="text/javascript">
    function CheckSave() {
    var field1 = document.getElementById('txtSINInput');

    if (field1.value == "") <--------Error
    prompt("field1 is blank");
    if (field1 == null)
    prompt("field1 is null");

    if (!confirm('You have made change(s).\nDo you want to leave
    without saving?'))
    return true;
    }

    </script>
    *********************************************

    The javascript gets called fine, but I get an error that says

    "Microsoft Jscript runtime error: Object Required".

    The object txtSINInput is there:

    <asp:TextBox ID="txtSINInput" style="float:left;
    margin-right:5px;" runat="server"
    CssClass="TextBox" Width="80px" MaxLength="11"
    />

    This is on the same page.

    Why doesn't it find it?
    tshad, May 8, 2009
    #1
    1. Advertising

  2. On May 8, 8:17 am, "tshad" <> wrote:
    > I have a control that I need to run some javascript on.
    >
    > This used to work in 1.1 and I am now on 3.5 and it doesn't seem to work.
    >
    > If I do:
    >
    > **********************************************
    > <script type="text/javascript">
    >     function CheckSave() {
    >         var field1 = document.getElementById('txtSINInput');
    >
    >         if (field1.value == "")              <--------Error
    >             prompt("field1 is blank");
    >         if (field1 == null)
    >             prompt("field1 is null");
    >
    >             if (!confirm('You have made change(s).\nDo you want to leave
    > without saving?'))
    >         return true;
    >     }
    >
    > </script>
    > *********************************************
    >
    > The javascript gets called fine, but I get an error that says
    >
    > "Microsoft Jscript runtime error:  Object Required".
    >
    > The object txtSINInput is there:
    >
    >                           <asp:TextBox ID="txtSINInput" style="float:left;
    > margin-right:5px;"  runat="server"
    >                              CssClass="TextBox" Width="80px" MaxLength="11"
    > />
    >
    > This is on the same page.
    >
    > Why doesn't it find it?


    Because you cannot use ASP.NET control id in the client script. If you
    look at view source code in the browser, you will see that there is no
    control with id="txtSINInput". ASP.NET generates its own id.

    Use ClientId property to make it working.

    Example:

    var field1 = document.getElementById('<%= txtSINInput.ClientId %>');
    Alexey Smirnov, May 8, 2009
    #2
    1. Advertising

  3. On May 8, 8:52 am, Alexey Smirnov <> wrote:
    > On May 8, 8:17 am, "tshad" <> wrote:
    >
    >
    >
    > > I have a control that I need to run some javascript on.

    >
    > > This used to work in 1.1 and I am now on 3.5 and it doesn't seem to work.

    >
    > > If I do:

    >
    > > **********************************************
    > > <script type="text/javascript">
    > >     function CheckSave() {
    > >         var field1 = document.getElementById('txtSINInput');

    >
    > >         if (field1.value == "")              <--------Error
    > >             prompt("field1 is blank");
    > >         if (field1 == null)
    > >             prompt("field1 is null");

    >
    > >             if (!confirm('You have made change(s).\nDo you want to leave
    > > without saving?'))
    > >         return true;
    > >     }

    >
    > > </script>
    > > *********************************************

    >
    > > The javascript gets called fine, but I get an error that says

    >
    > > "Microsoft Jscript runtime error:  Object Required".

    >
    > > The object txtSINInput is there:

    >
    > >                           <asp:TextBox ID="txtSINInput" style="float:left;
    > > margin-right:5px;"  runat="server"
    > >                              CssClass="TextBox" Width="80px" MaxLength="11"
    > > />

    >
    > > This is on the same page.

    >
    > > Why doesn't it find it?

    >
    > Because you cannot use ASP.NET control id in the client script. If you
    > look at view source code in the browser, you will see that there is no
    > control with id="txtSINInput". ASP.NET generates its own id.
    >
    > Use ClientId property to make it working.
    >
    > Example:
    >
    > var field1 = document.getElementById('<%= txtSINInput.ClientId %>');


    Sorry, I mean ClientID
    http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx
    Alexey Smirnov, May 8, 2009
    #3
  4. tshad

    tshad Guest

    "Alexey Smirnov" <> wrote in message
    news:...
    On May 8, 8:52 am, Alexey Smirnov <> wrote:
    > On May 8, 8:17 am, "tshad" <> wrote:
    >
    >
    >
    > > I have a control that I need to run some javascript on.

    >
    > > This used to work in 1.1 and I am now on 3.5 and it doesn't seem to
    > > work.

    >
    > > If I do:

    >
    > > **********************************************
    > > <script type="text/javascript">
    > > function CheckSave() {
    > > var field1 = document.getElementById('txtSINInput');

    >
    > > if (field1.value == "") <--------Error
    > > prompt("field1 is blank");
    > > if (field1 == null)
    > > prompt("field1 is null");

    >
    > > if (!confirm('You have made change(s).\nDo you want to leave
    > > without saving?'))
    > > return true;
    > > }

    >
    > > </script>
    > > *********************************************

    >
    > > The javascript gets called fine, but I get an error that says

    >
    > > "Microsoft Jscript runtime error: Object Required".

    >
    > > The object txtSINInput is there:

    >
    > > <asp:TextBox ID="txtSINInput" style="float:left;
    > > margin-right:5px;" runat="server"
    > > CssClass="TextBox" Width="80px" MaxLength="11"
    > > />

    >
    > > This is on the same page.

    >
    > > Why doesn't it find it?

    >
    > Because you cannot use ASP.NET control id in the client script. If you
    > look at view source code in the browser, you will see that there is no
    > control with id="txtSINInput". ASP.NET generates its own id.
    >
    > Use ClientId property to make it working.
    >
    > Example:
    >
    > var field1 = document.getElementById('<%= txtSINInput.ClientId %>');


    >Sorry, I mean ClientID
    >http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx


    I did find out how to do (similar to your way) by using:

    function SetBirthDate(sender) {
    var field1 = $get('<%=txtBirthDateInput.ClientID%>');

    field1.value = sender.getSelectedDateFormatted();

    return true;
    }

    Not sure why my other code didn't work.

    Here is my code from one of my old pages from ASP.Net 1.1:

    *******************************************************
    <script language=javascript>
    function CheckSave()
    {
    var field1 = document.getElementById('CompanyName');
    var field2 = document.getElementById('Position');

    if ((field1.value != "") || (field2.value != ""))
    if(!confirm('You have made change(s).\nDo you want to leave without
    saving?'))
    return false;
    return true;
    }
    </script>
    *******************************************************

    Here I didn't have to worry about how .net changed the ID's.

    I assume that they changed how it worked in 3.5.

    Also, not sure why $get works. I just found that in another piece of code.

    Thanks,

    Tom
    tshad, May 8, 2009
    #4
  5. On May 8, 10:41 am, "tshad" <> wrote:
    > "Alexey Smirnov" <> wrote in message
    >
    > news:...
    > On May 8, 8:52 am, Alexey Smirnov <> wrote:
    >
    >
    >
    > > On May 8, 8:17 am, "tshad" <> wrote:

    >
    > > > I have a control that I need to run some javascript on.

    >
    > > > This used to work in 1.1 and I am now on 3.5 and it doesn't seem to
    > > > work.

    >
    > > > If I do:

    >
    > > > **********************************************
    > > > <script type="text/javascript">
    > > > function CheckSave() {
    > > > var field1 = document.getElementById('txtSINInput');

    >
    > > > if (field1.value == "") <--------Error
    > > > prompt("field1 is blank");
    > > > if (field1 == null)
    > > > prompt("field1 is null");

    >
    > > > if (!confirm('You have made change(s).\nDo you want to leave
    > > > without saving?'))
    > > > return true;
    > > > }

    >
    > > > </script>
    > > > *********************************************

    >
    > > > The javascript gets called fine, but I get an error that says

    >
    > > > "Microsoft Jscript runtime error: Object Required".

    >
    > > > The object txtSINInput is there:

    >
    > > > <asp:TextBox ID="txtSINInput" style="float:left;
    > > > margin-right:5px;" runat="server"
    > > > CssClass="TextBox" Width="80px" MaxLength="11"
    > > > />

    >
    > > > This is on the same page.

    >
    > > > Why doesn't it find it?

    >
    > > Because you cannot use ASP.NET control id in the client script. If you
    > > look at view source code in the browser, you will see that there is no
    > > control with id="txtSINInput". ASP.NET generates its own id.

    >
    > > Use ClientId property to make it working.

    >
    > > Example:

    >
    > > var field1 = document.getElementById('<%= txtSINInput.ClientId %>');
    > >Sorry, I mean ClientID
    > >http://msdn.microsoft.com/en-us/library/system.web.ui.control.clienti...

    >
    > I did find out how to do (similar to your way) by using:
    >
    >     function SetBirthDate(sender) {
    >         var field1 = $get('<%=txtBirthDateInput.ClientID%>');
    >
    >         field1.value = sender.getSelectedDateFormatted();
    >
    >         return true;
    >     }
    >
    > Not sure why my other code didn't work.
    >
    > Here is my code from one of my old pages from ASP.Net 1.1:
    >
    > *******************************************************
    > <script language=javascript>
    > function CheckSave()
    > {
    >   var field1 = document.getElementById('CompanyName');
    >   var field2 = document.getElementById('Position');
    >
    >  if ((field1.value != "") || (field2.value != ""))
    >   if(!confirm('You have made change(s).\nDo you want to leave without
    > saving?'))
    >    return false;
    >  return true;}
    >
    > </script>
    > *******************************************************
    >
    > Here I didn't have to worry about how .net changed the ID's.
    >
    > I assume that they changed how it worked in 3.5.
    >
    > Also, not sure why $get works.  I just found that in another piece of code.
    >
    > Thanks,
    >
    > Tom


    $get works because of ASP.NET "injection"

    <%=txtBirthDateInput.ClientID%>
    Alexey Smirnov, May 8, 2009
    #5
  6. tshad wrote:
    > Here I didn't have to worry about how .net changed the ID's.
    >
    > I assume that they changed how it worked in 3.5.


    No, it hasn't changed. It depends on where you have the controls. The id
    is only changed if the control is inside a naming container (e.g. a user
    control).

    --
    Göran Andersson
    _____
    http://www.guffa.com
    Göran Andersson, May 8, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ratman
    Replies:
    0
    Views:
    643
    Ratman
    Sep 14, 2004
  2. Martin Heuckeroth
    Replies:
    5
    Views:
    667
    JiangZemin
    Apr 1, 2005
  3. Alan Silver
    Replies:
    1
    Views:
    3,752
    Alan Silver
    Aug 2, 2005
  4. gaurav tyagi
    Replies:
    14
    Views:
    1,304
    gaurav tyagi
    Jan 20, 2006
  5. Wiseman
    Replies:
    1
    Views:
    422
    Henry
    Jul 25, 2008
Loading...

Share This Page