作者testcrack (testcrack)
看板C_Sharp
标题[问题] GridView分页与ImageButton的问题
时间Wed Nov 3 11:12:23 2010
我的GridView有开分页(AllowPaging=true)
分页的功能没有问题,但是後来我新增了三个栏位,每个栏位都有放ImageButton
发现点选其他页时,就会出现错误讯息...
错误讯息如下:
无法将型别 'System.Web.UI.WebControls.GridView' 的物件转换为型别
'System.Web.UI.WebControls.ImageButton'。
此栏位的动作如下:
protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
{
GridViewRow selectedRow =
(GridViewRow)((ImageButton)e.CommandSource).NamingContainer;
int index = selectedRow.RowIndex;
string Event_ID =
(GridView1.Rows[index].Cells[1].FindControl("HyperLink1") as
HyperLink).Text.ToString();
if (e.CommandName.ToString() == "Select1")
Response.Write(Event_ID+"-A");
if (e.CommandName.ToString() == "Select2")
Response.Write(Event_ID+"-B");
if (e.CommandName.ToString() == "Select3")
Response.Write(Event_ID+"-C");
}
这三个栏位都是GridView中的样板
第一个栏位放的ImageButton,CommandName是Select1,其余类推..
至於会多加.ToString(),以及用if不用switch,这是坏习惯 XD
习惯先用这样的方式速写,最後再改.. XD
请问该麽处理?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.132.154.204
1F:推 Laluth:e.CommandSource 是 GridView 不是ImageButton 11/03 12:00
2F:→ testcrack:L大,如果不用分页,上述程式是没问题的。但是若启用分 11/03 16:58
3F:→ testcrack:页,就会发生错误。错误的地方也刚好是您提的那一行。可 11/03 16:59
4F:→ testcrack:否请问一下,您意思是该如何修改?(改成GridView吗?) 11/03 17:05
5F:推 Deadshot465:话说e.CommandName是一样的吧...?怎麽会有123囧? 11/03 18:36
6F:→ Deadshot465:然後e.CommandName本身已经是string了... 11/03 18:36
7F:→ Deadshot465:再ToString()有点多此一举.. 11/03 18:37
※ 编辑: testcrack 来自: 220.132.154.204 (11/03 18:49)
8F:推 Deadshot465:三个栏位中的都是ImageButton吗? 11/03 19:20
9F:→ testcrack:回D大:是的! ^^" 11/03 23:26
10F:推 Deadshot465:我刚刚试过了 换页时确实如L大说的会触发RowCommand 11/04 01:35
11F:→ Deadshot465:所以你要先判断e.CommandSource是不是ImageButton 11/04 01:35
12F:推 Deadshot465:e.CommandSource.GetType() == typeof(ImageButton) 11/04 01:38
13F:→ Deadshot465:这样就会执行正常了 11/04 01:39
14F:→ testcrack:感谢L大与D大,成功了!谢谢! ^^" 11/04 18:37