vs2005 GridView控件怎么选中一行??

tiger波波 2009-02-02 11:16:15
现在能在GridView控件里启用选定内容来选定一行,但每次都要点那个选中按钮,有没有什么办法,不启用选定内容,而是通过代码来选定一行,就是说没有选中按钮,而是用鼠标点中哪行就选择哪行,谢谢
...全文
325 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hsinbill 2009-02-02
  • 打赏
  • 举报
回复
QQ群:323373
blackmeit 2009-02-02
  • 打赏
  • 举报
回复
HTML:
<script type="text/javascript">
var oldrow=null;
var currentcolor=null;
var oldcolor=null;
function selectx(row)
{
if(oldrow!=null)
{
oldrow.style.backgroundColor=oldcolor;
}
row.style.backgroundColor='#99ccff';
oldrow=row;
oldcolor = currentcolor;
}
</script>

.cs:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "if(this!=oldrow){currentcolor=this.style.backgroundColor;this.style.backgroundColor='PeachPuff',this.style.fontWeight='';}");
e.Row.Attributes.Add("onmouseout", "if(this!=oldrow){this.style.backgroundColor=currentcolor;this.style.fontWeight='';}");
e.Row.Attributes.Add("onclick", e.Row.ClientID.ToString() + ".checked=true;selectx(this)");
}
}
代码改变世界 2009-02-02
  • 打赏
  • 举报
回复
现在有空。。。随便给你写个简单的例子好了
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="CMET.Web.test" %>

<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type " content="text/html; charset=gb2312 ">
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>
</asp:GridView>
</form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace CMET.Web
{
public partial class test : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("ID", typeof(string));
DataColumn dc2 = new DataColumn("Name", typeof(string));
dt.Columns.Add(dc);
dt.Columns.Add(dc2);

for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
dr["ID"] = i.ToString();
dr["Name"] = "Name" + i.ToString();
dt.Rows.Add(dr);
}
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add("onclick", "window.alert('您选择了第"+(e.Row.RowIndex+1).ToString()+"行');");
}
}
}
tiger波波 2009-02-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 blackmeit 的回复:]
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowType != DataControlRowType.Header)
{
e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#ff0000'");
}
}
}
[/Quote]
谢谢,这个可以实现。但是效果不太一样,选中了第二个,第一行颜色没变回来
blackmeit 2009-02-02
  • 打赏
  • 举报
回复
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowType != DataControlRowType.Header)
{
e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#ff0000'");
}
}
}
tiger波波 2009-02-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 Zyb_LoveC 的回复:]
可以在gridview的datarow添加属性,onclick,在onclick方法里写你要触发点击某一行时触发的事件
[/Quote]

gridview里没有datarow这个属性啊
代码改变世界 2009-02-02
  • 打赏
  • 举报
回复
可以在gridview的datarow添加属性,onclick,在onclick方法里写你要触发点击某一行时触发的事件

62,269

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

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

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

试试用AI创作助手写篇文章吧