简体   繁体   English

使用JQuery和JavaScript进行Gridview操作

[英]Gridview manipulation using JQuery and JavaScript

I have an ASP.NET gridview I want to manipulate using JavaScript/JQuery. 我有一个要使用JavaScript / JQuery进行操作的ASP.NET网格视图。 The problem I THINK I'm going to have with my approach is that the server won't have any knowledge of the rows that I am appending via gridview since the html representation of the gridview control is coupled with the object model that lives on the server. 我想我的方法要解决的问题是,服务器将不了解我通过gridview追加的行,因为gridview控件的html表示与驻留在该对象上的对象模型耦合在一起。服务器。 So here is what I need to do: 所以这是我需要做的:

I need to append to the gridview when a user submits data, and submit each row in the batch of entered data to the server to be processed. 当用户提交数据时,我需要追加到gridview,然后将输入的数据批中的每一行提交到要处理的服务器。 Because I have other ASP.NET controls that will contain data that I want to submit, I'd like to submit all that data via a traditional postback to the server. 因为我还有其他ASP.NET控件将包含要提交的数据,所以我想通过传统的回发将所有这些数据提交到服务器。

  1. How do I implement this approach if possible? 如果可能的话,我该如何实施这种方法?
  2. If not possible, could you please explain? 如果不可能,请您解释一下?

Thank you very much for your help. 非常感谢您的帮助。

var queryString = "";

// This could be based on a number of different events
$('#finishButton').click(function(){

    // Iterate through each input (you could add other form elements)
    $('#myForm input').each(function(){

        // Build your query string to post to your aspx page
        queryString += $(this).attr("name") + "&" + $(this).val() + ",";

    });
});

// Make sure special chars are escaped
queryString = escape(queryString);

// POST the form to your aspx page
$.ajax({ 
    type: 'POST', 
    url: 'myFormProcessor.aspx', 
    data: queryString,
    // Upon a successful POST, successHandler will be called 
    success: successHandler 
});

// Add the new data to the grid
function successHandler(){

    // Clone the last row
    $('#myTable tr:last').clone(true).insertAfter('#myTable tr:last');

    // Here you could just break apart the query 
    // string you build in the above code
    // and use those values to change the values 
    // in the row you added to the grid

}

Make sure to unescape the query string in your aspx page, and then break it up by the delimiters you're using. 确保在aspx页面中取消转义查询字符串,然后按使用的分隔符将其分解。 I used '&' to separate key/value and commas between variables (inputs). 我使用'&'来分隔变量(输入)之间的键/值和逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM