GridView中使用DataKeyNames存储数据键值
来源:互联网 发布:机器翻译 软件 编辑:程序博客网 时间:2024/06/12 07:01
源文转自:http://www.cnblogs.com/heekui/archive/2008/04/09/1143831.html
很多时候我们需要在GridView的RowCommand之类的事件中需要获取当前行的一些关联性的数据值。但这些数据值又没有直接体现在GridView的列中。这个时候该怎么办呢?
有同学喜欢用隐藏列的方式,把需要使用但不显示的字段绑定到此列上,同时设置列宽为0或不显示,使用时可以用常规的取某行某列的方式来获取数据。
但是在Framework 2.0中,我们可以采用DataKeyNames的方式来获取此类数据。
代码示例:
(前台)
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("GrupName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField Text="按钮" />
</Columns>
</asp:GridView>
Grup 为我们想使用但不需要显示的列。(如果有多个字段,使用逗号分开)
(后台)
{
if (!IsPostBack )
{
DataTable dt = new DataTable();
dt.Columns.Add("Grup");
dt.Columns.Add("GrupName");
dt.Rows.Add(new object[] { 0,"营业部" });
dt.Rows.Add(new object[] { 1,"市场部" });
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
// 获取当前行索引
int index = Convert.ToInt32(e.CommandArgument);
// 取出当前行数据键值对象中的值
string strGrup = ((GridView)sender).DataKeys[index].Values["Grup"].ToString();
}
顺便补充一句。
如果你使用模板列中放置按钮控件的方式,要想在按钮事件中获取这种字段值就更简单了。
只需要在按钮的CommandArgument属性设置为想绑定的字段,如:
<ItemTemplate>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" CommandArgument=' <%#Eval("Grup") %>' />
</ItemTemplate>
</asp:TemplateField>
按钮事件中如是写:
{
string strGrup = ((Button)sender).CommandArgument.ToString();
}
- GridView中使用DataKeyNames存储数据键值
- GridView中使用DataKeyNames存储数据键值
- GridView中使用DataKeyNames存储数据键值
- GridView 中DataKeyNames 存储数据键值
- C# GridView 中DataKeyNames 存储数据键值(转)
- 2005中GridView 的DataSource中存储过程用到的参数和DataKeyNames的冲突
- GridView中DataKeyNames的应用小结
- GridView中DataKeyNames的应用小结
- GridView中DataKeyNames的应用小结
- GridView控件的DataKeyNames
- GridView的DataKeyNames属性
- GridView.DataKeyNames 属性
- GridView中使用存储过程分页
- 在PHP中使用键值对数组构造JSON数据
- GridView的DataKeyNames属性(转)
- GridView的DataKeyNames的一些用法
- GridView的DataKeyNames和DataKeys属性
- 【转】GridView的DataKeyNames的一些用法
- POJ2553解题报告 强连通分支
- 编译64位版本的boost
- 常见的几个Qt编程问题的处理
- ASP.NET获取IP的6种方法
- Jakarta commons——Collections
- GridView中使用DataKeyNames存储数据键值
- 汇出数据表字段(&属性)清单
- C++读写文本文件
- Avalon接口类型浅析——Avalon-MM接口与Avalon-ST接口辨析
- Qt中使用QtSql问题
- 彩色UML学习笔记
- C++中的内联函数
- 如何判断浏览器版本
- 恩开始在这里写blog了