注册 登录
编程论坛 ASP.NET技术论坛

[求助]GridView鼠标选择行问题【未解决】

冰彩虹 发布于 2007-08-07 16:53, 4831 次点击

html页面:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="merID" HeaderText="商品ID" />
<asp:BoundField DataField="merName" HeaderText="商品名称" />
<asp:BoundField DataField="providerid" HeaderText="供应商ID" />
<asp:BoundField DataField="categoryID" HeaderText="分类" />
</Columns>
</asp:GridView>

cs文件代码:
public partial class GridViewSample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)//添加判断会报错
{
BusinessLogical.DataSource dataSource = new BusinessLogical.DataSource();
this.GridView1.DataSource = dataSource.GetMerchandises("");
this.GridView1.DataBind();
}
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
PostBackOptions myPostBackOptions = new PostBackOptions(this);
myPostBackOptions.AutoPostBack = false;
myPostBackOptions.RequiresJavaScriptProtocol = true;
myPostBackOptions.PerformValidation = false;

String evt = Page.ClientScript.GetPostBackClientHyperlink(sender as System.Web.UI.WebControls.GridView, "Select$" + e.Row.RowIndex.ToString());

if (e.Row.RowType == DataControlRowType.DataRow)
{
//单击事件
e.Row.Attributes.Add("onclick", evt);

//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#A9A9A9'");

//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

//设置悬浮鼠标指针形状为"小手"
e.Row.Attributes["style"] = "Cursor:hand";
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write(this.GridView1.SelectedIndex);
}
}

Page_Load中不加IsPostBack判断,正常;否则,网页就报错

群策群力,大家帮帮忙,谢谢了,^_^

[此贴子已经被作者于2007-8-12 9:00:12编辑过]

25 回复
#2
bygg2007-08-07 17:35
写个javascript中写个方法(比如rowClick),onclick事件时,调用rowClick事件,并返回点击的行号到本页面
在SelectedIndexChanged事件里面就可以取到这个值
#3
sean882007-08-07 18:06
以下是引用冰彩虹在2007-8-7 16:53:48的发言:

html页面:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="merID" HeaderText="商品ID" />
<asp:BoundField DataField="merName" HeaderText="商品名称" />
<asp:BoundField DataField="providerid" HeaderText="供应商ID" />
<asp:BoundField DataField="categoryID" HeaderText="分类" />
</Columns>
</asp:GridView>

cs文件代码:
public partial class GridViewSample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)//添加判断会报错
{
BusinessLogical.DataSource dataSource = new BusinessLogical.DataSource();
this.GridView1.DataSource = dataSource.GetMerchandises("");
this.GridView1.DataBind();
}
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
PostBackOptions myPostBackOptions = new PostBackOptions(this);
myPostBackOptions.AutoPostBack = false;
myPostBackOptions.RequiresJavaScriptProtocol = true;
myPostBackOptions.PerformValidation = false;

String evt = Page.ClientScript.GetPostBackClientHyperlink(sender as System.Web.UI.WebControls.GridView, "Select$" + e.Row.RowIndex.ToString());

if (e.Row.RowType == DataControlRowType.DataRow)
{
//单击事件
e.Row.Attributes.Add("onclick", evt);

//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#A9A9A9'");

//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

//设置悬浮鼠标指针形状为"小手"
e.Row.Attributes["style"] = "Cursor:hand";
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write(this.GridView1.SelectedIndex);
}
}

Page_Load中不加IsPostBack判断,正常;否则,网页就报错

群策群力,大家帮帮忙,谢谢了,^_^

我测试了一下,Page_Load中无论不加IsPostBack判断,点击GridView1,都会出错.说明和IsPostBack无关,问题出在你写的那句单击事件事件,如果没有,运行正常
String evt = Page.ClientScript.GetPostBackClientHyperlink(sender as System.Web.UI.WebControls.GridView, "Select$" + e.Row.RowIndex.ToString());
e.Row.Attributes.Add("onclick", evt);

