Select Dinamiche in fase di modifica

Fulvio59

Nuovo Utente
3 Gen 2009
24
0
1
Salve a tutti,

devo gestire delle select dinamiche a tre livelli e ho qualche problema non in fase di inserimento ma in fase di modifica.
Mi spiego meglio.
Ho trovato del codice sulla rete che ho adattato alle mie esigenze.

Il tutto è composto da 3 script:
script di modifica "modifica.asp" contenente l'evento "CaricaPagina" attivato dal tag BODY
script richiamato dall'evento di cui sopra "upddynamic.asp" contenente una classe
script che sviluppa la classe "class_dinamic_select.asp"


MODIFICA.ASP

<%@LANGUAGE="VBSCRIPT"%>

<HTML>
<HEAD>
<title>Modifica Riparazione</title>
<link rel="stylesheet" href="../css/base.css">
<script type="text/javascript" language="javascript">
url = "/lavorazioni/";
d = document;
preloadText = "Generazione lista...";
function caricaPagina(pagina,idLayer)
{
if (d.getElementById) {var x=(window.ActiveXObject)?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest();}
if (x)
{
d.getElementById(idLayer).innerHTML = preloadText;
x.onreadystatechange = function()
{
if (x.readyState == 4 && x.status == 200)
{
el=d.getElementById(idLayer);el.innerHTML = x.responseText;
}
}
x.open("GET", url + pagina, true);
x.send(null);
}
}
function checkForm()
{
var ilForm = d.forms[0];
var err = true;
var lRoot = document.getElementById("ErrorRootcauses");
var lResp = document.getElementById("ErrorResponsabilita");
var lDiff = document.getElementById("ErrorDifetto");
lRoot.style.display = "none";lResp.style.display= "none";lDiff.style.display="none";
if (ilForm.rootcauses.value == "")
{
lRoot.style.display = "block";lRoot.innerHTML = "E' necessario selezionare una rootcause";
err = false;
}
if (ilForm.responsabilita.value == "")
{
lResp.style.display = "block";lResp.innerHTML= "E' necessario selezionare una responsabilita.";
err = false;
}
if (ilForm.difetto.value == "")
{
lDiff.style.display = "block";lDiff.innerHTML= "E' necessario selezionare un difetto.";
err = false;
}
return err;
}

</script>

</head>
<body onload="caricaPagina('upddynamic.asp?type=rootcauses&idrip=<%=Request.querystring("idriparazione")%>)','ID_rootcauses');">

<!-- Inizio Corpo -->

<%

If request.QueryString("save") = "yes" then


.....SCRIVI SU DB

else

dim cn
dim sql

codicereparto=Request.QueryString("idreparto")
id=Request.querystring("idriparazione")
sql="Select * from V_DettaglioRiparazioni where `id riparazione`=" & id
set cn= Server.CreateObject("ADODB.Connection")
cn.Open objconn
set rs = cn.Execute (sql)
IF RS.EOF THEN
Session("testo")="Nessuna occorrenza trovata."
Response.redirect "../../script/messaggio.asp"
ELSE
fornitore=rs(10)
codsint=rs(11)
refdesignator=rs(12)
rootcauses=rs(13)
responsabilita=rs(17)
difetto=rs(14)
datasis=DataToString(rs(3))

%>


<br>
<form action="updriparazioni.asp?save=<%="yes"%>&amp;idriparazione=<%=id%>&amp;iddifetto=<%=iddifetto%>" method="post" name="srv">
<div style="margin-left:1px ">Id Riparazione:
<input type="text" readonly="readonly" maxlength=10 size=10 value="<%=rs(0)%>" style="font-family:tahoma; font-size:11px; margin-right:3px"

name="idriparazione" onblur="this.value=this.value.replace(/^\s*/,'')">
Id Utente:
<input type="text" readonly="readonly" maxlength=10 size=10 value="<%=rs(1)%>" style="font-family:tahoma; font-size:11px; margin-right:3px" name="idutente"

onblur="this.value=this.value.replace(/^\s*/,'')">
Id Reparto:
<input type="text" readonly="readonly" maxlength=10 size=10 value="<%=rs(2)%>" style="font-family:tahoma; font-size:11px; margin-right:3px" name="idreparto"

