RegisterClientScriptBlock not working
I want to implement javascript code dynamically but i am un able to. RegisterClientScriptBlock is not working
my java script code is
<script type="text/javascript">
var dynimages=new Array()
dynimages[0]=["photo1.jpg", "http://www.cnn.com"]
dynimages[1]=["photo2.jpg", "http://www.yahoo.com"]
dynimages[2]=["photo3.jpg", "http://www.google.com"]
var preloadimg="no"
var optlinktarget=""
var imgborderwidth=0
var filterstring="progid:DXImageTransform.Microsoft.GradientWipe(GradientSize=1.0 Duration=0.7)"
if (preloadimg=="yes"){
for (x=0; x<dynimages.length; x++){
var myimage=new Image()
myimage.src=dynimages[x][0]
}
}
function returnimgcode(theimg){
var imghtml=""
if (theimg[1]!="")
imghtml='<a href="'+theimg[1]+'" target="'+optlinktarget+'">'
imghtml+='<img src="'+theimg[0]+'" border="'+imgborderwidth+'">'
if (theimg[1]!="")
imghtml+='</a>'
return imghtml
}
function modifyimage(loadarea, imgindex){
if (document.getElementById){
var imgobj=document.getElementById(loadarea)
if (imgobj.filters && window.createPopup){
imgobj.style.filter=filterstring
imgobj.filters[0].Apply()
}
imgobj.innerHTML=returnimgcode(dynimages[imgindex])
if (imgobj.filters && window.createPopup)
imgobj.filters[0].Play()
return false
}
}
</script>
what i am trying to do is to create the array dynamically.Please help me what should i do
Re: RegisterClientScriptBlock not working
Hi and welcome,
It can help other people to respond if you state which version of the framework you are using.
To create the array dynamically you need to build up a string containing your javascript and then add that script to the page.
Code:
Page.ClientScript.RegisterStartupScript(GetType(), "MyStartUp", MyJavaScriptString , true);
Re: RegisterClientScriptBlock not working
It's simple, the only generated part seems to be the javascript array there, and you can loop and concatenate such a string before using fishcake's code to register it. For example, if you were getting it from a dataset...
Code:
string strJSArray = "";
int intCount = 0;
foreach(DataRow dr in ds.Tables[0].Rows)
{
strJSArray += "dynarray[" + intCount.ToString() + "]= ['" + dr[4].ToString() + "','" + dr[5].ToString() + "'];";
}
string strJS = @"<script type=""text/javascript"">
var dynimages=new Array()" +
strJSArray
+ @"var preloadimg=""no""
var optlinktarget=""""
var imgborderwidth=0
var filterstring=""progidXImageTransform.Microsoft.GradientWipe(GradientSize=1.0 Duration=0.7)""
if (preloadimg==""yes""){
for (x=0; x<dynimages.length; x++){
var myimage=new Image()
myimage.src=dynimages[x][0]
}
}
function returnimgcode(theimg){
var imghtml=""""
if (theimg[1]!="""")
imghtml='<a href=""'+theimg[1]+'"" target=""'+optlinktarget+'"">'
imghtml+='<img src=""'+theimg[0]+'"" border=""'+imgborderwidth+'"">'
if (theimg[1]!="""")
imghtml+='</a>'
return imghtml
}
function modifyimage(loadarea, imgindex){
if (document.getElementById){
var imgobj=document.getElementById(loadarea)
if (imgobj.filters && window.createPopup){
imgobj.style.filter=filterstring
imgobj.filters[0].Apply()
}
imgobj.innerHTML=returnimgcode(dynimages[imgindex])
if (imgobj.filters && window.createPopup)
imgobj.filters[0].Play()
return false
}
}
</script>";