ASP.NET2005 gridview中点击某行的任意位置选中某行 交替效果 变换鼠标手势
在 GridView中增加选择按钮SELECT,并让其不显示
<Columns>
<asp:CommandField ShowSelectButton="True" Visible="False" />
</Columns>
为gridview添加RowDataBound事件的代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标移动到每项时颜色交替效果
e.Row.Attributes["onmouseover"]= "e=this.style.backgroundColor;this.style.backgroundColor='#cccccc'";
e.Row.Attributes["onmouseout"]= "this.style.backgroundColor=e";
//设置悬浮鼠标指针形状为"小手"
e.Row.Attributes["style"] = "Cursor:hand";
//鼠标点击某行即选中某行
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标移动到每项时颜色交替效果
e.Row.Attributes["onmouseover"]= "e=this.style.backgroundColor;this.style.backgroundColor='#cccccc'";
e.Row.Attributes["onmouseout"]= "this.style.backgroundColor=e";
//设置悬浮鼠标指针形状为"小手"
e.Row.Attributes["style"] = "Cursor:hand";
//鼠标点击某行即选中某行
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
}
}
//获取选定行的指写数据
protected void GVData_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
try
{
currentNo = this.GVData.Rows[e.NewSelectedIndex].Cells[1].Text.ToString();
currentItem = this.GVData.Rows[e.NewSelectedIndex].Cells[6].Text.ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
{
try
{
currentNo = this.GVData.Rows[e.NewSelectedIndex].Cells[1].Text.ToString();
currentItem = this.GVData.Rows[e.NewSelectedIndex].Cells[6].Text.ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
运行的时候可能会出现“invalid postback or callback argument”的错误提示,只要将aspx页面<%@ Page%>上加上属性 EnableEventValidation="false" 就可以了。
注:好多网上找的代码是这样的,不用能,大家注意,代码如下:
//单击行的任意列会自动选中此行,加在RowCreated事件中也可以
e.Row.Attributes.Add("onclick", "__doPostBack('GridView1','Select$" + e.Row.RowIndex + "')");
e.Row.Attributes.Add("onclick", "__doPostBack('GridView1','Select$" + e.Row.RowIndex + "')");