ItemCommand事件不响应

解决DataList中LinkButton无效问题
本文描述了在Blog重写过程中遇到的DataList中LinkButton无法触发ItemCommand事件的问题及解决办法。作者最初尝试了多种解决方案均未奏效,最终发现是因为设置了Page和控件的EnableViewState为false导致的。将EnableViewState设为true后问题得到解决。
在重写Blog过程中,在DataList中加了LinkButton来删除当前记录,却发现怎么也不起作用。设置断点跟踪,发现ItemCommand事件根本无法响应。调试了老半天不知原因。
上网Google一下,列出几种原因:
  • 没有把DataList的ItemCommand事件和DataList_ItemCommand事件处理程序关联起来了。
  • 在Page_Load事件中调用DataList的DataBind()方法,而且在Page_Load事件中没有Page.IsPostBack的判断。 DataBind()方法应该放在Page_Load事件
    if (!this.IsPostBack )
    {
    }
    对照这些原因,检查了程序,并没解决问题。
    折腾了老半天,快要放弃了。
    山重水复疑无路,柳暗花明又一村。原来开始为了提高性能考虑,Page和控件的EnableViewState设为false,禁用了状态。把EnableViewState设为true问题就解决了。
    就这么简单,我晕。
  •  
<asp:datagrid id="grid" runat="server" Width="688px" HorizontalAlign="Center" AutoGenerateColumns="False" AllowPaging="True" PageSize="5" meta:resourcekey="gridResource1" > <ItemStyle HorizontalAlign="Left"></ItemStyle> <Columns> <asp:BoundColumn Visible="False" DataField="STAND_ID" HeaderText="STAND_ID" meta:resourcekey="BoundColumnResource1"></asp:BoundColumn> <asp:BoundColumn DataField="CUST_CODE" HeaderText="廠內代碼" meta:resourcekey="BoundColumnResource2"></asp:BoundColumn> <asp:BoundColumn DataField="CUST_NAME" HeaderText="公司名稱" meta:resourcekey="BoundColumnResource3"></asp:BoundColumn> <asp:BoundColumn DataField="VERSION_NAME" HeaderText="版稱" meta:resourcekey="BoundColumnResource4"></asp:BoundColumn> <asp:TemplateColumn HeaderText="修改" meta:resourcekey="BoundColumnResource5"> <HeaderStyle Width="30px"></HeaderStyle> <ItemTemplate> <asp:ImageButton id="BtnEdit" runat="server" ImageUrl="..\images\button_edit1.gif" CommandName="BtnEdit" meta:resourcekey="BtnEditResource1"></asp:ImageButton> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="刪除" meta:resourcekey="BoundColumnResource6"> <HeaderStyle Width="30px"></HeaderStyle> <ItemTemplate> <asp:ImageButton id="BtnDel" runat="server" CommandName="BtnDel" ImageUrl="..\images\button_del1.gif" meta:resourcekey="BtnDelResource1"></asp:ImageButton> </ItemTemplate> </asp:TemplateColumn> </Columns> <PagerStyle Visible="False"></PagerStyle> </asp:datagrid>如何加一个selectindexchanged事件,目前已经有private void grid_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { string _STAND_ID=e.Item.Cells[0].Text.Trim(); string _substrance=e.Item.Cells[1].Text.Trim(); if(e.CommandName=="BtnEdit") { if(Page011.Update_right==1) { Response.Redirect("gm_standard_add.aspx?state=update&standid="+_STAND_ID+""); } else { ITSC.Green.Control.MessageBox.Show(this, "No modification permission"); return; } } else if(e.CommandName=="BtnDel") { if(Page011.Delete_right==1) { ITSC.BLL.WMINV_GM_STANDARD_D bll_detail=new ITSC.BLL.WMINV_GM_STANDARD_D(); if(!bll_detail.Exists(int.Parse(_STAND_ID))) { ITSC.BLL.WMINV_GM_STANDARD_M bll=new ITSC.BLL.WMINV_GM_STANDARD_M(); bll.Delete(int.Parse(_STAND_ID)); ITSC.Green.Control.MessageBox.Show(this, "Delete successfully"); BindRow(); } else { ITSC.Green.Control.MessageBox.Show(this, "There is detailed information here. If you want to delete it, please delete the detailed information first, and then delete it"); return; } } else { ITSC.Green.Control.MessageBox.Show(this, "No deletion permission"); return; } } }事件
最新发布
11-14
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值