[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Calling javascript in for loop in code behind

Posted on 2009-02-19
6
Medium Priority
?
848 Views
Last Modified: 2012-05-06
Hi

i am using C# , in that i want to call a javascript function in for loop code behind ,i used RegisterClientScriptBlock and Page.ClientScript.RegisterClientScriptBlock, this will excute only once , i want to excute this function each time in a loop,please help me....
function setImageRate()
{
var rating=document.getElementById('hdnrate');
alert(rating.value);
 
var ctr1=document.getElementById('Img1');
var ctr2=document.getElementById('Img2');
var ctr3=document.getElementById('Img3');
var ctr4=document.getElementById('Img4');
var ctr5=document.getElementById('Img5');
  
        if (rating.value >= 1)
        {
            ctr1.src = "images/star.gif";
        }
        else if (rating.value < 1 && rating.value > 0)
        {
            ctr1.src = "images/star_disable.gif";
        }
        if (rating.value >= 2)
        {
            ctr2.src = "images/star.gif";
        }
        else if (rating.value < 2 && rating.value > 1)
        {
            ctr2.src = "images/star_disable.gif";
        }
        if (rating.value >= 3)
        {
            ctr3.src = "images/star.gif";
        }
        else if (rating.value < 3 && rating.value > 2)
        {     
            ctr3.src = "images/star_disable.gif";
        }
        if (rating.value >= 4)
        {
            ctr4.src = "images/star.gif";
        }
        else if (rating.value < 4 && rating.value > 3)
        {
            ctr4.src = "images/star_disable.gif";
        }
        if (rating.value >= 5)
        {
            ctr5.src = "images/star.gif";
        }
        else if (rating.value < 5 && rating.value > 4)
        {
            ctr5.src = "images/star_disable.gif";
        }
}
 
 
 
i called this function
Page.RegisterClientScriptBlock("MyScript", "<script>setImageRate()</script>");

Open in new window

0
Comment
Question by:ninusajil
  • 2
  • 2
  • 2
6 Comments
 
LVL 9

Expert Comment

by:Ken Fayal
ID: 23679865
Need help understanding your needs.  What do you mean by "i want to excute this function each time in a loop"

What is "each time"?  Each time the page loads, each time someone clicks a button???
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 23679870
Hello ninusajil,

Client-side and server-side script are two different beasts. You cannot simply call a client-side script (in any fashion) inside your server-side code. Your registerclientscriptblock will simply place this script onto the page to be executed when the page is rendered client-side. This happens *after* all your server-side script as completed.

It is quite difficult to suggest what you should do here as we don't know what it is that you are really trying to achieve.

Regards,

TimCottee
0
 

Author Comment

by:ninusajil
ID: 23679985
through for loop iam building a table, for each row , in a column i want to change images, for that i wrote that javascript, when i execute that javascript it only once excuting and working in first row

for(int i=0;i<=15;i++)
{
in this i want to call that javascript
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 43

Accepted Solution

by:
TimCottee earned 150 total points
ID: 23680005
ninusajil,

You can't! That is what I tried to explain, you cannot call javascript (which is executed on the client) within your loop executed on the server.

If you are building a table and from what I see here it has star ratings that you appear to be trying to set. Why cannot you simply set these on the server side as presumably you know what value and hence what images to show.

By all means have a client-side function that allows the user to change or reset these ratings but it makes no sense to have a client-side function set them, especially as it is not possible to acchieve what you appear to be trying to do with a mix of client/server code.

TimCottee
0
 
LVL 9

Expert Comment

by:Ken Fayal
ID: 23680014
Oh.. I see what you are doing.  The problem is that it's running so fast that you only see the images as they were in the last iteration of the loop.  If you actually want to see them changing you have to put a delay in the for loop.  Is that what you mean?  You want to actually "see" these images changing?  If so, then you can still use your javascript, but you don't have to register the script with ClientScriptManager.  You can just have it launch when the user clicks a button or when the page loads.
0
 

Author Comment

by:ninusajil
ID: 23680024
ok, i tried to do like this, anyway thanks for ur advice
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question