Gridview的使用

由于初学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
posted @ 2006-09-15 08:51  释天  阅读(318)  评论(1编辑  收藏  举报