我的页面上有一个 rad 网格和一个 rad 选项卡条,要求是当用户单击 rad 网格中的任何行时,必须在 rad 选项卡条中填充特定行的所有详细信息。我用于填充 rad 选项卡条的函数是用 jquery 编写的。我的问题是如何获取 rad 网格中的项目。我写过
var grid = $find("RadGrid1")
var items = grid.items.count;
它给了我一个错误。我怎样才能解决这个问题?
您可以使用 MasterTableView.ClientDataKeyNames 属性来访问所选项目的值,以指定要访问的键。您可以通过调用在客户端 OnRowSelected 事件期间访问这些值getDataKeyValues("KeyName")
。这是一个例子:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RadGridCustomFooter._Default" %>
<!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>RadGrid Example</title>
<script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
<telerik:RadGrid ID="rgCustomers" runat="server" Skin="Black"
AllowPaging="true"
AllowSorting="true"
PageSize="8">
<ClientSettings>
<ClientEvents OnRowSelected="grid_rowSelected" />
<Selecting AllowRowSelect="true" />
</ClientSettings>
<MasterTableView
ClientDataKeyNames="ID,Name,Address,Sales">
<Columns>
<telerik:GridBoundColumn
DataField="ID"
UniqueName="ID">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn
DataField="Name"
UniqueName="Name">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn
DataField="Address"
UniqueName="Address">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn
DataField="Sales"
DataFormatString="{0:C}"
UniqueName="Sales">
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
<SelectedItemStyle CssClass="none" />
</telerik:RadGrid>
<div id="results"></div>
<script type="text/javascript">
function grid_rowSelected(sender, args) {
var id = args.getDataKeyValue("ID");
var name = args.getDataKeyValue("Name");
var address = args.getDataKeyValue("Address");
var sales = args.getDataKeyValue("Sales");
var selectedValues = String.format("ID: {0}, Name: {1}, Address: {2}, Sales: {3}", id, name, address, sales);
$('#results').html(selectedValues);
}
</script>
</form>
</body>
</html>
这是代码隐藏:
using System;
using Telerik.Web.UI;
namespace RadGridCustomFooter
{
public partial class _Default : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
rgCustomers.NeedDataSource += new GridNeedDataSourceEventHandler((s, a) =>
{
var customers = new[] {
new { ID = 1, Name = "Acme Products", Address = "123 Maple Drive", Sales = 23450 },
new { ID = 2, Name = "Northwind", Address = "1 First Street", Sales = 11569 },
new { ID = 3, Name = "Alverson Utilities", Address = "890 Union Avenue", Sales = 78232 },
new { ID = 4, Name = "Creative Solutions", Address = "91223 Texas Drive", Sales = 110058 },
new { ID = 5, Name = "Northwind", Address = "1 First Street", Sales = 11569 },
new { ID = 6, Name = "Alverson Utilities", Address = "890 Union Avenue", Sales = 78232 },
new { ID = 7, Name = "Northwind", Address = "1 First Street", Sales = 11569 },
new { ID = 8, Name = "Acme Products", Address = "123 Maple Drive", Sales = 23450 },
new { ID = 9, Name = "Creative Solutions", Address = "91223 Texas Drive", Sales = 110058 },
new { ID = 10, Name = "Northwind", Address = "1 First Street", Sales = 11569 },
new { ID = 11, Name = "Alverson Utilities", Address = "890 Union Avenue", Sales = 78232 },
new { ID = 12, Name = "Acme Products", Address = "123 Maple Drive", Sales = 23450 },
new { ID = 13, Name = "Creative Solutions", Address = "91223 Texas Drive", Sales = 110058 }
};
((RadGrid)s).DataSource = customers;
});
}
}
}
在此处查看文档以获取更多详细信息。
尝试将您的var grid = $find("RadGrid1")
内部封装在 $(document).ready() 函数中
$(document).ready(function() {
var grid = $find("RadGrid1");
});
对 ASP.NET 对象的客户端引用是在页面生成的最后创建的,因此您必须等到完成后 $find 才能为您提供所需的引用
还可以从这里熟悉 Telerik 网格的客户端 API: http ://www.telerik.com/help/aspnet-ajax/grdgettingfamiliarwithclientapi.html
迪克