JavaScript code in C# aspx.cs page

Last post 05-15-2008 10:14 PM by blodfox777. 5 replies.

Sort Posts:

  • JavaScript code in C# aspx.cs page

    05-11-2008, 3:12 AM
    • Member
      11 point Member
    • khoshru
    • Member since 02-04-2008, 12:01 PM
    • Dhaka, Bangladesh
    • Posts 42

    Dear all,

    How to write JavaScript code in C# aspx.cs page on pageload event? 

    Khoshru
    Dhaka, Bangladesh
  • Re: JavaScript code in C# aspx.cs page

    05-11-2008, 3:33 AM
    • Member
      299 point Member
    • talgiladi
    • Member since 07-01-2007, 9:06 AM
    • Israel
    • Posts 43

     What is it exactly that you want to do?

    If you want to add javascript function you can use:

    Page.ClientScript.RegisterClientScriptBlock

    Tal Giladi
    Software engineer
    www.talgiladi.net
  • Re: JavaScript code in C# aspx.cs page

    05-11-2008, 3:47 AM
  • Re: JavaScript code in C# aspx.cs page

    05-11-2008, 3:49 AM
    • Member
      11 point Member
    • khoshru
    • Member since 02-04-2008, 12:01 PM
    • Dhaka, Bangladesh
    • Posts 42

    Hi,

    Thanks for your replay. I have an aspx.cs page and I want to write javascript code to show some images randomly. So What should I have to do.

    Khoshru
    Dhaka, Bangladesh
  • Re: JavaScript code in C# aspx.cs page

    05-15-2008, 10:11 PM
    Answer

    Hi,

     

    Try to refer the following code to get the random image in javascript.

     

    <script language="Javascript">

    <!--

    // please keep these lines on when you copy the source

    // made by: Nicolas - http://www.javascript-page.com

     

    var currentdate = 0

    var core = 0

     

    function StringArray (n) {

      this.length = n;

      for (var i =1; i <= n; i++) {

        this[i] = ' '

     

      }

    }

     

    image = new StringArray(10)

    image[0] = '0.gif'

    image[1] = '1.gif'

    image[2] = '2.gif'

    image[3] = '3.gif'

    image[4] = '4.gif'

    image[5] = '5.gif'

    image[6] = '6.gif'

    image[7] = '7.gif'

    image[8] = '8.gif'

    image[9] = '9.gif'

     

    var ran = 60/image.length

     

    function ranimage() {

      currentdate = new Date()

      core = currentdate.getSeconds()

      core = Math.floor(core/ran)

        return(image[core])

    }

     

    document.write("<img src='" +ranimage()+ "'>")

     

    //-->

    </script>

     

    Thanks.

     

    Michael Jin.
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: JavaScript code in C# aspx.cs page

    05-15-2008, 10:14 PM
    • Participant
      1,092 point Participant
    • blodfox777
    • Member since 07-12-2007, 8:30 AM
    • Posts 159

    Hi khoshru

    try the following code, hope it helpsSmile

     

    <html><head>
    <script language="javascript">
    
    today=new Date(); 
    jran=today.getTime(); 
    function rnd() { 
    ia=9301; 
    ic=49297; 
    im=233280; 
    jran = (jran*ia+ic) % im; 
    return jran/(im*1.0); 
    }; 
    
    function rand(number) { 
    return Math.ceil(rnd()*number); 
    }; 
    
    document.write("<center>"); 
    for(i=1;i<=1;i++) { 
    myNum=(rand(3));
    if(myNum == 1) document.write("<img src='http://static.asp.net/asp.net/images/books/book170.jpg'>"); 
    if(myNum == 2) document.write("<img src='http://static.asp.net/asp.net/images/books/book179.jpg'>");  
    if(myNum == 3) document.write("<img src='http://static.asp.net/asp.net/images/books/book171.jpg'>");  
    else { 
    document.write(); 
    }
    
    }; 
    document.write("</center>");
    
    </script> 
    <title>OK</title></head>
    <body>
    
    </body>

     </html>

     

    Regards!

    -- "Mark As Answer" If my reply helped you --
Page 1 of 1 (6 items)