【问题标题】:ASPXGridView ClientSideEvents How to Get Selected Row's KeyField ValueASPXGridView ClientSideEvents 如何获取选定行的 KeyField 值
【发布时间】:2012-01-15 03:07:15
【问题描述】:

我正在尝试在客户端获取选定的网格行 KeyField 值;

我曾经尝试以下并得到各种结果:

方法#1

<ClientSideEvents RowClick="function(s, e) {var key= grid.GetSelectedKeysOnPage()[0];}" />
//This gives previous selected rows value everytime

方法#2

<ClientSideEvents RowClick="function(s, e) { grid.GetRowValues(grid.GetFocusedRowIndex(), 'MyKeyFieldName', OnGetRowValues); }" />
//This gives previous selected row and also gives an error: "A primary key field specified via the KeyFieldName property is not found in the underlying data source. Make sure.. blabla" But the MyKeyFieldName is true and i dont want to make a callback, i dont want to use this method!

方法#3

<ClientSideEvents RowClick="function(s, e) { grid.GetRowValues(e.visibleIndex, 'MyKeyFieldName', OnGetRowValues); }">
//This gives the same result with Method #2

问题是:如何在没有回调或回发的情况下在客户端 RowClick 事件中收集(不是先前但)当前选定行的 KeyField 值?

【问题讨论】:

    标签: asp.net devexpress client-side aspxgridview


    【解决方案1】:

    方法#2和#3

    这两种方法都需要回调到服务器。

    确保您已指定行选择操作所需的 ASPxGridView.KeyFieldName 属性。

    如何在没有回调或回发的情况下收集所选行@客户端的 KeyField 值?

    处理客户端ASPxClientGridView.SelectionChanged事件;

    通过“e.isSelected”属性确定刚刚选择的行;

    通过客户端ASPxClientGridView.GetRowKey方法确定行的keyValue。

    将“e.visibleIndex”属性作为参数传递:

    <ClientSideEvents SelectionChanged="function(s, e) {
        if (e.isSelected) {
            var key = s.GetRowKey(e.visibleIndex);
            alert('Last Key = ' + key);
        }
    }" />
    

    【讨论】:

    • 谢谢,但这不是我期待的答案。我在问如何在 Clientside RowClick 事件中收集新选择的行的 Keyfield 值?而 ASPxClientGridView.GetSelectedKeysOnPage[0] 在选择改变时也在改变,它不一样。
    • 如果您想使用 Mikhail 的解决方案,仅供参考;使用 SelectionChanged 事件的问题:当用户排序或过滤时;存储的密钥未更新(未触发 SelectionChanged 事件)
    • 哎呀对不起;我的意思是 visibleIndex 将在排序后更改等,因此当您必须使用缓存的 visibleIndex 调用 ASPxClientGridView.GetRowValues 时,您将得到错误的行
    • 这是正确的,因为总行数改变了。
    • 有一段时间我以某种方式忽略了这个问题,正如“风筝”所提到的,使用 Mikhail 解决方案是正确的,但对于排序、过滤条件是不可信的。我通过停止使用 DevEx 控件找到了明显的解决方案。它们很好,但定制起来很差。例如,Grid 控件没有“RowDataBound”事件。 DevEx 开发人员针对特定要求为其他一些事件提供建议,但他们的建议并不适合所有情况。 RowDataBound 是一个事件,不是任何其他事件。等等。我现在改变了 MVC+jQuery 的路线,这是一个开发 web 的辉煌世界。
    【解决方案2】:

    如何通过 3 个简单的步骤。

    在我的例子中,我想在用户单击行时从 ASPxGridView 中获取字段(“ID”)的内容...

    1. 为行单击创建 ClientSideEvent 并放置“RowClick(s, e);”在函数中。
    2. 创建事件将调用的实际函数,如下所示 - 这是棘手的部分;不要使用 GetFocusedRowIndex() 来获取索引,因为它是 FOCUSED 索引。使用 e.visibleIndex

      function RowClick(s, e) {
          // Do callback to get the row data for 'ID' using current row.
          MyAspxGridView.GetRowValues(e.visibleIndex, 'ID', OnGetRowId);
      }
      
    3. 创建您的回调以获取您想要的字段。我正在获取“ID”。

      function OnGetRowId(idValue) {
          alert('ID: ' + idValue.toString());
      }
      

    【讨论】:

    • 这里有一点延迟。您如何解决延迟问题?
    【解决方案3】:
    function OnbtnOkClick(s, e) {
        grid.GetRowValues(grid.GetFocusedRowIndex(), 'FieldName1;FieldName2', OnGetRowValues);
    }
    
    function OnGetRowValues(values) {
        var fName1 = values[0];
        var fName2 = values[1];
        txt1.SetText(fName1);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      • 1970-01-01
      相关资源
      最近更新 更多