我正在gridview
显示数据库中的数据,例如
ProductID 产品名称 价格
A00001 Apple 10.00 ADDTOCART
ADDTOCART 是一个按钮。
GridViewRow gr = ((sender as LinkButton).NamingContainer as GridViewRow);
string itemId = gr.Cells[0].Text.Trim();
这些是我用于代码隐藏的代码,用于在点击 ADDTOCART 时获取 ProductID
需要帮助代码,它可以让我在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名称具有产品名称,并且当我单击ADDTOCART时,var id将具有id。谢谢你提前。
答案 0 :(得分:1)
试试这个,可以通过查看Dom
来操作元素名称和选择器在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);
}
});
}
答案 1 :(得分:0)
我猜您的数据库中的数据位于您的C#应用程序中,该应用程序位于您的服务器端。上面的代码是jquery / javascript,它位于您的客户端。
了解服务器端和客户端的区别非常重要。基本上说,你可以从&#34;查看来源&#34;来自浏览器,是客户端,否则,在服务器端。
回到你的问题,你可以在你的c#应用程序中使用数据库中的数据构建html,或者构建json数据对象,然后在jquery中使用它。
我应该在上面发表评论,但随着公司合并,域名更改,我丢失了我的电子邮件地址,必须从头开始新的。我没有足够的观点来发表评论。
希望帮助