2012-04-11 85 views
1

我做在asp.net web應用添加服務器端處理數據的JavaScript在.aspx頁面中。我需要翻頁效果,所以我使用了turn.js(ref:http://www.turnjs.com/)。現在我需要在服務器端處理一些數據,即代碼隱藏並將其發送到客戶端。我從上面的網站下載了該項目(來自:https://github.com/blasten/turn.js)。但問題是,我不知道如何將代碼隱藏的數據發送到實際加載動態頁面的.aspx頁面中的javascript。如何從代碼隱藏在asp.net使用C#

function addPage(page, book) { 
     // First check if the page is already in the book 
     if (!book.turn('hasPage', page)) { 
      // Create an element for this page 
      var element = $('<div />', {'class': 'page '+((page%2==0) ? 'odd' : 'even'), 'id': 'page-'+page}).html('<i class="loader"></i>'); 
      // If not then add the page 
      book.turn('addPage', element, page); 
      // Let's assum that the data is comming from the server and the request takes 1s. 
      setTimeout(function(){ 
        element.html('<div class="data">Data for page '+page+'</div>'); 
      }, 1000); 
     } 
    } 

這是javascript功能,增加了動態網頁。如何在行中添加頁面的內容element.html('<div class="data">Data for page '+page+'</div>'); 請儘快幫助我,我很困難。

在你的aspx頁面

回答

0

element.html(「<%= pageData%>」)

在你的代碼

背後創建一個名爲pageData,然後在你的代碼(parhaps頁面加載事件)公共字符串將字符串數據要到pageData

或者你可以創建一個Web服務或只是一個簡單的ashx的文件,與所需的數據作出反應,並使用AJAX來得到下一個頁面的數據。

+0

有用的..謝謝了很多.. – Vikram 2012-04-11 16:33:39

0

有幾種方法可以做你所要求的。

  1. 將asp:Literal控件添加到您的javascript函數中。在後面的代碼中設置其內容。

    setTimeout(function(){ 
          element.html('<div class="data">Data for page '+<asp:Literal runat="server" Id="litPageData"></asp:Literal>+'</div>'); 
        }, 1000); 
    
  2. 添加一個隱藏字段,在代碼中設置它的後面,並訪問你的Javascript的價值。

    <asp:Hidden runat="server" id="hidPageData"></asp:Hidden> 
    

    (我可能是有點過的語法這一塊)

    setTimeout(function(){ 
          element.html('<div class="data">Data for page '+$('#<%= hidPageData.ClientId%>').val() +'</div>'); 
        }, 1000); 
    
  3. Symeon的解決方案,已經發布。

+0

曾爲..非常感謝! – Vikram 2012-04-11 16:33:26

0

在你的代碼隱藏例如code.cs聲明一個變量: - 字符串strDummyValues = 「虛擬價值」;

在ASPX頁

: - 使用 %< = strDummyValues%>表示

element.html( '數據頁 '+ <%= strDummyValues%> +'');

問候 阿賈伊·辛格·塔庫爾