如何让我的选择GridView控件的行成一个JavaScript变量? [英] How do I get my selected GridView rows into a Javascript variable?

查看:274
本文介绍了如何让我的选择GridView控件的行成一个JavaScript变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用ASP.NET C#MVC3剃刀模板,我想了解以下内容:

I use ASP.NET C# MVC3 with razor templates and I want to know the following:

我有一个DevEx preSS GridView控件中,我可以选择行。结果
选中后,我想将它们传递到一个Javascript变量。我该怎么做呢?结果
我若在同一时间选择的内容多行,我可以让他们在一个数组或东西吗?

I have a DevExpress GridView in which I can select rows.
When selected, I want to pass them into a Javascript variable. How do I do this?
And what if I have selected multiple rows at the same time, can I get them in an array or something?

编辑:
谢谢你的评论,我现在有以下JQuery的功能:

Thank you for the comment, I now have the following 'JQuery' function:

$(function () { // Verwijder functie
    $('#select').click(function () {
        var Delete = confirm("Weet u zeker dat u de geselecteerde records wilt verwijderen?");
        if (Delete) {
            //Verwijder Funtie
            var test = ;
            alert(test);
        } else {
            //Niks
        }
    });
});

我需要选定行到变量'测试'的ID,我GetSelectedFieldValues​​Callback和GetSelectedFieldValues​​尝试。但是,这不是工作,我很期待。
如果有人可以提供一些例子我真的AP preciate这一点。

I need to get the Id of the selected rows into the variable 'test', I tried it with GetSelectedFieldValuesCallback and GetSelectedFieldValues. But this is not working as I'm expecting. If someone can provide some example I would really appreciate that.

推荐答案

有是一个令人惊讶的缺乏一个网格的最基本的操作中的一个例子。特别是如果你想发送的行回服务器进行进一步的处理。

There is a surprising lack of examples for one of the most basic operations of a grid. Especially if you want to send rows back to the server for further processing.

与grid.GetSelectedFieldValues​​)问题(是,它会产生一个回发。这意味着你将需要第二次回发到实际数据发送回服务器。

The problem with grid.GetSelectedFieldValues() is that it generates a postback. Meaning you would need a second postback to actually send data back to the server.

最优雅的解决方案,我能够做到的,是用grid.GetSelectedKeysOnPage()。这将返回选择通过settings.KeyFieldName定义键字段=ID;

The most elegant solution I was able to achieve is to use grid.GetSelectedKeysOnPage(). This will return selected key fields defined through settings.KeyFieldName = "Id";

这将显示网格视图。

<script type="text/javascript">
    $(function () {
        $('#btn1').click(function () {
            simpleGrid.PerformCallback();
        });
    });
    function OnBeginCallback(s, e) {
        var selectedValues = s.GetSelectedKeysOnPage();
        e.customArgs["Id"] = "";
        for (var i = 0; i < selectedValues.length; i++) {
            e.customArgs["Id"] += selectedValues[i] + ',';
        }
    }
</script>
@Html.Partial("ProductsPartial", Model.Data)
<div id="btn1">
    btn
</div>

您创建一个单独的局部视图网格是非常重要的(不知道为什么,但是这就是它说devex preSS页面上。

It is important that you create your grid in a separate partial view (don't know why, but that is what it says on the devexpress page.

在ProductsPartial局部视图:

The "ProductsPartial" partial view:

@Html.DevExpress().GridView(
        settings =>
        {
            settings.Name = "simpleGrid";
            settings.KeyFieldName = "Id";
            settings.CallbackRouteValues = new { Controller = "yourController", Action = "Postback" };
            settings.SettingsText.Title = "simpleGridWithPostback";
            settings.Settings.ShowTitlePanel = true;
            settings.Settings.ShowStatusBar = GridViewStatusBarMode.Visible;
            settings.SettingsPager.Mode = GridViewPagerMode.ShowAllRecords;
            settings.SettingsPager.AllButton.Text = "All";
            settings.SettingsPager.NextPageButton.Text = "Next &gt;";
            settings.SettingsPager.PrevPageButton.Text = "&lt; Prev";
            settings.Width = new System.Web.UI.WebControls.Unit(200);
            settings.Columns.Add("Id");
            settings.Columns.Add("Name");
            settings.CommandColumn.Visible = true;
            settings.CommandColumn.ShowSelectCheckbox = true;
            settings.ClientSideEvents.BeginCallback = "OnBeginCallback";
        }).Bind(Model).GetHtml()

最后,控制器可以在其中处理数据

And finally the controller in which you can process the data

public ActionResult Postback()
{
    String data = Request["Id"];
}

这种方式可以处理所有你想要的数据服务器端

This way you can process all the data you want server side

这篇关于如何让我的选择GridView控件的行成一个JavaScript变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