Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > javascript not working

Reply
Thread Tools

javascript not working

 
 
tshad
Guest
Posts: n/a
 
      05-08-2009
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?


 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      05-08-2009
On May 8, 8:17*am, "tshad" <(E-Mail Removed)> 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 %>');
 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      05-08-2009
On May 8, 8:52*am, Alexey Smirnov <(E-Mail Removed)> wrote:
> On May 8, 8:17*am, "tshad" <(E-Mail Removed)> 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/libr....clientid.aspx
 
Reply With Quote
 
tshad
Guest
Posts: n/a
 
      05-08-2009

"Alexey Smirnov" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On May 8, 8:52 am, Alexey Smirnov <(E-Mail Removed)> wrote:
> On May 8, 8:17 am, "tshad" <(E-Mail Removed)> 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/libr....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



 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      05-08-2009
On May 8, 10:41*am, "tshad" <(E-Mail Removed)> wrote:
> "Alexey Smirnov" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
> On May 8, 8:52 am, Alexey Smirnov <(E-Mail Removed)> wrote:
>
>
>
> > On May 8, 8:17 am, "tshad" <(E-Mail Removed)> 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/libr...ontrol.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%>
 
Reply With Quote
 
Gran Andersson
Guest
Posts: n/a
 
      05-08-2009
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).

--
Gran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help: Javascript not working in Firefox "event not defined" Wiseman Javascript 1 07-25-2008 03:04 PM
Working with XML in JavaScript / XML Javascript'? noff Javascript 0 06-14-2007 12:46 PM
wifi not working on new hp, or not working after live update =?Utf-8?B?RHJhZ29ueA==?= Wireless Networking 1 10-01-2005 11:17 PM
Cookies working on intranet but NOT working on Internet Martin Heuckeroth ASP .Net 5 04-01-2005 01:37 AM
Regular Expression validators NOT working, Required Field validators ARE working Ratman ASP .Net 0 09-14-2004 09:36 PM



Advertisments