2017-05-25 21 views
0

我有gridview顯示從數據庫中的數據,如如何從一個gidview數據的jQuery

的ProductID產品名稱價格


A00001蘋果10.00 ADDTOCART

ADDTOCART是一個按鈕。

GridViewRow gr = ((sender as LinkButton).NamingContainer as GridViewRow); 

string itemId = gr.Cells[0].Text.Trim(); 

這些是我使用代碼隱藏的代碼來獲取的ProductID當點擊ADDTOCART

需要幫助的,可以讓變量的代碼我jQuery中宣佈獲得的ProductID像點擊ADDTOCART按鈕時的代碼隱藏功能。

function ShowCurrentTime() { 

     var name = "The name";//$("#<%=txtUserName.ClientID%>")[0].value; //get the data. 
     var id = "The id";//$("#<%=TextBox1.ClientID%>")[0].value; //get the data. 
     $.ajax({ 
      type: "POST", 
      url: "WebForm1.aspx/GetCurrentTime", //the url and method name of the webmethod 
      data: "{ name: '" + name + "', id: '" + id + "' }", //pass in 2 data. 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: OnSuccess, 
     failure: function (response) { //fixed 
      alert(response.d); 
     } 
     }); 
    } 
    function OnSuccess(response) { //fetching object come out, object inside got name and id, need to specific which data you want by obj.id/obj.name 
     var obj = response.d; //fetching the webmethod 
     alert(obj.id + "" + obj.name) //display the return 
     $('#trycart').append("<li>" + obj.id + "</li>"); 
     $('#cart').append("<tr><td>" + obj.id + "</td><td>" + obj.name +"</td></tr>"); //access the table id=cart, add in the data comeback from webmethod. 
    } 

我需要幫助有變數名稱有產品名稱,和VAR ID有ID,當我點擊ADDTOCART。先謝謝了。

+0

當這個方法「ShowCurrentTime」被叫?點擊按鈕? – Krishna

+0

是的,在buttonclick上,按鈕ADDTOCART – Chris

+0

您是如何將addtocart按鈕的事件處理程序附加到JavaScript方法的?在ShowCurrentTime方法中,您需要獲取單擊的按鈕,然後在按鈕的同一TR中查找文本框,並使用它們的值和if和name一樣。 –

回答

0

我想你的數據庫中的數據是在你的C#應用​​程序,這是在你的服務器端。上面的代碼是jquery/javascript,它在你的客戶端。

瞭解服務器端和客戶端的差異非常重要。基本上說,從瀏覽器的「查看源代碼」中可以看到的所有內容都是客戶端,否則在服務器端。所以回到你的問題,你可以在你的c#應用程序中使用數據庫中的數據構建html,或者構建json數據對象,然後在jquery中使用它。

我應該在上面發表評論,但隨着公司合併,域名更改,我丟失了我的電子郵件地址,不得不從頭開始新的。我沒有足夠的積分來發表評論。

希望幫助

1

試試這個,該元素的名稱和選擇可以通過查看大教堂

在按鍵被操縱的onclick通過爲參

function ShowCurrentTime(element) 
{ 

var name = $(element).closest("tr").find("input[id*='txtUserName']").val(); 
    var id = $(element).closest("tr").find("input[id*='TextBox1']").val(); 
    $.ajax({ 
     type: "POST", 
     url: "WebForm1.aspx/GetCurrentTime", //the url and method name of the webmethod 
     data: "{ name: '" + name + "', id: '" + id + "' }", //pass in 2 data. 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: OnSuccess, 
    failure: function (response) { //fixed 
     alert(response.d); 
    } 
    }); 
} 
相關問題