onblur="this.value=this.value.replace(/^\s*/,'')">
Data:
<input type="text" maxlength=10 size=10 value="<%=datasis%>" style="font-family:tahoma; font-size:11px; margin-right:3px" name="data"

onblur="this.value=this.value.replace(/^\s*/,'')">
Ora:
<input type="text" maxlength=8 size=8 value="<%=rs(4)%>" style="font-family:tahoma; font-size:11px; margin-right:3px" name="ora"

onblur="this.value=this.value.replace(/^\s*/,'')">
</div>
<br>
<div style="height:1px; background-image:url(../immagini/dot1.jpg); margin-left:10px; margin-right:10px "><img src="../immagini/spacer.gif"></div>
<br>
Fornitore:
<select size=1 style="font-family:tahoma; font-size:11px; margin-right:3px" name="fornitore">
<%
dim RSForn, idforn
'forn=rs(10)
set RSForn = Server.CreateObject("ADODB.RECORDSET")
RSForn.ActiveConnection=Objconn
sql=("Select * from T_FORNITORI")
RSForn.Open sql
Response.Write("<option selected>" & fornitore & "</option>")
while not RSForn.EOF
if ucase(fornitore) <> ucase((RSForn(2))) then
Response.Write "<option value=" & trim(RSForn(0)) & ">" & RSForn(1) & "</option>" & vbcrlf
end if
RSForn.movenext
wend
RSForn.close
set RSForn=nothing

%>
</select>
&nbsp;&nbsp;Ref Designator:
<input type="text" maxlength=18 size=18 value="<%=refdesignator%>" style="font-family:tahoma; font-size:11px; margin-right:3px" name="refdes">
</div><br>
<div style="margin-left:1px ">
<div id="FormError"></div>
<table border="0" cellpadding="0" cellspacing="0" style="WIDTH: 100%;">
<tr><td colspan="2" style="TEXT-ALIGN: left"><div id="ErrorRootcauses"></td>
<tr colspan="6" >
<td style=" WIDTH: 10%; TEXT-ALIGN: left">Root&nbsp;Causes:&nbsp;</td>
<td style="WIDTH: 28%; TEXT-ALIGN: left">
<div id="ID_rootcauses">

<select name="rootcauses" style="WIDTH: 40%" ><option value="" selected>seleziona una rootcause</option></select>
</div>
</td>
<td><div id="ErrorResponsabilita"></td>
<td style="margin-right:5px WIDTH: 4%; TEXT-ALIGN: left" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Responsabilita:&nbsp;</td>
<td style="WIDTH: 32%; TEXT-ALIGN: left">
<DIV id=ID_responsabilita>
<select name="responsabilita" style="WIDTH: 70%" ><option value="" selected>seleziona una responsabilita</option></select>
</div>
</td>
<td><div id="ErrorDifetto"></td>
<td style="margin-left:1px WIDTH: 4%; TEXT-ALIGN: right">Difetto:&nbsp;</td>
<td style="WIDTH: 25%; TEXT-ALIGN: left">
<DIV id=ID_difetto>
<select name="difetto" style="WIDTH: 73%" ><option value="" selected>seleziona un difetto</option></select>
</div>
</td>
</TR>
</table>
<br>
</div>
<br>
<p>
<table width="98%" border="0" cellspacing="0" cellpadding="0">
<br>
<div style="height:1px; background-image:url(../immagini/dot1.jpg); margin-left:10px; margin-right:10px "><img src="../immagini/spacer.gif"></div>
<br><br>
<div style="margin-left:355px ">
<INPUT type="button" value="modifica" name="b1" onclick="Controlla()">&nbsp;&nbsp;<input type="Reset" Value="annulla" id=Reset1 name=Reset1>
</div>
</table>
</p>
</form>

<%


End If
end if


%>

</body>
</html>



UPDDYNAMIC.ASP


<!--#include file="class_dinamic_select.asp"-->
<%
value = replace(request("value")&"", "'", "'")
fieldtype = request("type")
set ds = new DinamicSelect
ds.DataBase = "DB



