1.增加GridView的GVSelect_RowDataBound事件
protected void GVSelect_RowDataBound(object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType ==DataControlRowType.DataRow)
{
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#8EC26F'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout","this.style.backgroundColor=c");
//设置悬浮鼠标指针形状为"小手"
e.Row.Attributes["style"] ="Cursor:hand";
//单击/双击 事件
e.Row.Attributes.Add("OnClick", "ClickEvent('" + e.Row.Cells[10].FindControl("btnDetial").ClientID + "')");
//注:OnClick参数是指明为鼠标单击时间,后个是调用javascript的ClickEvent函数
}
}
2.在页面的HTML里添加javascript函数,用来响应鼠标点击事件,因为ASP客户端不能主动调用服务端的函数,我在这里在Gridview添加一个辅助Button列,然后在ClickEvent(cId)函数中,调用这个Button的单击事件。
<scriptlanguage="javascript">
function ClickEvent(cId)
{
//调用LinkButton的单击事件,btnBindData是LinkButton的ID
document.getElementById(cId).click();
}
</script>
3.响应事件
protected void gdvTaskList_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Detail")
{
//在这里对你需要的数据信息进行输出
SetClientInfo();//我的处理函数
}
}
GridView鼠标停留变色和单击处理事件,当鼠标在GridView的行上停留时,将该行变色,当单击该行时,做相应处理,记得要在<gridview ></gridview> 之间 加上RowDataBound=GVSelect_RowDataBound 属性
注释:
添加辅助列方法
<asp:TemplateField ItemStyle-CssClass="showcell" HeaderStyle-CssClass="showcell">
<ItemTemplate>
<asp:LinkButton runat="server" ID="btnDetial" CommandName="Detail" CommandArgument='<%# Eval("ID") %>'>辅助列</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
样式:
.showcell{
DISPLAY:none;
}
单击行后改变行样式,需在页面增加
<script language="javascript">
var prevselitem=null;
function selectx(row)
{
if(prevselitem!=null)
{
prevselitem.style.backgroundColor='#ffffff';
}
row.style.backgroundColor='#8EC26F';
prevselitem=row;
}
</script>
调用方法,将后台语句
//单击/双击 事件
e.Row.Attributes.Add("OnClick", "ClickEvent('" + e.Row.Cells[10].FindControl("btnDetial").ClientID + "')");
改为:
e.Row.Attributes.Add("OnClick", "ClickEvent('" + e.Row.Cells[10].FindControl("btnDetial").ClientID + "');selectx(this)");
protected void GVSelect_RowDataBound(object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType ==DataControlRowType.DataRow)
{
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#8EC26F'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout","this.style.backgroundColor=c");
//设置悬浮鼠标指针形状为"小手"
e.Row.Attributes["style"] ="Cursor:hand";
//单击/双击 事件
e.Row.Attributes.Add("OnClick", "ClickEvent('" + e.Row.Cells[10].FindControl("btnDetial").ClientID + "')");
//注:OnClick参数是指明为鼠标单击时间,后个是调用javascript的ClickEvent函数
}
}
2.在页面的HTML里添加javascript函数,用来响应鼠标点击事件,因为ASP客户端不能主动调用服务端的函数,我在这里在Gridview添加一个辅助Button列,然后在ClickEvent(cId)函数中,调用这个Button的单击事件。
<scriptlanguage="javascript">
function ClickEvent(cId)
{
//调用LinkButton的单击事件,btnBindData是LinkButton的ID
document.getElementById(cId).click();
}
</script>
3.响应事件
protected void gdvTaskList_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Detail")
{
//在这里对你需要的数据信息进行输出
SetClientInfo();//我的处理函数
}
}
GridView鼠标停留变色和单击处理事件,当鼠标在GridView的行上停留时,将该行变色,当单击该行时,做相应处理,记得要在<gridview ></gridview> 之间 加上RowDataBound=GVSelect_RowDataBound 属性
注释:
添加辅助列方法
<asp:TemplateField ItemStyle-CssClass="showcell" HeaderStyle-CssClass="showcell">
<ItemTemplate>
<asp:LinkButton runat="server" ID="btnDetial" CommandName="Detail" CommandArgument='<%# Eval("ID") %>'>辅助列</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
样式:
.showcell{
DISPLAY:none;
}
单击行后改变行样式,需在页面增加
<script language="javascript">
var prevselitem=null;
function selectx(row)
{
if(prevselitem!=null)
{
prevselitem.style.backgroundColor='#ffffff';
}
row.style.backgroundColor='#8EC26F';
prevselitem=row;
}
</script>
调用方法,将后台语句
//单击/双击 事件
e.Row.Attributes.Add("OnClick", "ClickEvent('" + e.Row.Cells[10].FindControl("btnDetial").ClientID + "')");
改为:
e.Row.Attributes.Add("OnClick", "ClickEvent('" + e.Row.Cells[10].FindControl("btnDetial").ClientID + "');selectx(this)");