2016-07-25 44 views
0

我有兩個頁面customer.aspx和customer_detail.aspc,每個頁面都有相應的代碼隱藏文件。目標是能夠將特定的表單元格的值從客戶發送到customer_detail。例如,以下JavaScript基於customer.aspx.cs文件中的BindTable方法動態地將表格呈現到customer.aspx上。 customer.aspx:C#從一個頁面發送表格單元格數據到另一個頁面

<script type="text/javascript"> 
     $(document).ready(function() { 
      $.ajax({ 
       type: "POST", 
       contentType: "application/json; charset=utf-8", 
       url: "customer.aspx/BindCustomer", 
       data: "{}", 
       dataType: "json", 
       success: function(data) { 
        for (var i = 0; i < data.d.length; i++) { 
         $("#customer_table").append("<tr><td>"+'<a id = "anchor" href="customer_individual.aspx">' + data.d[i].customer_name +'</a>'+ "</td><td>" + data.d[i].customer_id +"</td><td>" + data.d[i].total_value + "</td><td>" + data.d[i].created_date + "</td></tr>"); 
        } 
       }, 
       error: function(result) { 
       alert("Error"); 
       } 
      }); 
     }); 

上述BindCustomer功能是一個用customer.aspx.cs一個WebMethod,它返回一個客戶陣列對象,顧客[],它被綁定到CUSTOMER_TABLE。注意,在上面,我在表的第一個td元素中有一個href,它將頁面重定向到customer_detail.aspx。

目標是在單擊customer_detail.aspx.cs鏈接後發送一個customer_name值,以便我可以查詢單獨存儲的客戶對象,並使用名稱並相應地填充頁面。什麼是最好的方法來完成這個?有關如何進行的任何建議將非常有幫助!

+0

當你說,'發送數據到'頁面,你的意思是重定向用戶到該頁面並顯示或以某種方式引用該數據? –

+0

是的,目前的鏈接工作,其中'customer_detail'頁面包含一些靜態字段,如文本框和標籤。用戶需要重定向到頁面以及值data.d [i] .customer_name,然後我可以使用它填充customer_detail頁面上的文本框 – skarred14

+0

然後,這就是它的全部內容。只需將想要傳遞的值附加到查詢字符串中的網址即可。 –

回答

0

最簡單的方法就是格式化的鏈接與你想在這樣的查詢字符串來傳遞數據給customer_detail.aspx(它看起來非常相似,你已經做什麼。)

<a href="/customer_detail.aspx?customerName=dataYouWantToPass">Link</a> 

當加載customer_detail.aspx時,您從查詢字符串中檢索值。

在你Page_Load事件customer_detail.aspx

var customerName = Request["customerName"]; 

你可以更具體的說Request.Querystring["customerName"],但這個頁面上,你可能不關心的值是否在查詢字符串或表單域通過。 Request["customerName"]涵蓋了兩個。

+0

明白了..我正在嘗試這種方法。同樣的事情適用於傳遞推導值嗎?我的意思是我想傳入data.d [i] .customer_name的動態值,它是在創建customer.aspx數據表期間創建的。特別是,這是否適用? Link,其中上面的鏈接的值是data.d [i] .customer_name – skarred14

+0

Hannan:能夠使用上面建議的方法傳遞靜態文本,並且能夠在第二頁上檢索該文本。 。你能建議我如何檢索這個動態文本嗎?我試過如下:Modfying的HREF爲:$( 「#patient_table」)追加( 「​​」+ '' + data.d[i].patient_name + '' +「​​」+「」);並在相同的document.ready:$(「a」)。prop(「href」,function(){return this.href + = $ .trim($(this).text());});我嘗試了內聯:onclick =「window.location.href = this.href + $ .trim($(this).text()); 建議? – skarred14

+0

你不能傳遞這樣的函數 - 只是一個靜態值。但是它更好,Customer_detail.aspx不需要知道參數來自哪裏,它只是有它的輸入(參數)並做它的事情。 –

相關問題