hebiziyu 发表于 2014-11-20 10:40:05

dev控件的gridview,纯客户端获取选中行某字段值的demo

最近有用到一个功能、在客户端获取用户选中行的某字段值,默认的方法会导致页面回传(AJAX)刷新,效率比较低,于是搜索一番,拿到下面这个demo代码,经测试效果很好,公布在这里供有需要的朋友使用。
页面代码:
<%-- BeginRegion --%>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Data_Bind_Control_PassDataOnClient" %>

<%@ Register Assembly="DevExpress.Web.v14.1"
    Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dxe" %>
<%@ Register Assembly="DevExpress.Web.v14.1"
    Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dxwgv" %>
<%@ Register Assembly="DevExpress.Web.v14.1" Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dxe" %>
<%@ Register Assembly="DevExpress.Web.v14.1" Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dxwgv" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%-- EndRegion --%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Data caching on the client</title>
<script type="text/javascript">
function ProcessRow(index) {
    alert("The row id is '" + grid.cpTitleId + "', and title is " + grid.cpTitles);
}
</script>   
   
</head>
<body>
    <form id="form1" runat="server">
   
      <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/pubs.mdb"
            SelectCommand="SELECT , , , , FROM ">
      </asp:AccessDataSource>
      <dxwgv:aspxgridview id="grid" runat="server" width="950px" AutoGenerateColumns="False"
                DataSourceID="AccessDataSource1" KeyFieldName="title_id"
                OnCustomJSProperties="grid_CustomJSProperties">
            <settingsbehavior allowfocusedrow="True" allowselectbyrowclick="True" />
            <SettingsPager PageSize="5" />   
<%-- BeginRegion Columns --%>
            <clientsideevents focusedrowchanged="function(s, e) {
       
    alert("The row id is '" + grid.cpTitleId + "', and title is " + grid.cpTitles);
}" />
            <Columns>
                <dxwgv:GridViewDataTextColumn Caption="title_id" FieldName="title_id" ReadOnly="True"
                  VisibleIndex="1">
                </dxwgv:GridViewDataTextColumn>
                <dxwgv:GridViewDataTextColumn Caption="title" FieldName="title" VisibleIndex="2">
                </dxwgv:GridViewDataTextColumn>
                <dxwgv:GridViewDataTextColumn Caption="type" FieldName="type" VisibleIndex="3">
                </dxwgv:GridViewDataTextColumn>
                <dxwgv:GridViewDataDateColumn Caption="pubdate" FieldName="pubdate" VisibleIndex="4">
                </dxwgv:GridViewDataDateColumn>
                <dxwgv:GridViewDataTextColumn Caption="price" FieldName="price" VisibleIndex="5">
                </dxwgv:GridViewDataTextColumn>
                <dxwgv:GridViewDataTextColumn Caption="button" VisibleIndex="6">
                  <DataItemTemplate>
                        <input type="button" value="Click Me" />
                  </DataItemTemplate>
                </dxwgv:GridViewDataTextColumn>
            </Columns>
<%-- EndRegion --%>
      </dxwgv:aspxgridview>
    </form>
</body>
</html>

C#代码:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DevExpress.Web.ASPxTabControl;
using System.Drawing;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;
using System.Collections.Generic;
using DevExpress.Data;

public partial class Data_Bind_Control_PassDataOnClient : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void grid_CustomJSProperties(object sender, ASPxGridViewClientJSPropertiesEventArgs e) {
      int startIndex = grid.PageIndex * grid.SettingsPager.PageSize;
      int end = Math.Min(grid.VisibleRowCount, startIndex + grid.SettingsPager.PageSize);
      object[] titleId = new object, titles = new object;
      for(int n = startIndex; n < end; n++) {
            titleId = grid.GetRowValues(n, "title_id");
            titles = grid.GetRowValues(n, "title");
      }
      e.Properties["cpTitleId"] = titleId;
      e.Properties["cpTitles"] = titles;
    }
}


页: [1]
查看完整版本: dev控件的gridview,纯客户端获取选中行某字段值的demo