日期:2014-05-18  浏览次数:20365 次

怎么才能让单击gridview一行时获得某个列的值呢
我现在想做个按钮编辑功能,点击某行时获得其ID号然后点击按钮进行编辑

------解决方案--------------------
e.Row.Attribute.Add( "onclick ", "alert(this.cells[0].innerHTML) ");
------解决方案--------------------
给个Demo
//aspx
<%@ Page Language= "C# " AutoEventWireup= "true " CodeBehind= "GridViewDemo.aspx.cs " Inherits= "GridViewDemo " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<html xmlns= "http://www.w3.org/1999/xhtml " >
<head id= "Head1 " runat= "server ">
<title> 无标题页 </title>
<script type= "text/javascript ">
function setValue(a)
{
alert (a);
document.getElementById( "Hidden1 ").value = a;
}
</script>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
&nbsp; <asp:SqlDataSource ID= "SqlDataSource1 " runat= "server " ConnectionString= "Data Source=.\SQLexpress;Initial Catalog=pubs;Integrated Security=True "
ProviderName= "System.Data.SqlClient " SelectCommand= "SELECT * FROM [authors] ">
</asp:SqlDataSource>
<asp:GridView ID= "GridView1 " runat= "server " DataKeyNames= "au_id " AutoGenerateColumns= "False " DataSourceID= "SqlDataSource1 " OnRowDataBound= "GridView1_RowDataBound " >
<Columns>
<asp:BoundField DataField= "au_id " HeaderText= "au_id <br> abc " ReadOnly= "True " SortExpression= "au_id " HtmlEncode= "false " />
<asp:BoundField DataField= "au_lname " HeaderText= "au_lname " SortExpression= "au_lname " />
<asp:BoundField DataField= "au_fname " HeaderText= "au_fname " SortExpression= "au_fname " />
<asp:BoundField DataField= "phone " HeaderText= "phone " SortExpression= "phone " />
<asp:BoundField DataField= "address " HeaderText= "address " SortExpression= "address " />
<asp:BoundField DataField= "city " HeaderText= "city " SortExpression= "city " />
<asp:BoundField DataField= "state " HeaderText= "state " SortExpression= "state " />
<asp:BoundField DataField= "zip " HeaderText= "zip " SortExpression= "zip " />
<asp:CheckBoxField DataField= "contract " HeaderText= "contract " SortExpression= "contract " />
</Columns>
</asp:GridView>
<asp:Button ID= "Button1 " runat= "server " OnClick= "Button1_Click " Text= "Button " />
<input id= "Hidden1 " name= "Hidden1 " type= "hidden " /> </div>
</form>
</body>
</html>

//aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.U