开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 5123|回复: 0

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

[复制链接]

0

精华

9

贡献

41

赞扬

帖子
68
软币
1133
在线时间
157 小时
注册时间
2013-9-3
发表于 2014-11-20 10:40:05 | 显示全部楼层 |阅读模式
最近有用到一个功能、在客户端获取用户选中行的某字段值,默认的方法会导致页面回传(AJAX)刷新,效率比较低,于是搜索一番,拿到下面这个demo代码,经测试效果很好,公布在这里供有需要的朋友使用。
页面代码:
[HTML] 纯文本查看 复制代码
<%-- 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[index - grid.visibleStartIndex] + "', and title is " + grid.cpTitles[index - grid.visibleStartIndex]);
}
</script>    
    
</head>
<body>
    <form id="form1" runat="server">
    
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/pubs.mdb"
            SelectCommand="SELECT [title_id], [title], [type], [pubdate], [price] FROM [titles]">
        </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[grid.GetFocusedRowIndex()] + "', and title is " + grid.cpTitles[grid.GetFocusedRowIndex()]);
}" />
            <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#代码:

[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[end - startIndex], titles = new object[end - startIndex];
        for(int n = startIndex; n < end; n++) {
            titleId[n - startIndex] = grid.GetRowValues(n, "title_id");
            titles[n - startIndex] = grid.GetRowValues(n, "title");
        }
        e.Properties["cpTitleId"] = titleId;
        e.Properties["cpTitles"] = titles;
    }
}


评分

参与人数 1贡献 +2 赞扬 +1 收起 理由
羽叶 + 2 + 1 赞一个

查看全部评分

回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-12-23 01:40

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表