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

x
?
Solved

c# and javacode

Posted on 2011-10-13
11
Medium Priority
?
277 Views
Last Modified: 2012-05-12
hey guys i want to interact my c# code and java code.

i have a javascript and want to pass an array to my java script.

///  aspx

<div style="position: absolute; z-index: 1;">
<script type="text/javascript">
                 new fadeshow(fadeimages, 776, 224, 0, 3000, 1, "R")
</script>
  </div>

//// javascript

var fadeimages=new Array()
//SET IMAGE PATHS. Extend or contract array as needed
fadeimages[0]=["images/slideshow/1.jpg", "", ""] //plain image syntax
fadeimages[1]=["images/slideshow/Sunset.jpg", "", ""] //plain image syntax
fadeimages[2]=["images/slideshow/Water lilies.jpg", "", ""] //plain image syntax
 
var fadebgcolor="white"

////NO need to edit beyond here/////////////
 
var fadearray=new Array() //array to cache fadeshow instances
var fadeclear=new Array() //array to cache corresponding clearinterval pointers
 
var dom=(document.getElementById) //modern dom browsers
var iebrowser=document.all
 
function fadeshow(theimages, fadewidth, fadeheight, borderwidth, delay, pause, displayorder)
{
this.pausecheck=pause
this.mouseovercheck=0
this.delay=delay



///// C# code

    private string[] GetNextImageUrl()
    {
        string[] files = Directory.GetFiles(Server.MapPath("~/images/slideshow/"), "*.jpg");

        return files;
    }


how can i pass my list of file to a javascript?
0
Comment
Question by:JCWEBHOST
  • 5
  • 4
  • 2
11 Comments
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 1000 total points
ID: 36961206
You can create string from array. Assign string to hidden field. You can easily access hidden field (DOM object) in javascript/jquery
0
 
LVL 9

Expert Comment

by:dexterrajesh
ID: 36961210
hi,

you have two options.

1. You can use ClientScript.RegisterStartUpScript to inject javascript into the page
e.g.

List<string> tempString = new List<string>();   
tempString.Add("Hello");   
tempString.Add("World");    
StringBuilder sb = new StringBuilder();   
sb.Append("<script>");   
sb.Append("var testArray = new Array;");   
foreach(string str in tempString)   {     sb.Append("testArray.push('" + str + "');");   }   
sb.Append("</script>");    
ClientScript.RegisterStartupScript(this.GetType(), "TestArrayScript", sb.ToString()); 

Open in new window


use the array testArray in your corresponding javascript function

2. inline code

<%     int[] numbers = new int[5];     // Fill up numbers...      var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); %> somewhere later on the aspx page

<script type="text/javascript">     var jsVariable = <%= serializer.Serialize(numbers) %>; </script>

hope this helps
0
 

Author Comment

by:JCWEBHOST
ID: 36961229
i am lost?
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.

 

Author Comment

by:JCWEBHOST
ID: 36961244
i have a javascript


 
/***********************************************
* Ultimate Fade-In Slideshow (v1.51): © Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/
 
var fadeimages=new Array()
//SET IMAGE PATHS. Extend or contract array as needed
fadeimages[0]=["http://www.ramcon.jcwebhostza.com/images/slideshow/1.jpg", "", ""] //plain image syntax
fadeimages[1]=["http://www.ramcon.jcwebhostza.com/images/slideshow/Sunset.jpg", "", ""] //plain image syntax
fadeimages[2]=["http://www.ramcon.jcwebhostza.com/images/slideshow/Water lilies.jpg", "", ""] //plain image syntax 
 
var fadebgcolor="white"

////NO need to edit beyond here/////////////
 
var fadearray=new Array() //array to cache fadeshow instances
var fadeclear=new Array() //array to cache corresponding clearinterval pointers
 
var dom=(document.getElementById) //modern dom browsers
var iebrowser=document.all
 
function fadeshow(theimages, fadewidth, fadeheight, borderwidth, delay, pause, displayorder)
{
this.pausecheck=pause
this.mouseovercheck=0
this.delay=delay
this.degree=10 //initial opacity degree (10%)
this.curimageindex=0
this.nextimageindex=1
fadearray[fadearray.length]=this
this.slideshowid=fadearray.length-1
this.canvasbase="canvas"+this.slideshowid
this.curcanvas=this.canvasbase+"_0"
if (typeof displayorder!="undefined")
theimages.sort(function() {return 0.5 - Math.random();}) //thanks to Mike (aka Mwinter) :)
this.theimages=theimages
this.imageborder=parseInt(borderwidth)
this.postimages=new Array() //preload images
for (p=0;p<theimages.length;p++){
this.postimages[p]=new Image()
this.postimages[p].src=theimages[p][0]
}
 
var fadewidth=fadewidth+this.imageborder*2
var fadeheight=fadeheight+this.imageborder*2
 
if (iebrowser&&dom||dom) //if IE5+ or modern browsers (ie: Firefox)
document.write('<div id="master'+this.slideshowid+'" style="position:relative;width:'+fadewidth+'px;height:'+fadeheight+'px;overflow:hidden;"><div id="'+this.canvasbase+'_0" style="position:absolute;width:'+fadewidth+'px;height:'+fadeheight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);opacity:0.1;-moz-opacity:0.1;-khtml-opacity:0.1;background-color:'+fadebgcolor+'"></div><div id="'+this.canvasbase+'_1" style="position:absolute;width:'+fadewidth+'px;height:'+fadeheight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);opacity:0.1;-moz-opacity:0.1;-khtml-opacity:0.1;background-color:'+fadebgcolor+'"></div></div>')
else
document.write('<div><img name="defaultslide'+this.slideshowid+'" src="'+this.postimages[0].src+'"></div>')
 
if (iebrowser&&dom||dom) //if IE5+ or modern browsers such as Firefox
this.startit()
else{
this.curimageindex++
setInterval("fadearray["+this.slideshowid+"].rotateimage()", this.delay)
}
}

function fadepic(obj){
if (obj.degree<100){
obj.degree+=10
if (obj.tempobj.filters&&obj.tempobj.filters[0]){
if (typeof obj.tempobj.filters[0].opacity=="number") //if IE6+
obj.tempobj.filters[0].opacity=obj.degree
else //else if IE5.5-
obj.tempobj.style.filter="alpha(opacity="+obj.degree+")"
}
else if (obj.tempobj.style.MozOpacity)
obj.tempobj.style.MozOpacity=obj.degree/101
else if (obj.tempobj.style.KhtmlOpacity)
obj.tempobj.style.KhtmlOpacity=obj.degree/100
else if (obj.tempobj.style.opacity&&!obj.tempobj.filters)
obj.tempobj.style.opacity=obj.degree/101
}
else{
clearInterval(fadeclear[obj.slideshowid])
obj.nextcanvas=(obj.curcanvas==obj.canvasbase+"_0")? obj.canvasbase+"_0" : obj.canvasbase+"_1"
obj.tempobj=iebrowser? iebrowser[obj.nextcanvas] : document.getElementById(obj.nextcanvas)
obj.populateslide(obj.tempobj, obj.nextimageindex)
obj.nextimageindex=(obj.nextimageindex<obj.postimages.length-1)? obj.nextimageindex+1 : 0
setTimeout("fadearray["+obj.slideshowid+"].rotateimage()", obj.delay)
}
}
 
fadeshow.prototype.populateslide=function(picobj, picindex){
var slideHTML=""
if (this.theimages[picindex][1]!="") //if associated link exists for image
slideHTML='<a href="'+this.theimages[picindex][1]+'" target="'+this.theimages[picindex][2]+'">'
slideHTML+='<img src="'+this.postimages[picindex].src+'" border="'+this.imageborder+'px">'
if (this.theimages[picindex][1]!="") //if associated link exists for image
slideHTML+='</a>'
picobj.innerHTML=slideHTML
}
 
 
fadeshow.prototype.rotateimage=function(){
if (this.pausecheck==1) //if pause onMouseover enabled, cache object
var cacheobj=this
if (this.mouseovercheck==1)
setTimeout(function(){cacheobj.rotateimage()}, 100)
else if (iebrowser&&dom||dom){
this.resetit()
var crossobj=this.tempobj=iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas)
crossobj.style.zIndex++
fadeclear[this.slideshowid]=setInterval("fadepic(fadearray["+this.slideshowid+"])",50)
this.curcanvas=(this.curcanvas==this.canvasbase+"_0")? this.canvasbase+"_1" : this.canvasbase+"_0"
}
else{
var ns4imgobj=document.images['defaultslide'+this.slideshowid]
ns4imgobj.src=this.postimages[this.curimageindex].src
}
this.curimageindex=(this.curimageindex<this.postimages.length-1)? this.curimageindex+1 : 0
}
 
fadeshow.prototype.resetit=function(){
this.degree=10
var crossobj=iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas)
if (crossobj.filters&&crossobj.filters[0]){
if (typeof crossobj.filters[0].opacity=="number") //if IE6+
crossobj.filters(0).opacity=this.degree
else //else if IE5.5-
crossobj.style.filter="alpha(opacity="+this.degree+")"
}
else if (crossobj.style.MozOpacity)
crossobj.style.MozOpacity=this.degree/101
else if (crossobj.style.KhtmlOpacity)
crossobj.style.KhtmlOpacity=this.degree/100
else if (crossobj.style.opacity&&!crossobj.filters)
crossobj.style.opacity=this.degree/101
}
 
 
fadeshow.prototype.startit=function()
{
var crossobj=iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas)
this.populateslide(crossobj, this.curimageindex)
if (this.pausecheck==1){ //IF SLIDESHOW SHOULD PAUSE ONMOUSEOVER
var cacheobj=this
var crossobjcontainer=iebrowser? iebrowser["master"+this.slideshowid] : document.getElementById("master"+this.slideshowid)
crossobjcontainer.onmouseover=function(){cacheobj.mouseovercheck=1}
crossobjcontainer.onmouseout=function(){cacheobj.mouseovercheck=0}
}
this.rotateimage()
}

Open in new window




now this my c# code, and i am getting the image paths

 
protected void Page_Load(object sender, EventArgs e)
    {
       
        string[] f = GetNextImageUrl();

           }

    private string[] GetNextImageUrl()
    {
        string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "images/slideshow/", "*.jpg");

        for (int i = 0; i < files.Length; i++)
        {
            string path = files[i];
            path = "http://www.ramcon.jcwebhostza.com/" + files[i].Replace(AppDomain.CurrentDomain.BaseDirectory, string.Empty);
            files[i] = path;
        }

        return files;
    }

Open in new window



now i want to assign the src loction to my javascript:

var fadeimages=new Array()
//SET IMAGE PATHS. Extend or contract array as needed
fadeimages[0]=["http://www.ramcon.jcwebhostza.com/images/slideshow/1.jpg", "", ""]
0
 
LVL 9

Expert Comment

by:dexterrajesh
ID: 36961301
in you code-behind,

protected void Page_Load(object sender, EventArgs e)
    {
       
        string[] f = GetNextImageUrl();

           }

    private string[] GetNextImageUrl()
    {
        string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "images/slideshow/", "*.jpg");
       StringBuilder sb = new StringBuilder();   
       sb.Append("<script>");   
       sb.Append("var fadeimages = new Array;"); 
        for (int i = 0; i < files.Length; i++)
        {
            string path = files[i];
            path = "http://www.ramcon.jcwebhostza.com/" + files[i].Replace(AppDomain.CurrentDomain.BaseDirectory, string.Empty);
            files[i] = path;
            sb.Append("fadeimages.push('" + str + "');");   
        }
        sb.Append("</script>");   
        ClientScript.RegisterStartupScript(this.GetType(), "FadeImagesScript", sb.ToString());
        return files;
    }

Open in new window


and ofcourse you need to return the array in the method GetNextImageUrl...
I have not removed your codes

hope this helps
0
 
LVL 9

Expert Comment

by:dexterrajesh
ID: 36961303
and ofcourse you need not to return the array in the method GetNextImageUrl...
I have not removed your codes
0
 

Author Comment

by:JCWEBHOST
ID: 36961349
i tried this

 
var fadeimages=new Array()

var list = "http://www.ramcon.jcwebhostza.com/images/slideshow/1.jpg,http://www.ramcon.jcwebhostza.com/images/slideshow/Sunset.jpg,http://www.ramcon.jcwebhostza.com/images/slideshow/Water lilies.jpg"; 

fadeimages = list.split(",");

Open in new window


but not working?
0
 

Author Comment

by:JCWEBHOST
ID: 36961381
i tred this:

 
private string[] GetNextImageUrl()
    {
        string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "images/slideshow/", "*.jpg");
        StringBuilder sb = new StringBuilder();
        sb.Append("<script>");
        sb.Append("var fadeimages = new Array;");
        for (int i = 0; i < files.Length; i++)
        {
            string path = files[i];
            path = "http://www.ramcon.jcwebhostza.com/" + files[i].Replace(AppDomain.CurrentDomain.BaseDirectory, string.Empty);
            files[i] = path;
            sb.Append("fadeimages.push('" + files[i] + "');");
        }
        sb.Append("</script>");
        Page.ClientScript.RegisterStartupScript(this.GetType(), "FadeImagesScript", sb.ToString());
        return files;
    }

Open in new window


nothing happening?

0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36961447
Assign sb.ToString() to hidden field
hiddFld.value = sb.ToString();

And access this hidden field in javascript. Your javascript code could be
<script language="JavaScript1.2">

/***********************************************
* Flexi Slideshow- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var variableslide=new Array()


var list = "http://www.ramcon.jcwebhostza.com/images/slideshow/1.jpg,http://www.ramcon.jcwebhostza.com/images/slideshow/Sunset.jpg,http://www.ramcon.jcwebhostza.com/images/slideshow/Water lilies.jpg";
for (var i = 0; i< list.split(",").length; i++)
  variableslide[i]=[list.split(",")[i], '', ''];


var slidewidth='130px' //set to width of LARGEST image in your slideshow
var slideheight='120px' //set to height of LARGEST iamge in your slideshow, plus any text description
var slidebgcolor='#F3F3F3'


var slidedelay=3000

////Do not edit pass this line////////////////

var ie=document.all
var dom=document.getElementById

for (i=0;i<variableslide.length;i++){
var cacheimage=new Image()
cacheimage.src=variableslide[i][0]
}

var currentslide=0

function rotateimages(){
contentcontainer='<center>'
if (variableslide[currentslide][1]!="")
contentcontainer+='<a href="'+variableslide[currentslide][1]+'">'
contentcontainer+='<img src="'+variableslide[currentslide][0]+'" border="0" vspace="3">'
if (variableslide[currentslide][1]!="")
contentcontainer+='</a>'
contentcontainer+='</center>'
if (variableslide[currentslide][2]!="")
contentcontainer+=variableslide[currentslide][2]

if (document.layers){
crossrotateobj.document.write(contentcontainer)
crossrotateobj.document.close()
}
else if (ie||dom)
crossrotateobj.innerHTML=contentcontainer
if (currentslide==variableslide.length-1) currentslide=0
else currentslide++
setTimeout("rotateimages()",slidedelay)
}

if (ie||dom)
document.write('<div id="slidedom" style="width:'+slidewidth+';height:'+slideheight+'; background-color:'+slidebgcolor+'"></div>')

function start_slider(){
crossrotateobj=dom? document.getElementById("slidedom") : ie? document.all.slidedom : document.slidensmain.document.slidenssub
if (document.layers)
document.slidensmain.visibility="show"
rotateimages()
}

if (ie||dom)
start_slider()
else if (document.layers)
window.onload=start_slider

</script>

Open in new window

0
 
LVL 9

Assisted Solution

by:dexterrajesh
dexterrajesh earned 1000 total points
ID: 36961662
remove the line

var fadeimages=new Array();

from your script as the array has been declared and instantiated in code-behind.

alert the fadeimages in script to check if it is null or instantiated

0
 

Author Comment

by:JCWEBHOST
ID: 36961690
i have choose this option to save the url in a hidden field but i can not access it?

java file:

8226572]

private string GetNextImageUrl()
    {
        string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "images/slideshow/", "*.jpg");
        string list = "";

        for (int i = 0; i < files.Length; i++)
        {
            string path = files[i];
            path = "http://www.ramcon.jcwebhostza.com/" + files[i].Replace(AppDomain.CurrentDomain.BaseDirectory, string.Empty);
            list += path + ",";
        }
        return list.TrimEnd(',');
    }

Open in new window


protected void Page_Load(object sender, EventArgs e)
    {
        lblList.Text = GetNextImageUrl();
    }

Open in new window


i think my javascript loads first then my c# run,  please help
var fadeimages =new Array()

var list = document.getElementById(lblList).value;
alert(list); 

for (var i = 0; i< list.split(",").length; i++)
{
    fadeimages[i]=[list.split(",")[i]];
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

834 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