DataGrid模板列上的ImageButton 不能触发ItemCommand事件?

redebug 2003-10-20 06:27:39
在一个模板列上放一个linkButton 和imageButton,
LinkButton的CommandName 和CommandArgument都没有设置
ImageButton的CommandName 和CommandArgument都有设置
结果是linkbutton触发ItemCommand事件,ImageButton不触发,
CSDN上的帖子几乎搜完了,也没有个定论,到底ImageButton能不能触发ItemCommand事件???如何编码???
...全文
46 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ninesong 2003-10-22
  • 打赏
  • 举报
回复
mark
redebug 2003-10-21
  • 打赏
  • 举报
回复
思归已经是五星了,就不给你分了,多谢你!
结贴
redebug 2003-10-21
  • 打赏
  • 举报
回复
谢谢各位的帮助,问题解决了.
我的问题是出在我将事件的声明和事件的处理函数写在了.cs文件中,没有写在.aspx文件中,在这种情况下,linkButton可以触发事件,imageButton不能了.


由于我前面一直是在做winform的系统,对BS系统没有接触过,现在就感觉象是一个武功全失的人,有力使不出,努力学习中!!!!
liuzhonghe 2003-10-21
  • 打赏
  • 举报
回复
<asp:TemplateColumn>
<ItemStyle Width="50px"></ItemStyle>
<ItemTemplate>
<asp:LinkButton id="btnLink" Runat="server" CommandName="TestLink" Text="Hello" />
<asp:ImageButton OnClick="ImageButton1_Click" CommandName="bb" Runat="server" ID="image"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>
能触发事件阿试过了能行
flyinglz 2003-10-21
  • 打赏
  • 举报
回复
我是直接将onClick(当然也可以是别的事件)事件定义在页面上,如下:
<asp:ImageButton id="Imagebutton3" ImageUrl="../../images/edit.gif" OnClick="imagebutton" CommandName="Select" Runat="server"></asp:ImageButton>

然后在CS代码中定义一个protected 或public 的事件函数(imagebutton).
saucer 2003-10-21
  • 打赏
  • 举报
回复
are you sure? did you forget to add runat=server? try

<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
void Page_Load(object o, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}

void BindData()
{
SqlDataAdapter da = new SqlDataAdapter("select * from authors",
"server=localhost;database=pubs;uid=sa;pwd=;");

DataTable dt = new DataTable ();
da.Fill(dt);


DataGrid1.DataSource = dt.DefaultView;
DataGrid1.DataBind();
}

void DataGrid1_Command(Object sender, DataGridCommandEventArgs e)
{
//Response.Write(e.CommandSource.GetType().Name);
if (e.CommandSource is ImageButton)
{
ImageButton btn = (ImageButton)e.CommandSource;
DataGridItem dgi = (DataGridItem)btn.Parent.Parent;
Response.Write(String.Format("image button on {0}, command Name:", dgi.ItemIndex, btn.CommandName));
}
}

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:DataGrid id="DataGrid1" runat="server" OnItemCommand="DataGrid1_Command">
<Columns>
<asp:TemplateColumn HeaderText="image button">
<ItemTemplate>
<asp:ImageButton id="btnImage" Runat="server"
CommandName="TestImage" ImageUrl="http://www.csdn.net/images/csdn.gif"
AlternateText="CSDN" />
<asp:LinkButton id="btnLink" Runat="server"
CommandName="TestLink" Text="Hello"/>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
redebug 2003-10-20
  • 打赏
  • 举报
回复
那就是说ImageButton不能触发DataGrid的ItemCommand事件了?
oldhunter 2003-10-20
  • 打赏
  • 举报
回复
给它的CLICK事件再附加个方法.没办法的办法了.

62,025

社区成员

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

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

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

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