在OnRowDataBound或OnItemDataBound事件中获取字段值
不管是在GridView,DataList还是Repeater控件中,其中Repeater控件,没有DataKeyNames或是DataKeyField属性,想获取记录的主键值,只好用Label或是HiddenField绑定主键字段,然后在后台OnItemDataBound获取。
虽然GridView和DataList控件有DataKeyNames和DataKeyField属性,或是某时候程序需求,一些非主键字段需要进一步函数处理而需要获取这些字段。
现Insus.NET教大家几种方法,在OnRowDataBound或是OnItemDataBound事件获取主键值或是字段值。如果是GridView控件或是DataList控件,需要设置属性DataKeyNames和DataKeyField,而Repeater控件,只能使用HiddenField控件了。
先来看看GridView控件:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="LabelName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
去.aspx.cs网页,写GridView1_RowDataBound事件。
当然想在OnRowDataBound获取主键时,不用设置DataKeyNames属性,也可以跟获取其它字段一样获取得到,下面是第二种方法:
第三种方法:

下面是DataList控件演示,
<asp:DataList ID="DataList1" runat="server" DataKeyField="ID" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<asp:Label ID="LabelName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>
第一种:
第二种方法:
第三种方法:
下面是Repeater控件在OnItemDataBound事件中,获取字段值。
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<HeaderTemplate>
<table cellpadding="3" cellspacing="0" style="border: 1px solid #808080; border-collapse: collapse; width: 500px;">
<tr>
<td style="border: 1px solid #808080;">主键
</td>
<td style="border: 1px solid #808080;">Name
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="border: 1px solid #808080;">
<%# Eval("ID") %>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("ID") %>' />
</td>
<td style="border: 1px solid #808080;">
<asp:Label ID="LabelName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
第一种方法:
第二种方法:
第三种方法:
在OnRowDataBound或OnItemDataBound事件中获取字段值的更多相关文章
- GridView控件RowDataBound事件中获取列字段值的几种途径
前台: <asp:TemplateField HeaderText="充值总额|账号余额"> <ItemTemplate> <asp:Label ID ...
- Django如何从Model中获取字段名称——verbose_name
一.背景 CRM项目重新总结一下,重写之前的项目发现有不少知识点已经忘记,所以特此来重新总结一下一便后续能够回忆起 二.代码分析 1. 核心代码 from django.conf.urls impor ...
- 【Java/JDBC】利用ResultSetMetaData从数据库的某表中获取字段信息并存到csv文件
代码下载:https://files.cnblogs.com/files/xiandedanteng/FindNotnullColumns20191102-2.rar 这篇还不够完善,请看更完善的续篇 ...
- EHlib在数据单元中显示字段值为图形。
-[定制网格数据单元] 在数据单元中显示字段值为图形. TDBGridEh allows to show bitmaps from TImageList component depending o ...
- MySQL数据库中tinyint字段值为1,读取出来为true的问题
原文:https://blog.csdn.net/shuyou612/article/details/46788475 MySQL数据库中tinyint字段值为1,读取出来为true的问题 今天在 ...
- GridView控件RowDataBound事件中获取列字段值的几种途径 !!!
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == ...
- GridView控件RowDataBound事件中获取列字段途径
今天不知道怎么回事怎么也找不到gridview列中的控件,关键是其为编辑时隐藏域中的控件,取值就很成问题了,网上搜了很到,找到这个比较经典的东东了,可能大家都知道,但很少对比整理到一起,有多种方法可以 ...
- GridView的RowDataBound事件中获取当前行内容的几种方法
1. Cells[x].Txt. 从列单元格的文本值获取.这种方法简单高率,最为常用,但是功能单纯.此法存在几个缺点: (1)无法获取到设置了隐藏属性的数据列的值,所取到的值为“”(空). ...
- 【特性】select语句中使用字符串链接获取字段值失败
坑1 在一个多行的表中,想把其中的一个字段值拿出来,组成一个字符串供后面使用. 按照以往,自己就如以下这么写了: declare @sql varchar(8000) set @sql='insert ...
随机推荐
- git revert reset
git revert是用一次新的commit来回滚之前的commit,git reset是直接删除指定的commit. git reset 是把HEAD向后移动了一下,而git revert是HEAD ...
- Celery-4.1 用户指南: Optimizing (优化)
简介 默认的配置做了很多折中考虑.它不是针对某个情况优化的,但是大多数情况下都工作的非常好. 基于一个特殊的使用场景,有很多优化可以做. 优化可以应用到运行环境的不同属性,可以是任务执行的时间,使用的 ...
- angular之增删改查
<!DOCTYPE html> <html lang="en" ng-app="app"> <head> <meta ...
- pa15-三省吾身
序号 项 1 凡事提前10分钟 凡事提前10分钟,会让你有充裕的时间应对可能的突发事件,更加从容. 试着把起床闹钟提前10分钟,你就会发现你出门不必急匆匆,早饭也可慢慢享用,一整天的状态也 ...
- rsync mac->windows openssh
rsync -azvP --progress -e "ssh -p 6666" /Users/codar/360\344\272\221\347\233\230/ghld/ rsy ...
- Oracle Management Packs
http://kerryosborne.oracle-guy.com/2008/10/oracle-management-packs/ There has been quite a bit of co ...
- #测试两种不同的SVM,rbf的核真是太棒了(一种会拐弯的边界)
from sklearn import datasets import numpy as np X, y = datasets.make_blobs(n_features=2, centers=2) ...
- HTML元素(包括HTML5)的标签、属性及事件大全
1.标签列表 标签 描述 <!–…–> 定义注释. <!DOCTYPE> 定义文档类型. <a> 定义超链接. <abbr> 定义缩写. <acr ...
- css垂直居中方法(一)
第一种方法:首先用margin:0 auto实现水平居中,然后设置position:relative,设置top为50%(父元素高度的50%),然后设置margin-top:-150px(设置负值是因 ...
- iter创建一个可以被迭代的对象
#!/usr/bin/env python obj = iter([11,22,33,44]) #iter 创建一个可以被迭代的对象 print(obj) r1 = next(obj) print(r ...