Gridview的使用

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

代码如下:

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

None.gif}
None.gif 
这样就选取了在gridview的所选列的的数值,(由于我在操作数据库的时候删了一些数据,致使数据的序列不完整, SelectedIndex 获取或设置 GridView 控件中的选中行的索引 ,就不等于在表中的id值了)
同时当我使用selecteddatakey.value.tostring();
None.gif  Message.Text  =   " The primary key value of the selected row is  "  
    CustomersGridView.SelectedDataKey.Value.ToString()
 
None.gif
也可以使用也得到了所选行的数值,
None.gif Message.Text  =   " The primary key value of the selected row is  "   &  _
None.gif      CustomersGridView.SelectedValue.ToString() 
&   " . "
None.gif
这个只能使用到第一字段,
如果要使用其他字段,就要用到 SelectedDataKey 属性
若要访问第二个键,请将 GridView1.SelectedDataKey[1] 的值用作 DetailsView 控件的 SqlDataSource 控件的 ControlParameter 对象的 PropertyName

转载于:https://www.cnblogs.com/kttt/archive/2006/09/15/504762.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值