Reputation: 85
i'm working on a web project with asp.net and VB.Net. I have inside a loop à javascript code let's say it's :
Do While Not oActivite.eof
strScript = "<script type='text/javascript'>"
strScript = strScript + "alert(" + strActiviteId + ");"
strScript = strScript + "</script>"
Loop
ScriptManager.RegisterStartupScript(Me.Page, GetType(String), "tmp", strScript, False)
strScript is a string And strActiviteId is a variable which value change in every loop.
When the page loads, i only get an alert with the first value of strActiviteId. what i want is to get :
Alert(strActiviteIdFirstLoop);
Alert(strActiviteIdSecondtLoop);
...
But the problém is more than this, i have a bigger script instead of the alert, a jQuery script that adds a div containing a span and an input in front of a td.
and what i want is append to the div id the strActiviteId variable, before i would get divs with the same strActiviteId value in the id , but when i did what you told me, now i get three divs in front of each checkbox.
this is the jQuery code i put in the strScript :
$("#ctl00_indexBody_ID_ACTIVITE tbody tr td").after("
<span id='ctl00_indexBody_label1' >Tarif : </span>
<input type='TEXT'
onchange='ContenuModifier();'
style='width: 270px;' id='N_PRIX'
name='N_PRIX' onfocus='BeginSaisie(this,'99999')'
onblur='EndSaisie(this,'99999')'
onkeypress=';CtrlSaisie(event,this,'99999')' maxlength='5'
class='user_access_email bootstrapped-input input-text input-block-level input-xlarge'>
");
I hope this is understandable, thank you for your help.
Ps : eof is a built in method that returns false when there is no data in oActivite which is an object i populated earlier.
Upvotes: 0
Views: 642
Reputation: 37710
The first line :
strScript = "<script type='text/javascript'>"
resets the script value in each loop. And you register the script only after the do while loop. Only the last script value will be rendered.
Try this:
strScript = "<script type='text/javascript'>"
Do While Not oActivite.eof
strScript += "alert(" + strActiviteId + ");"
Loop
strScript += "</script>"
ScriptManager.RegisterStartupScript(Me.Page, GetType(String), "tmp", strScript, False)
That said, you should consider using a StringBuilder depending on the loop count, to reduce string instantiation.
Upvotes: 1