不是很明白,你这里加这个单击事件,为何又用GridView1_SelectedIndexChanged事件?

[此贴子已经被作者于2007-8-7 18:07:18编辑过]

#4
川流不息2007-08-08 01:10
他应该加了一个“选择”列。
#5
冰彩虹2007-08-08 08:25
以下是引用bygg在2007-8-7 17:35:22的发言:
写个javascript中写个方法(比如rowClick),onclick事件时,调用rowClick事件,并返回点击的行号到本页面
在SelectedIndexChanged事件里面就可以取到这个值

呵呵,小弟还是不懂!

怎么调用rowClick事件,把点击的行号返回到本页面的SelectedIndexChanged事件里?需要用ajax嘛?这个还在学习中哈...

#6
冰彩虹2007-08-08 08:31
以下是引用sean88在2007-8-7 18:06:16的发言:

我测试了一下,Page_Load中无论不加IsPostBack判断,点击GridView1,都会出错.说明和IsPostBack无关,问题出在你写的那句单击事件事件,如果没有,运行正常
String evt = Page.ClientScript.GetPostBackClientHyperlink(sender as System.Web.UI.WebControls.GridView, "Select$" + e.Row.RowIndex.ToString());
e.Row.Attributes.Add("onclick", evt);

不是很明白,你这里加这个单击事件,为何又用GridView1_SelectedIndexChanged事件?



不会吧?你是2005嘛?不知道报啥错哈,config文件要配置<pages enableEventValidation="false"/>

e.Row.Attributes.Add("onclick", evt);这句话就是把onclick和GridView1_SelectedIndexChanged关联起来;

如流川兄说的那样添加选择列问题就简单了,可我想要的效果是鼠标点击GridView行的任何地方,都可以触发SelectedIndexChanged事件

#7
foshan2007-08-08 08:34
我是用以下代码实现在单元行的任意地方单击使该行处于选定状态(激发“选择”行事件)的……
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
this.GridView1.Rows[i].Attributes.Add("onclick", "javascript:__doPostBack('" + this.GridView1.Rows[i].FindControl("LinkButton1").ClientID.Replace("_", "$") + "','')");
}
}
#8
冰彩虹2007-08-08 09:37
以下是引用foshan在2007-8-8 8:34:40的发言:
我是用以下代码实现在单元行的任意地方单击使该行处于选定状态(激发“选择”行事件)的……
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
this.GridView1.Rows[i].Attributes.Add("onclick", "javascript:__doPostBack('" + this.GridView1.Rows[i].FindControl("LinkButton1").ClientID.Replace("_", "$") + "','')");
}
}

可行,但是这样不是还要额外添加一个模版列放置LinkButton1嘛?

PS:RowDataBound事件里不需要再来个for循环了,直接e.Row获取当前绑定行

#9
sean882007-08-08 10:29
以下是引用冰彩虹在2007-8-8 8:31:03的发言:


不会吧?你是2005嘛?不知道报啥错哈,config文件要配置<pages enableEventValidation="false"/>

e.Row.Attributes.Add("onclick", evt);这句话就是把onclick和GridView1_SelectedIndexChanged关联起来;

如流川兄说的那样添加选择列问题就简单了,可我想要的效果是鼠标点击GridView行的任何地方,都可以触发SelectedIndexChanged事件

我用的是VS2005,测试了一下,现在config文件要配置加上了<pages enableEventValidation="false"/>,有e.Row.Attributes.Add("onclick", evt);这句话也页面不会报错了,可是由于你这句e.Row.Attributes.Add("onclick", evt);页面还是有一点点问题,无法完全执行页面,左下会出现黄色警告.

[此贴子已经被作者于2007-8-8 11:12:48编辑过]

#10
foshan2007-08-08 10:30
以下是引用冰彩虹在2007-8-8 9:37:48的发言:

可行,但是这样不是还要额外添加一个模版列放置LinkButton1嘛?