if fieldtype = "rootcauses" then
' ROOT CAUSES
ds.DefaultText = "seleziona una root causes"
ds.sql = "select distinct descroot from T_RESPONSABILITA_NEW"
ds.SelectValue = "descroot"
ds.SelectText = "descroot"
ds.evento = " onchange=""caricaPagina('upddynamic.asp?type=responsabilita&amp;value=' " &_
"+ this.options[this.options.selectedIndex].value,'ID_responsabilita');"""
ds.NomeCampo = "rootcauses"
end if



' RESPONSABILITA'
if fieldtype = "responsabilita" then
ds.DefaultText = "seleziona una responsabilita"
ds.sql = "select * from T_RESPONSABILITA_NEW where descroot='"& value&"'"
ds.selected = value
ds.SelectValue = "id"
ds.SelectText = "descrizione"
ds.evento = " onchange=""caricaPagina('upddynamic.asp?type=difetto&amp;value='+ " &_
"this.options[this.options.selectedIndex].value,'ID_difetto');"""
ds.NomeCampo = "responsabilita"
end if


'DIFETTO
if fieldtype = "difetto" then
ds.DefaultText = "seleziona un difetto"
ds.sql = "select descrizione from T_DIFETTI_NEW where `id responsabilita` = '"&value&"'"
ds.selected = value
ds.SelectValue = "descrizione"
ds.SelectText = "descrizione"
ds.evento = ""
ds.NomeCampo = "difetto"
end if
Response.Write (ds.CreaDinamicSelect())
set ds = nothing
%>



CLASS_DYNAMIC_SELECT.ASP


<%

Class DinamicSelect
Public SelectText 'impostare il nome del campo del database che verrà visualizzato nella lista
Public SelectValue 'impostare il nome del campo del database che verrà passato come valore della lista
Public Selected 'impostare la voce che verrà visualizzata nella lista al caricamento di pagina
Public DataBase 'impostare l'url del database
Public sql 'impostare la query per estrarre i dati
Public Evento 'impostare l'evento che provvederà al submit "parziale" del form
Public NomeCampo 'impostare il nome del campo (nell'ambito del form)
Public DefaultText 'impostare il valore di default per le select (nell'ambito del form)

Public Function CreaDinamicSelect()
'creazione e apertura della connessione
tmpString = ""
Set conn = Server.createobject("adodb.connection")
conn.open = "DSN=nomedb"
Server.MapPath(me.DataBase)
tmpString = tmpString & "<select style=""width:100%"" name=""" & me.NomeCampo & """" & me.Evento & """>" & vbcrlf
tmpString = tmpString & "<option value="""">" & Me.DefaultText & "</option>" & vbcrlf
'creazione del recordset
set rs = Server.Createobject("adodb.recordset")
'apertura del recordset
rs.open me.sql,conn,3,3
do until rs.eof
tmpString = tmpString & "<option value=""" & Trim(rs(me.SelectValue)) & """"
if rs(me.SelectValue) = me.selected then
tmpString = tmpString & "selected = ""selected"""
end if
tmpString = tmpString & ">" & Trim(UCase(rs(me.SelectText))) & "</option>" & vbcrlf
rs.movenext
loop
tmpString = tmpString &"</select>"& vbcrlf
'chiusura e distruzione del recordset
rs.close
set rs = nothing
'chiusura e distruzione della connessione
conn.close
set conn = nothing
CreaDinamicSelect = tmpString
End function
End Class
%>





Ora il mio problema è che, in fase di modifica, vorrei visualizzare nelle select come prima opzione i dati presenti nel db e poi riempire le select con i restanti dati.
Pero quando l'evento onload del tag body richiama lo script upddynamic.asp, i dati del form non sono ancora visualizzati e quindi come faccio?
L'unico dato che ho al momento di attivazione dell'evento è un identificativo che mi permetterebbe di recuperare quei dati rileggendo la tabella.
Mi sembra però una lettura ridondante e la vorrei evitare.
Esiste un modo diverso per riempire il valore di default delle select con il dato del db?

Grazie in anticipo
 
Ho risolto

posto le modifiche

All'inizio dello script modifica.asp ho inserito la seguente lettura

id=Request.querystring("idriparazione")
sqlupd="select distinct `codice difetto`, responsabilita, `codice difetto1` from T_DETTAGLIORIPARAZIONI where `id riparazione`= " & id
set cnupd= Server.CreateObject("ADODB.Connection")
cnupd.Open objconn
set rsupd = cnupd.Execute (sqlupd)

