0

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.

3
  • there's no loop in your code. Please show the whole logic. The problem probably sits in the loop Commented Jun 18, 2015 at 10:19
  • Why do you use jQuery to build your markup? can't you simply use a <asp:Reapeater> ? Commented Jun 18, 2015 at 11:45
  • Because i want to give it the same id as the checkbox, and afterwards i want to hide it, and whenever the user checks the textbox appers in front of it. Commented Jun 18, 2015 at 11:54

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Im sorry if i look like i'm stalling you, but i asked the same question before and it was hard to answer, that's why i started with the alert example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.