DataKey 类用于表示数据绑定控件中某个记录的主键。记录的主键可以由数据源中的一个或多个字段组成。尽管 DataKey 类不是集合,但它可以存储多个键字段值。当调用 DataKey 类的某个构造函数

时,将填充键字段值。可以通过以下方法从 DataKey 对象中检索键字段值:

使用 DataKey.Item(Int32) 属性检索 DataKey 对象中特定索引位置的键字段值。

使用 DataKey.Item(String) 属性检索特定字段的键字段值。

使用 Value 属性检索 DataKey 对象中索引 0 位置的键字段值。当主键只包含一个字段时,此属性常用作检索记录键值的快捷方式。

使用 Values 属性创建可用于循环访问键字段值的 IOrderedDictionary 对象。

通常,当设置了数据绑定控件的 DataKeyNames 属性时,控件自动生成 DataKey 对象。DataKey 对象包含 DataKeyNames 属性中指定的一个或多个键字段的值。
一次显示一个记录的数据绑定控件(如 DetailsView 或 FormView)通常在它的 DataKey 属性中存储所显示的当前记录的 DataKey 对象。
一次显示多个记录的数据绑定控件(如 GridView) 通常在DataKeys 属性中存储DataKeyArray集合,该集合存储它的每个记录的 DataKey 对象。

正常情况下,DataKeys 属性用于检索 GridView 控件中特定数据行的 DataKey 对象。但是,如果您只需要检索当前选中行的 DataKey 对象,则可以简单地使用 SelectedDataKey 属性作为一种快捷方式。 这和从 DataKeys 集合检索位于指定索引(由 SelectedIndex 属性指定)位置的 DataKey 对象一样。还可以使用 SelectedValue 属性直接检索当前选中行的数据键值。

下面的代码示例演示如何使用 SelectedDataKey 属性来确定 GridView 控件中选中行的数据键值。


代码

<%@ Page language="C#" %>

<script runat="server">
  void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)  
  {        
    // Display the primary key value of the selected row.
    Message.Text = "The primary key value of the selected row is " +
      CustomersGridView.SelectedDataKey.Value.ToString() + ".";    
  }
</script> <html>
  <body>
    <form runat="server">        
      <h3>GridView SelectedDataKey Example</h3>            
      <asp:label id="Message"
        forecolor="Red"
        runat="server"/>                
      <br/><br/>
      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        allowpaging="true"
        autogeneratecolumns="true"
        autogenerateselectbutton="true"    
        datakeynames="CustomerID"
        onselectedindexchanged="CustomersGridView_SelectedIndexChanged"   
        runat="server">                
        <selectedrowstyle backcolor="LightBlue"
          forecolor="DarkBlue"/>               
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>            
    </form></body></html>

下面的代码示例演示如何在主/从方案中将第二个键字段作为参数使用。使用 GridView 控件显示 Northwind 数据库的 Order Details 表中的记录。在 GridView 控件中选择某一记录时,将在

DetailsView 控件中显示来自 Products 表的产品详细信息。ProductID 是 GridView 控件中的第二个键名称。若要访问第二个键,请将 GridView1.SelectedDataKey[1] 的值用作 DetailsView 控件的

SqlDataSource 控件的 ControlParameter 对象的 PropertyName。


代码

<%@ Page Language="C#" %>

<!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>Selecting Data Key Values</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:SqlDataSource 
      ID="SqlDataSource1" 
      runat="server" 
      ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString%>"
      ProviderName="System.Data.SqlClient" 
      SelectCommand="SELECT * FROM [Order Details]">
    </asp:SqlDataSource>     <asp:SqlDataSource 
      ID="SqlDataSource2" 
      runat="server" 
      ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString%>"
      ProviderName="System.Data.SqlClient" 
      SelectCommand="SELECT * FROM [Products]WHERE [ProductID] = @productid">
      <SelectParameters>
        <asp:ControlParameter 
          Name="productid" 
          ControlID="GridView1" 
          PropertyName="SelectedDataKey[1]" />
      </SelectParameters>
    </asp:SqlDataSource>
    
  </div>   <asp:GridView 
    ID="GridView1" 
    runat="server" 
    AllowPaging="True" 
    AutoGenerateColumns="False"
    DataKeyNames="OrderID,ProductID" DataSourceID="SqlDataSource1">
    <Columns>
      <asp:CommandField ShowSelectButton="True" />
      <asp:BoundField DataField="OrderID" HeaderText="OrderID" ReadOnly="True"/>
      <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" />
      <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
      <asp:BoundField DataField="Quantity" HeaderText="Quantity" />
      <asp:BoundField DataField="Discount" HeaderText="Discount" />
      </Columns>
    </asp:GridView>
    <br />
    <asp:DetailsView 
      ID="DetailsView1" 
      runat="server" 
      AutoGenerateRows="False" 
      DataKeyNames="ProductID"
      DataSourceID="SqlDataSource2" 
      Height="50px" Width="125px">
      <Fields>
        <asp:BoundField 
          DataField="ProductID" 
          HeaderText="ProductID" 
          InsertVisible="False"
          ReadOnly="True" />
        <asp:BoundField DataField="ProductName" HeaderText="ProductName"/>
        <asp:BoundField DataField="SupplierID" HeaderText="SupplierID"/>
        <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
        <asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" />
        <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
        <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" />
        <asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" />
        <asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel" />
        <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" />
      </Fields>
    </asp:DetailsView>
  </form>
