【问题标题】:How to get data from a gidview to jquery如何从gridview获取数据到jquery
【发布时间】:2017-05-25 02:50:27
【问题描述】:

我有 gridview 显示来自数据库的数据,例如

产品ID 产品名称 价格


A00001 苹果 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 名称以获取产品名称,并获取 var id 以获取 id。提前致谢。

【问题讨论】:

  • 何时调用此方法“ShowCurrentTime”?点击按钮?
  • 是的,在 buttonclick 上,按钮 ADDTOCART
  • 您是如何将 addtocart 按钮的事件处理程序附加到 JavaScript 方法的?在 ShowCurrentTime 方法中,您需要抓住被点击的按钮,然后在按钮的同一 TR 中找到文本框,并使用它们的值作为 if 和 name。

标签: c# jquery asp.net gridview


【解决方案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);
    }
    });
}

【讨论】:

    【解决方案2】:

    我猜你来自数据库的数据在你的 C# 应用程序中,它在你的服务器端。上面的代码是 jquery/javascript,在你的客户端。

    了解服务器端和客户端的区别非常重要。基本上说,您从浏览器的“查看源代码”中看到的所有内容都是客户端,否则,在服务器端。

    所以回到你的问题,你可以在你的 c# 应用程序中使用数据库中的数据构建 html,或者构建 json 数据对象,然后在 jquery 中使用它。

    我应该在上面发表评论,但是随着公司合并,域更改,我丢失了我的电子邮件地址,必须从头开始一个新的。积分不够我写评论。

    希望有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多