IF rsupd.EOF THEN
Session("testo")="Nessuna occorrenza trovata."
Response.redirect "../../script/messaggio.asp"
ELSE
root=rsupd(0)
res=rsupd(1)
dif=rsupd(2)
end if


ho aggiunto delle querystring all'evento ONLOAD...

<body onload="caricaPagina('upddynamic.asp?type=rootcauses&idrip=<%=Request.querystring("idriparazione")%>&root=<%=root%>&res=<%=Res%>&dif=<%=dif%>','ID_rootcauses');">

infine ho modificato le select nel form come segue...

<div style="margin-left:1px ">
<div id="FormError"></div>
<table border="0" cellpadding="0" cellspacing="0" style="WIDTH: 100%;">
<tr><td colspan="2" style="TEXT-ALIGN: left"><div id="ErrorRootcauses"></td>
<tr colspan="6" >
<td style=" WIDTH: 10%; TEXT-ALIGN: left">Root&nbsp;Causes:&nbsp;</td>
<td style="WIDTH: 28%; TEXT-ALIGN: left">
<div id="ID_rootcauses">
<select name="rootcauses" style="WIDTH: 40%" ><option value="" selected>seleziona una rootcause</option></select>
</div>
</td>
<td><div id="ErrorResponsabilita"></td>
<td style="margin-right:5px WIDTH: 4%; TEXT-ALIGN: left" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Responsabilita:&nbsp;</td>
<td style="WIDTH: 32%; TEXT-ALIGN: left">
<DIV id=ID_responsabilita>
<select name="responsabilita" style="WIDTH: 70%" ><option selected="selected" value="<%=responsabilita%>"><%=responsabilita%></option></select>

</div>
</td>
<td><div id="ErrorDifetto"></td>
<td style="margin-left:1px WIDTH: 4%; TEXT-ALIGN: right">Difetto:&nbsp;</td>
<td style="WIDTH: 25%; TEXT-ALIGN: left">
<DIV id=ID_difetto>
<select name="difetto" style="WIDTH: 73%%" ><option selected="selected" value="<%=difetto%>"><%=difetto%></option></select>
</div>
</td>
</TR>
</table>
<br>
</div>



Lo script upddynamic.asp è stato modificato come segue...

<!--#include file="class_dinamic_select.asp"-->
<%
value = replace(request("value")&"", "'", "&#39")
fieldtype = request("type")
idrip = request("idrip")
set ds = new DinamicSelect
rootcauses = request("root")
responsabilita = request("res")
difetto = request("dif")

ds.DataBase = "Powerone"


if fieldtype = "rootcauses" then
' ROOT CAUSES
ds.DefaultText = rootcauses
ds.sql = "select distinct descroot from T_RESPONSABILITA_NEW"
ds.SelectValue = "descroot"
ds.SelectText = "descroot"
ds.evento = " onchange=""caricaPagina('upddynamic.asp?type=responsabilita&amp;value=' " &_
"+ this.options[this.options.selectedIndex].value,'ID_responsabilita');"""

ds.NomeCampo = "rootcauses"
end if



' RESPONSABILITA'
if fieldtype = "responsabilita" then
ds.DefaultText = responsabilita
ds.sql = "select * from T_RESPONSABILITA_NEW where descroot='"& value&"'"
ds.selected = value
ds.SelectValue = "id"
ds.SelectText = "descrizione"
ds.evento = " onchange=""caricaPagina('upddynamic.asp?type=difetto&amp;value='+ " &_
"this.options[this.options.selectedIndex].value,'ID_difetto');"""
ds.NomeCampo = "responsabilita"
end if


'DIFETTO
if fieldtype = "difetto" then
ds.DefaultText = difetto
ds.sql = "select descrizione from T_DIFETTI_NEW where `id responsabilita` = '"& value&"'"
ds.selected = value
ds.SelectValue = "descrizione"
ds.SelectText = "descrizione"
ds.evento = ""
ds.NomeCampo = "difetto"
end if
Response.Write (ds.CreaDinamicSelect())
set ds = nothing



%>
 

Discussioni simili