gridview中怎样实现单击某行激发事件?

lizhiqiang5846 2006-11-02 03:05:24
gridview中,怎样实现 当鼠标停在某行时 鼠标指针变为 手,然后单击后激发事件,并且要知道鼠标点击的是哪一行?
...全文
249 5 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
shyao_2006 2006-11-02
  • 打赏
  • 举报
回复
关注这个问题!
to lytone52(给狼伴舞):
我测试过您给的程序,我的Gridview是动态赋的数据源,用模板列读值。按照您提供的代码,我读不到 row.Cells[2].Text 的值,不知道是不是与数据源的设置方式 有关?
wuxingmaxail 2006-11-02
  • 打赏
  • 举报
回复
前台

<script language="javascript">
function DbClickEvent(d)
{
window.alert("事件类型: DoubleClidk 作用对象: " + d);
}
function ClickEvent(d)
{
window.alert("事件类型: OnClick 作用对象: " + d);
}
function GridViewItemKeyDownEvent(d)
{
window.alert("事件类型: GridViewItemKeyDownEvent 作用对象: " + d);
}
function KeyDownEvent()
{
if( event.altKey && event.keyCode > 48 && event.keyCode < 54 )
{
window.alert("事件类型: FormKeyDownEvent 选中记录数: " + ( parseInt(event.keyCode) - 48 ));
}
}
</script>
wuxingmaxail 2006-11-02
  • 打赏
  • 举报
回复
if( e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标移动到每项时颜色交替效果
e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'");
e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#6699FF';this.style.color='#8C4510'");

//单击/双击 事件
e.Row.Attributes.Add("OnDblClick", "DbClickEvent('" + e.Row.Cells[1].Text + "')");
e.Row.Attributes.Add("OnClick", "ClickEvent('" + e.Row.Cells[1].Text + "')");

e.Row.Attributes.Add("OnKeyDown", "GridViewItemKeyDownEvent('" + e.Row.Cells[1].Text + "')");

//设置悬浮鼠标指针形状为"小手"
e.Row.Attributes["style"] = "Cursor:hand";

}
dotnba 2006-11-02
  • 打赏
  • 举报
回复
客户端脚本完成
lytone52 2006-11-02
  • 打赏
  • 举报
回复
<%@ Page language="C#" %>

<script runat="server">

void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{

// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;

// Display the company name from the selected row.
// In this example, the third column (index 2) contains
// the company name.
Message.Text = "You selected " + row.Cells[2].Text + ".";

}

void CustomersGridView_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{

// Get the currently selected row. Because the SelectedIndexChanging event
// occurs before the select operation in the GridView control, the
// SelectedRow property cannot be used. Instead, use the Rows collection
// and the NewSelectedIndex property of the e argument passed to this
// event handler.
GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex];

// You can cancel the select operation by using the Cancel
// property. For this example, if the user selects a customer with
// the ID "ANATR", the select operation is canceled and an error message
// is displayed.
if (row.Cells[1].Text == "ANATR")
{

e.Cancel = true;
Message.Text = "You cannot select " + row.Cells[2].Text + ".";

}

}

</script>

<html>
<body>
<form runat="server">

<h3>GridView Select Example</h3>

<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="true"
autogenerateselectbutton="true"
allowpaging="true"
selectedindex="0"
onselectedindexchanged="CustomersGridView_SelectedIndexChanged"
onselectedindexchanging="CustomersGridView_SelectedIndexChanging"
runat="server">

<selectedrowstyle backcolor="LightCyan"
forecolor="DarkBlue"
font-bold="true"/>

</asp:gridview>

<br/>

<asp:label id="Message"
forecolor="Red"
runat="server"/>

<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="CustomersSource"
selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>

</form>
</body>
</html>
------------------------------
相关推荐
发帖
.NET社区

6.1w+

社区成员

.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
帖子事件
创建了帖子
2006-11-02 03:05
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。