DateGrid中的ImageButton问题(请教高手)

hxhbluestar 2004-08-01 08:56:08
本人想在
DateGrid中添加一个模板列,然后在模板列中放一个ImageButton按钮,实现删除操作,同时,在这个DataGrid中含有系统的按钮列(即“查看/编辑”等系统自带的按钮),请问,如何实现?

我碰到的问题是,只要是带有系统的按钮列,程序死活不理睬ImageButton的事件,而一旦没有用到系统按钮列,一切正常!

所以在这里请教高手来回答,没有经过调试(记住,含有编辑列等系统自带的按钮列)请不要随便做答!

如果谁有类似的代码,请发给我hxhforwork@hotmail.com,不胜感谢!!
...全文
185 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
hxhbluestar 2004-08-03
  • 打赏
  • 举报
回复
还是要看MSDN啊,问题解决了
一定要写
if( !IsPostBack)
{
GridBind()
}
hxhspace 2004-08-02
  • 打赏
  • 举报
回复
响应 DataList、Repeater 和 DataGrid 控件中的按钮事件

在控件模板中包括 Button、LinkButton 或 ImageButton。将按钮的 CommandName 属性设置为标识其功能的字符串,如“排序”或“复制”。
创建用于控件的 ItemCommand 事件的方法。在该方法中,执行下列操作:
检查事件参数对象的 CommandSource 属性的 CommandName 属性,查看传递的是什么字符串。
为用户单击的按钮执行相应的逻辑。
注意 如果您调用了父控件(DataList、Repeater 或 DataGrid 控件)的 DataBind 方法,ItemCommand 事件将不会发生,原因是父控件的内容已经重置。因此,您通常不需要在每次往返时调用 DataBind 方法(即在初始化页时无需检查发回)。有关详细信息,请参见数据绑定多记录 Web 服务器控件和在设计时数据绑定单值 Web 服务器控件。
hxhbluestar 2004-08-02
  • 打赏
  • 举报
回复
到底是什么因素导致了程序只处理LinkButton事件呢?大家一定要回答我啊!!!!
hxhbluestar 2004-08-02
  • 打赏
  • 举报
回复
问题的关键在于,程序根本就是不处理Button、ImageButton等类似的控件的事件,单单只处理LinkButton事件

我原来在另外一个项目中用过ImageButton、Button甚至更加负责的控件,但是都没有问题,会不会是项目配置的问题??
tiger341 2004-08-02
  • 打赏
  • 举报
回复
你在编辑按扭列的每个按扭事件方法的末尾添上this.DataGrid1.EditIndex = -1,然后再重新绑定DataGrid里的数据
Proyang 2004-08-01
  • 打赏
  • 举报
回复
public void DG1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//可以加一行(我没加大小写):
response.write("名字:"+e.CommandName);
response.end()
ImageButton ib = (ImageButton)e.CommandSource;
this.Label1.Text = ib.CommandName;

switch (e.CommandName)
{
case "Image":
this.Label1.Text = "click";
break;
case "ImageBtn":
this.Label1.Text = "imageButton";
break;
}
imagebutton的autopostback=true
hxhbluestar 2004-08-01
  • 打赏
  • 举报
回复
请大侠帮忙了,小弟实在是没有招了
hxhbluestar 2004-08-01
  • 打赏
  • 举报
回复
问题在于,只要是LinkButton,一点问题都没有,但是ImageButton就是不行
hxhbluestar 2004-08-01
  • 打赏
  • 举报
回复
//DataGrid


<asp:datagrid id="DG1" runat="server" Width="528px" AutoGenerateColumns="False">
<EditItemStyle Wrap="False" HorizontalAlign="Center"></EditItemStyle>
<ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
<HeaderStyle Wrap="False" HorizontalAlign="Center"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="ID" ReadOnly="True" HeaderText="编号">
<HeaderStyle Wrap="False" HorizontalAlign="Center"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="GroupName" ReadOnly="True" HeaderText="组名称">
<HeaderStyle Wrap="False" HorizontalAlign="Center"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="查看/编辑">
<ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
</asp:EditCommandColumn>
<asp:TemplateColumn>
<ItemStyle Width="50px"></ItemStyle>
<ItemTemplate>
<asp:LinkButton CommandName="Image" Runat="server" CausesValidation="False">sadfsdf</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemStyle Width="50px"></ItemStyle>
<ItemTemplate>
<asp:ImageButton CommandName="ImageBtn" Runat="server" ImageUrl="../../images/delete.gif" CausesValidation="False"
ID="Linkbutton1"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>

</Columns>
</asp:datagrid><asp:label id="Label1" runat="server" Width="520px"></asp:label>


//绑定代码
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
DG1.DataBind();
}

private void InitializeComponent()
{
this.ButAdd.Click += new System.EventHandler(this.ButAdd_Click);
this.DG1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DG1_ItemCommand);
this.DG1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DG1_EditCommand);
this.DG1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DG1_DeleteCommand);
this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}

public void DG1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string strID = e.Item.Cells[0].Text;
Response.Write("<script language=\"javascript\">parent.frames['leftFrame'].location.href='"+@"../../web/user_menu.aspx?type="+ConstValue.IntDRG.ToString()+"&id="+strID+"';</script>");
Response.Write("<script language=\"javascript\">this.location.href='EditDRG.aspx?id="+strID+"&type="+ConstValue.IntEDIT.ToString()+"';</script>");
}

public void DG1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Label1.Text = "";
string strID = e.Item.Cells[0].Text;
DTDRG = objDRG.GetAll();
int intRes = DelTree(strID);
if(intRes!=-1)
{
DTDRG = objDRG.GetAll();
DG1.DataSource = DTDRG;
DG1.DataBind();
}
else
{
Label1.Text = "删除失败";
}
}

public void DG1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
ImageButton ib = (ImageButton)e.CommandSource;
this.Label1.Text = ib.CommandName;

switch (e.CommandName)
{
case "Image":
this.Label1.Text = "click";
break;
case "ImageBtn":
this.Label1.Text = "imageButton";
break;
}



}
Proyang 2004-08-01
  • 打赏
  • 举报
回复
请问你的datagrid是怎么绑定的?
还有你的按钮列的代码贴出来看看吧
hxhbluestar 2004-08-01
  • 打赏
  • 举报
回复
自己顶一下
hxhbluestar 2004-08-01
  • 打赏
  • 举报
回复
再描叙一下,就是在DataGrid中只包含
“绑定列”
“超级链接列”
“模板列”
时,程序会处理ImageButton的事件

但是如果含有
“按钮列”

则程序不执行!

62,074

社区成员

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

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

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

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