开发者

Iterate Gridview through jquery

suppose i have a gridview and my gridview has many rows including header and footer and pager.

in every row there is some textbox, dropdown and checkbox.

like two textbox for employeeid and employeename,one dropdown for sex, one checkbox for selecting graduate or not.

so i need to know 开发者_开发问答that how could i iterate gridview through jquery and also iterate each row excluding header,footer and pager.

so i need to extract values from textbox,dropdown and checkbox from each row and build json string for sending data to server.

build json string like

            var json = "{'Name':'" + $("input[id*='txtName']").val() +
        "','Subject':'" + $("input[id*='Subject']").val()  +
        "','Email':'" + $("input[id*='Email']").val() +
        "','Message':'" + jQuery.trim($('.message').val()) + "'}";

thanks


Try this.

Add RowStyle to the GridView markup like this

<RowStyle CssClass="row" />
<AlternatingRowStyle CssClass="row" /><!-- If needed  -->

Now all the trs in the GridViewRow will have <tr class="row">. Then call this script inside your button click event.

var items = [];
$(".row").each(function() {
    var item = {};
    item.ID = $.trim($(this).find("input[id*='ID']").val());
    item.Name = $.trim($(this).find("input[id*='Name']").val());
    item.Gender = $(this).find("select[id*='Gender']").val();
    item.IsGraduate = $(this).find("input[id*='IsGraduate']").is(':checked');
    items.push(item);
    });
var DTO = JSON.stringify(items);

Please add reference to json2.js for browsers that do not have JSON support

Mock-up: http://jsfiddle.net/naveen/P8Aue/


I don't know what gridview you are using, but i can suggest you such solution.

Gridview is rendered as table and you can give attributes to your rows like "Row0","Row1"

By finding your gridview in java script you can iterate through its rows and using assigned attributes know what is the current row and find appropriate element in row and take its value

$("tr",$("#gridViewID")).each(function()
{
  //   here you can get elements index of your row by tour attribute

   `$("input[id*='txtName']",$(this))` //is your textbox

   // getting all this values you can build your json string

})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