</body>
</html>

【转自】http://www.cnblogs.com/Fskjb/archive/2010/05/02/1725887.html

DatakeyNames和datakey的更多相关文章

  1. GridView中DataKeyNames的应用小结

    一. GridView的DataKeyNames属性设为"ID,Name" GridView1.DataKeyNames = new string[]{ "ID" ...

  2. GridView绑定DataKeyNames以及如何取这些值

    DataKeyNames='FID'   //前台绑定一个值GridView1.DataKeys[e.Row.RowIndex].Value.ToString;-------------------- ...

  3. Gridview中DataKeyNames 设置多个主键 取值

    1.设置DataKeyNames a.F4  在属性面板中设置   多个值以逗号隔开  例如id,mane,sex b.通过后台代码 this.gridview.DataSource = Bind() ...

  4. 取得GridView某行的DataKey

    首先绑定DataKeyNames GridView.DataKeyNames = new string[] { "字段名称" }; 取值 string aaa= GridView. ...

  5. data-key

    在foreach或者each循环中给按钮赋予值 html中:data-key="@config.key" js里获取值: var key = $(this).data(" ...

  6. GridView设置多个DatakeyNames

    1.aspx页面GridView直接绑定DataKeyNames aspx设置: <asp:GridView ID="grvGrid" runat="server& ...

  7. 在HTML5规范中div中读取预存的data-[key]值

    HTML 代码: <div id="div_test" data-test="this is test" ></div> jQuery ...

  8. GridView主键列不让编辑时应该修改属性DataKeyNames

    原文发布时间为:2008-08-02 -- 来源于本人的百度文章 [由搬家工具导入] 为了防止GridView主键被编辑,应该在GridView属性DataKeyNames里面写上主键

  9. GridView详细介绍

    GridView控件的属性 表10.6 GridView控件的行为属性属性描述AllowPaging指示该控件是否支持分页.AllowSorting指示该控件是否支持排序.AutoGenerateCo ...

随机推荐

  1. 学习笔记CB007:分词、命名实体识别、词性标注、句法分析树

    中文分词把文本切分成词语,还可以反过来,把该拼一起的词再拼到一起,找到命名实体. 概率图模型条件随机场适用观测值条件下决定随机变量有有限个取值情况.给定观察序列X,某个特定标记序列Y概率,指数函数 e ...

  2. Linux第十节课学习笔记

    部署LVM三步: 1.pv:使设备支持LVM: 2.vg:对支持LVM的设备进行整合: 3.lv:将整合的空间进行切割. 每个基本单元PE的大小为4M,分配空间必须是4M的整数倍.可以容量或基本单元个 ...

  3. [随笔][Java][总结][java 类型系统]

    java 的类型系统大体分为两类,对象和基本类型.java使用静态类型检查来保证类型安全.每个变量在使用之前需要声明.非静态类型的语言不要求变量在使用之前进行声明. 基本数据类型 java的基本类型不 ...

  4. python发送短信和发送邮件

    先注册好 发短信脚本内容 #接口类型:互亿无线触发短信接口,支持发送验证码短信.订单通知短信等. #账户注册:请通过该地址开通账户http://sms.ihuyi.com/register.html ...

  5. mysql用户管理及授权

    以mariadb5.5版本为例 新建用户 登录mariadb # mysql -uroot -p Enter password: Welcome to the MariaDB monitor. Com ...

  6. 检查MySQL内存使用情况

    ==================================================================================================== ...

  7. 配置Tomcat使用https协议(配置SSL协议)

    配置Tomcat使用https协议(配置SSL协议) 2014-01-20 16:38 58915人阅读 评论(3) 收藏 举报 转载地址:http://ln-ydc.iteye.com/blog/1 ...

  8. Qt控制流简析

    前言: Qt库及其绑定python语言的PySide库.PyQt库在圈中已经是TD的标配了,Qt提供了多种快速绘制图形窗口的方式.但正是因为这个原因,导致很多TD局限在设计窗口外观的桎梏中,而忽略了Q ...

  9. RN 各种小问题

    问题:vs code 无法启动 双击无反应 方法:控制台 输入 netsh winsock reset 重启 vs 问题:RN Debugger Network 不显示 源网页:https://git ...

  10. SSD硬盘测速较低的原因备忘

    SATA3 SSD测速度盘速度只有200MB/s,可能原因有: 原因分为几种:没开AHCI 没有4K对齐 虽然接的是SATA3接口但SATA3有分为3G和6G这些传输速度接口的分别,同理SATA线3G ...