gridview在鼠标点击行时,怎么获取某列的值

 我来答
逆长小白菜
2016-03-08 · TA获得超过2万个赞
知道大有可为答主
回答量:1.7万
采纳率:94%
帮助的人:7505万
展开全部
我用的是asp.net,GridView实现它的RowDataBound事件,具体看代码,希望可以帮到你:
前台:
<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
<title></title>
<!--点击GridView任意行的位置,获取该行的ID值-->
<script type="text/javascript">
function getId(row_index) {
var grid_view = document.getElementById('<%=GridView1.ClientID %>');
var rows = grid_view.rows;
var personID = rows[row_index].cells[0].innerHTML;
alert("获取的ID为:" + personID);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" HeaderStyle-Width="300px" ItemStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="LightSkyBlue" />
<asp:BoundField HeaderText="Name" DataField="Name" HeaderStyle-Width="300px" ItemStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="LightSkyBlue" />
<asp:BoundField HeaderText="Tel" DataField="Tel" HeaderStyle-Width="300px" ItemStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="LightSkyBlue" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

后台代码:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<Person> list = new List<Person>();
list.Add(new Person("001", "张三"));
list.Add(new Person("002", "李四"));
list.Add(new Person("003", "王五"));
list.Add(new Person("004", "赵六"));
list.Add(new Person("005", "何七"));
GridView1.DataSource = list;
GridView1.DataBind();
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int row_index = e.Row.RowIndex + 1;
e.Row.Attributes.Add("onclick", "getId(" + row_index + ");");
}
}
}

class Person
{
public Person(string id, string name)
{
ID = id;
Name = name;
}

private string id;

public string ID
{
get { return id; }
set { id = value; }
}
private string name;

public string Name
{
get { return name; }
set { name = value; }
}
private string tel;

public string Tel
{
get { return tel; }
set { tel = value; }
}
}
安贞高峰
2018-04-12 · TA获得超过3068个赞
知道小有建树答主
回答量:2680
采纳率:75%
帮助的人:185万
展开全部
我用的是asp.net,GridView实现它的RowDataBound事件,具体看代码,希望可以帮到你:
前台:
<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
<title></title>
<!--点击GridView任意行的位置,获取该行的ID值-->
<script type="text/javascript">
function getId(row_index) {
var grid_view = document.getElementById('<%=GridView1.ClientID %>');
var rows = grid_view.rows;
var personID = rows[row_index].cells[0].innerHTML;
alert("获取的ID为:" + personID);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" HeaderStyle-Width="300px" ItemStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="LightSkyBlue" />
<asp:BoundField HeaderText="Name" DataField="Name" HeaderStyle-Width="300px" ItemStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="LightSkyBlue" />
<asp:BoundField HeaderText="Tel" DataField="Tel" HeaderStyle-Width="300px" ItemStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="LightSkyBlue" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

后台代码:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<Person> list = new List<Person>();
list.Add(new Person("001", "张三"));
list.Add(new Person("002", "李四"));
list.Add(new Person("003", "王五"));
list.Add(new Person("004", "赵六"));
list.Add(new Person("005", "何七"));
GridView1.DataSource = list;
GridView1.DataBind();
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int row_index = e.Row.RowIndex + 1;
e.Row.Attributes.Add("onclick", "getId(" + row_index + ");");
}
}
}

class Person
{
public Person(string id, string name)
{
ID = id;
Name = name;
}

private string id;

public string ID
{
get { return id; }
set { id = value; }
}
private string name;

public string Name
{
get { return name; }
set { name = value; }
}
private string tel;

public string Tel
{
get { return tel; }
set { tel = value; }
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式