PS:RowDataBound事件里不需要再来个for循环了,直接e.Row获取当前绑定行

就是用 选择 列放到模板中
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select">选择</asp:LinkButton>
</ItemTemplate><ItemStyle Width="10px"></ItemStyle>
<HeaderStyle Width="10px"></HeaderStyle>
</asp:TemplateField>

[此贴子已经被作者于2007-8-8 10:32:05编辑过]

#11
foshan2007-08-08 10:55
冰斑竹,怎么以下报错的?

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add("onclick", "javascript:__doPostBack('" + e.Row.FindControl("LinkButton1").ClientID.Replace("_", "$") + "','')");
}
#12
冰彩虹2007-08-08 10:59
以下是引用foshan在2007-8-8 10:55:00的发言:
冰斑竹,怎么以下报错的?

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add("onclick", "javascript:__doPostBack('" + e.Row.FindControl("LinkButton1").ClientID.Replace("_", "$") + "','')");
}

不会吧,这样会报错?报啥错?

#13
foshan2007-08-08 11:04
这样就行了:
if (e.Row.RowIndex >= 0)
{
e.Row.Attributes.Add("onclick", "javascript:__doPostBack('" + e.Row.FindControl("LinkButton1").ClientID.Replace("_", "$") + "','')");
}
#14
川流不息2007-08-08 11:05
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(此处判断不标题栏和尾栏)
{
e.Row.Attributes.Add("onclick","alert('"+e.RowIndex+"');");
}

}

[此贴子已经被作者于2007-8-8 11:06:17编辑过]

#15
foshan2007-08-08 11:06
第一次运行时 e.Row.RowIndex = -1 ,所以找不到  LinkButton1 ,所以报错
#16
冰彩虹2007-08-08 11:21
是的,RowDataBound事件里很容易疏忽对e.Row.RowType进行判断

if (e.Row.RowType == DataControlRowType.DataRow)
{
......
}

大家言归正卷啦,革命还未成功,大家仍需努力^_^
#17
zd1234562007-08-08 14:49
我做了一下 好像可以的啊.
没报错啊.
#18
冰彩虹2007-08-08 19:39


为什么有的可以,有的报错呢?
#19
冰彩虹2007-08-10 08:28
各位大哥大姐,小的小妹,问题依旧哈,帮帮忙哦...

#20
冰彩虹2007-08-10 18:40
#21
冰彩虹2007-08-23 18:40

还没结帖呐,大哥大姐们

#22
川流不息2007-08-24 04:23

这么久了,这帖子还在,靠,逼得我拿出压箱底的本事了。小冰,看能不能解决你的问题。这是vs2003的datagrid,你套用你的gridview看看。
if(e.Item.ItemType==ListItemType.AlternatingItem ||e.Item.ItemType==ListItemType.Item)
{
if(e.Item.ItemIndex!=intCount-1)//最后一行不设事件
{
e.Item.Attributes.Add("onmouseover","this.bgColor='ccff00';style.cursor = 'hand';");
e.Item.Attributes.Add("onmouseout","this.bgColor=''");
string strTemp = "document.all.hidItemIndex.value='"+e.Item.ItemIndex+"';";
e.Item.Attributes.Add("onclick",strTemp+"window.Form1.submit();");
}
}

页面刷新时你取这个隐藏框的值就可以取到了。

#23
冰彩虹2007-08-24 20:31
川流兄啊,你还有压箱底的东东舍不得拿出来哈?????

呵呵,谢谢谢谢!回头试试看
#24
xianzhenfly2007-08-28 08:35
我曾见过有人扩展了GridView的单击双击功能,我推断GridView就不支持单击双击功能。
以下供参考:
http://www.cnblogs.com/webabcd/archive/2007/01/22/626484.html
只是我不是很懂。
#25
xianzhenfly2007-08-28 10:24
#26
冰彩虹2007-09-03 20:13

顶起,呵呵

1