2013-10-22 101 views
0

我有一個.js文件: - :查詢SQL,並將其分配給JavaScript

  • namenvarchar
  • pathnvarchar

    $(function(){ 
             var playItem = 0, 
            title=$('.jp-interface .jp-title'), 
            jPlayer=$("#jplayer"), 
            myPlayList = [ 
          {name:"BlackPlant",mp3:"audio/black_plant.mp3",ogg:"audio/black_plant.ogg"}, 
          {name:"Hidden",mp3:"audio/hidden.mp3",ogg:"audio/hidden.ogg"}, 
          {name:"The Separation",mp3:"audio/separation.mp3",ogg:"audio/separation.ogg"} 
         ],  
         jPlay=function(idx){ 
          if(typeof idx==typeof 0) 
           jPlayer.jPlayer("setMedia",myPlayList[idx]).jPlayer('play') 
          if(typeof idx==typeof '') 
           jPlayer.jPlayer("setMedia",myPlayList[playItem=idx=='next'?(++playItem<myPlayList.length?playItem:0):(--playItem>=0?playItem:myPlayList.length-1)]).jPlayer('play')     
          title.text(myPlayList[playItem].name) 
          Cufon.refresh() 
         } 
    
        jPlayer.jPlayer({ 
         ready: function() { 
          jPlay(playItem) 
         }, 
         ended:function(){ 
          jPlay('next') 
         } 
        }) 
    
        $(".jp-prev,.jp-next") 
         .click(function() { 
          jPlay($(this).is('.jp-next')?'next':'prev') 
          return false; 
         }) 
    
    }); 
    

    存儲在SQL Server> 數據庫)

我的音樂播放器在.aspx頁面中使用此腳本播放音樂。我想在sql server中查詢路徑和名稱,並將它們分配給myPlayList .js文件。有沒有辦法做到這一點?

回答

0

在你的aspx頁面添加這個腳本

<script type="text/javascript"> 
function QueryDB() { 
    PageMethods.GetServerData(OnSucceeded, OnFailed); 
} 

function OnSucceeded(result, userContext, methodName) { 
    var name= result[0];//You will get your name here 
    var path = result[1];//and your path here 
} 

function OnFailed(error, userContext, methodName) { 
    alert(error); 
} 
</script> 

,你必須在你的aspx頁面在你的代碼也添加

<asp:ScriptManager runat="server" ID="ScriptManager1" EnablePartialRendering="true" EnablePageMethods="true" /> 

現在

背後必須添加一個頁面的方法是這樣

[System.Web.Services.WebMethod] 
public static string[] QueryDB() 
{ 
/* Query your database from here and pass the name and path where I am passing 
myName and myPath*/ 
    return new string[] { myName , myPath}; 
} 
相關問題