惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
IT之家
IT之家
Jina AI
Jina AI
D
DataBreaches.Net
V
Visual Studio Blog
腾讯CDC
雷峰网
雷峰网
博客园 - 【当耐特】
Y
Y Combinator Blog
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
MongoDB | Blog
MongoDB | Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
MyScale Blog
MyScale Blog
爱范儿
爱范儿
博客园 - Franky
有赞技术团队
有赞技术团队
Engineering at Meta
Engineering at Meta

博客园 - 释天

4年后新的开始 flash make things move 电子书 破解pubwin ep html学习笔记(-) js学习笔记(-) - 释天 - 博客园 flex2注册码 Flex2.0和C#间的通信[转] - 释天 - 博客园 flash与.net的联合开发(c#,c++)请大家讨论一下 flash中三维动画学习(关于类) flash中一个二维的类 经典言论 c#解答题参考答案 [求教]vs2005下关于css的问题 一个关于文件操作的类(功能还是有点弱) 写了个烂代码,请原谅 C#中接口(interface)的理解 赠送gmail邀请 三个范式的解释 iframe的使用
Gridview的使用
释天 · 2006-09-15 · via 博客园 - 释天

由于初学asp.net2.0,写得不好,请见谅,
由于在写一个文章编辑的页面,使用了gridview这个控件,在itemtemplate里添加了一个linkbutton来
链接到其他页面,但是试了好几种方法,之一使用parent这个属性成功了

代码如下:

 protected void reedit_Click(object sender, EventArgs e)
    
{
        LinkButton lb 
= (LinkButton)sender;  
        DataControlFieldCell df
=(DataControlFieldCell)lb.Parent;
        GridViewRow grv 
= (GridViewRow)df.Parent;
        GridView1.SelectedIndex 
= grv.RowIndex;
        
int tt=grv.RowIndex ;
        
string id;
        id
=GridView1.Rows [tt].Cells [0].Text .ToString ();
        Response.Write(id);
        Message.Text 
= "The primary key value of the selected row is" + GridView1.SelectedDataKey.Values[2];
        Response.Redirect(xxx.aspx?id);
    }

}
 

这样就选取了在gridview的所选列的的数值,(由于我在操作数据库的时候删了一些数据,致使数据的序列不完整,SelectedIndex 获取或设置 GridView 控件中的选中行的索引 ,就不等于在表中的id值了)
同时当我使用selecteddatakey.value.tostring();

 Message.Text = "The primary key value of the selected row is " 
    CustomersGridView.SelectedDataKey.Value.ToString()
 

也可以使用也得到了所选行的数值,

Message.Text = "The primary key value of the selected row is " & _
      CustomersGridView.SelectedValue.ToString() 
& "."

这个只能使用到第一字段,
如果要使用其他字段,就要用到 SelectedDataKey 属性
若要访问第二个键,请将 GridView1.SelectedDataKey[1] 的值用作 DetailsView 控件的 SqlDataSource 控件的 ControlParameter 对象的 PropertyName