Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 23rd, 2004, 02:14 PM
Authorized User
 
Join Date: Jan 2004
Location: , , .
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Default Error Handling in JavaScript

Can some one please copy and past this in an asp page so you can know what I'm talking about. Basically it is probally very simple. I want the Jscript function to produce an alert() if no records were found in the recordset below. The function works fine if there are records. Basically what it does it calls another page based on the selection you user clicks. I already incorporated a message display that shows no records found to the user. but if they hit the button it will call the function which will produce the error. I could either hide the button if no records are found, but how do i do that??? Or I can use something in the Jscript function to produce an alert(). Please Help!





<script ID="clientEventHandlersJS" LANGUAGE="javascript">
<!--
//
function capt(field)
{

    var m = 0;


    {
        for (i=0;i < field.length; i++)
        {
            campo = field[i].checked;
            if(campo == true)
            {
                window.location.href="../CatLPMaestro/LPSustituidosDetalle.asp?PS_Id=" + field[i].value;

                m=m+1;
            }
        }

        if (m==0)
        {
            campo=field.checked;
            if(campo == true)
            {
                window.location.href= "../CatLPMaestro/LPSustituidosDetalle.asp?PS_Id=" + field.value;
            }
        else
                {
                    alert("Antes de ejecutar esta acción debe seleccionar una de las opciones listadas");
                }


        }
    }
}
//-->
</script>


<%

LPSustituidos = "SELECT * FROM VINVLP_Sustituidos_List WHERE (PsID_Id = " & (Request.QueryString("Ps_Id")) & ")"
Set rsLPSustituidos = Server.CreateObject("ADODB.Recordset")
rsLPSustituidos.Open LPSustituidos, conn, adOpenStatic, adLockOptimistic
'ON ERROR RESUME NEXT
%>

 <table WIDTH="100%" BORDER="0" CELLSPACING="1" CELLPADDING="1" align="center">



                            <tr>
                            <td colspan="10" align="lEFT" class="Detalle">
                            <input type="submit" value="Detalle" class="boton120" name="Delpart" onClick="capt(list)">
                            </tr>
                            <tr>
                                <td class="Detalle" align="center"></td>

                                <td class="GridTitulo" align="center">Codigo de Fabricante</td>
                                <td class="GridTitulo" align="center">Codigo Interno</td>
                                <td class="GridTitulo" align="center">Familia</td>
                                <td class="GridTitulo" align="center">Sub-Familia</td>
                                <td class="GridTitulo" align="center">Estatus de Vida</td>
                                <td class="GridTitulo" align="center">Estatus de Venta</td>
                                <td class="GridTitulo" align="center">Precio de Venta</td>
                                <td class="GridTitulo" align="center">Contribucíon</td>





                            </tr>

                            <%If rsLPSustituidos.BOF And rsLPSustituidos.EOF Then%>
                            <tr>
                                <td class="DetalleDato" align="center" colspan="11">NO RECORDS FOUND FOR THIS PRODUCT.</td>
                            </tr>
                            <%Else

                            Do While Not rsLPSustituidos.EOF%>



                            <tr>
                                <td class="Detalle" align="center"><input class="GridCheckbox" type="Radio" id="list" name="list" value="<%=rsLPSustituidos("Ps_Id")%>" tabindex="22" maxlength="0"></td>

                                <td class="Griddato" align="center" valign="top"><%=rsLPSustituidos.Fields("Ps_CodFab" )%></td>
                                <td class="Griddato" align="center" valign="top"><%=rsLPSustituidos.Fields("Ps_CodInt" )%></td>
                                <td class="Griddato" align="center" valign="top"><%=rsLPSustituidos.Fields("Fam_Nom")% ></td>
                                <td class="Griddato" align="center" valign="top"><%=rsLPSustituidos.Fields("SubFam_Nom ")%></td>
                                <td class="Griddato" align="center" valign="top"><%=rsLPSustituidos.Fields("StatVda")% ></td>
                                <td class="Griddato" align="center" valign="top"><%=rsLPSustituidos.Fields("StatVta")% ></td>
                                <td class="Griddato" align="center" valign="top"><%=rsLPSustituidos.Fields("Ps_PreVta" )%></td>
                                <td class="Griddato" align="center" valign="top"><%=rsLPSustituidos.Fields("Ps_Contrib ")%></td>







                            </tr>
                            <%
                            i = i +1
                            MiCont = MiCont + 1
                            rsLPSustituidos.MoveNext
                            Loop
                            End if%>
<%
rsLPSustituidos.Close
set reLPSustituidos = nothing
%>

  #2 (permalink)  
Old January 23rd, 2004, 02:18 PM
Authorized User
 
Join Date: Jan 2004
Location: , , .
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I hope I didnt confuse anyone. Everythin works fine execpt if there is no recoreds and they hit the button the jscript function will produce an error. This is what I want to correct.

  #3 (permalink)  
Old January 23rd, 2004, 02:40 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You could simplify to this.
Code:
function capt(fields)
{
  for (var i = 0; i < fields.length; i++)
  {
    var oField = fields[i];
    if (oField.checked)
    {
      window.location.href="../CatLPMaestro/LPSustituidosDetalle.asp?PS_Id=" + oField.value;
      return;  
    }  
  }
  alert("Antes de ejecutar esta acción debe seleccionar una de las opciones listadas");  
}
A word of warning, all elements are supposed to have different IDs. Although some browsers allow you to give them the same ones and treat it as a collection a compliant one won't and your code would crash. Also only IE lets you pass an id an element reference
Give them the same name and either no id or an id that is individual such as
Code:
id="list<% = i %>"
. Then modify call to:
Code:
onClick="capt(document.getElementsByName('list'));"
--

Joe
  #4 (permalink)  
Old January 23rd, 2004, 02:57 PM
Authorized User
 
Join Date: Jan 2004
Location: , , .
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply. Sorry I didnt traslate this in english. The alert in the function is for another validation

{
                    alert("Antes de ejecutar esta acción debe seleccionar una de las opciones listadas");
                }

this just tells the user that he must check an option in the gridbox. I want if the user hits the button and there is no record to pop up an alert of some kind.

  #5 (permalink)  
Old January 23rd, 2004, 07:10 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I see, in that case you can add a couple of lines to the top.
If you use your original code, then it's more difficult because if there are no records list will be null, if only one record then it won't be a collection and the code would fail. If you use my suggestion of document.getElementsByName then the following will work :
Code:
function capt(fields)
{
var iLength = fields.length;
if (iLength == 0)
{
alert("Ningunos expedientes encontrados.");
return;
}  
for (var i = 0; i < iLength; i++)
  {
    var oField = fields[i];
    if (oField.checked)
    {
      window.location.href="../CatLPMaestro/LPSustituidosDetalle.asp?PS_Id=" + oField.value;
      return;  
    }  
  }
  alert("Antes de ejecutar esta acción debe seleccionar una de las opciones listadas");  
}

--

Joe


Similar Threads
Thread Thread Starter Forum Replies Last Post
error handling prv299 .NET Web Services 5 May 19th, 2008 07:27 AM
Error Handling evad Excel VBA 2 August 18th, 2005 03:54 AM
Error Handling twbit BOOK: ASP.NET Website Programming Problem-Design-Solution 1 May 3rd, 2005 09:18 PM
Error Handling Louisa VB.NET 2002/2003 Basics 1 May 13th, 2004 10:22 AM
Error handling ! ittorget Classic ASP Professional 2 November 14th, 2003 09:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.