| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4826 人关注过本帖
标题:[求助]GridView鼠标选择行问题【未解决】
只看楼主 加入收藏
冰彩虹
Rank: 4
来 自:上海
等 级:贵宾
威 望:14
帖 子:806
专家分:44
注 册:2007-6-28
结帖率:100%
收藏
 问题点数:0 回复次数:25 
[求助]GridView鼠标选择行问题【未解决】

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编辑过]

搜索更多相关主题的帖子: 鼠标 GridView 选择 
2007-08-07 16:53
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
收藏
得分:0 
写个javascript中写个方法(比如rowClick),onclick事件时,调用rowClick事件,并返回点击的行号到本页面
在SelectedIndexChanged事件里面就可以取到这个值

飘过~~
2007-08-07 17:35
sean88
Rank: 1
等 级:新手上路
帖 子:146
专家分:0
注 册:2007-3-27
收藏
得分:0 
以下是引用冰彩虹在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编辑过]


Knowledge is infinite.
2007-08-07 18:06
川流不息
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2000
专家分:47
注 册:2006-11-8
收藏
得分:0 
他应该加了一个“选择”列。

日月更替,天地輪回,人間已是幾回春。 江山不老,人正少年,只手能擎半邊天。
2007-08-08 01:10
冰彩虹
Rank: 4
来 自:上海
等 级:贵宾
威 望:14
帖 子:806
专家分:44
注 册:2007-6-28
收藏
得分:0 
以下是引用bygg在2007-8-7 17:35:22的发言:
写个javascript中写个方法(比如rowClick),onclick事件时,调用rowClick事件,并返回点击的行号到本页面
在SelectedIndexChanged事件里面就可以取到这个值

呵呵,小弟还是不懂!

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


Flying without wings
2007-08-08 08:25
冰彩虹
Rank: 4
来 自:上海
等 级:贵宾
威 望:14
帖 子:806
专家分:44
注 册:2007-6-28
收藏
得分:0 
以下是引用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事件


Flying without wings
2007-08-08 08:31
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
收藏
得分:0 
我是用以下代码实现在单元行的任意地方单击使该行处于选定状态(激发“选择”行事件)的……
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("_", "$") + "','')");
}
}

我是2.0超级菜鸟,请多多教导!
2007-08-08 08:34
冰彩虹
Rank: 4
来 自:上海
等 级:贵宾
威 望:14
帖 子:806
专家分:44
注 册:2007-6-28
收藏
得分:0 
以下是引用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获取当前绑定行


Flying without wings
2007-08-08 09:37
sean88
Rank: 1
等 级:新手上路
帖 子:146
专家分:0
注 册:2007-3-27
收藏
得分:0 
以下是引用冰彩虹在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编辑过]


Knowledge is infinite.
2007-08-08 10:29
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
收藏
得分:0 
以下是引用冰彩虹在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编辑过]


我是2.0超级菜鸟,请多多教导!
2007-08-08 10:30
快速回复:[求助]GridView鼠标选择行问题【未解决】
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012562 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved