vs2005 GridView控件怎么选中一行?? 现在能在GridView控件里启用选定内容来选定一行,但每次都要点那个选中按钮,有没有什么办法,不启用选定内容,而是通过代码来选定一行,就是说没有选中按钮,而是用鼠标点中哪行就选择哪行,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以在gridview的datarow添加属性,onclick,在onclick方法里写你要触发点击某一行时触发的事件 gridview里没有datarow这个属性啊 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'"); } } } 现在有空随便给你写个简单的例子好了<%@ 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()+"行');"); } }} 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)"); } } 访问 IIS 元数据库失败. 谁能告诉我怎么清除cookies啊? 如何在浏览器中直接打开IIS服务器中的文件 Repeater导出Excel怎么把最后一列去掉 asp.net的身份验证有哪些?(最好通俗易懂) 请各位高手帮个忙 Web.config的读取问题 下柆框宽度已定,但由于有些item长度大于下拉框宽度,如何在鼠标移到一个item时,将这个item的全名显示出来 连接数据库,提示找不到可安装的isam怎么解决?在线等 使用sqldatareader 放回数据的问题 有关ImageButton显示图片的问题? .net ajax json,一个小问题。。汗
gridview里没有datarow这个属性啊
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowType != DataControlRowType.Header)
{
e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#ff0000'");
}
}
}
<%@ 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()+"行');");
}
}
}
<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)");
}
}