Datalist中使用ItemCommand事件

在頁面部分設定如下:

<asp:Button ID="Button3" CommandName="Mo" runat="server" BackColor="#FF8080" BorderColor="Black" BorderStyle="Solid"
                              CommandArgument='<%# DataBinder.Eval(Container.DataItem, "id") %>' CausesValidation="false" Text="修改" Width="40px" />
                              <asp:Button ID="Button2" CommandName="De" runat="server" BackColor="#FF8080" BorderColor="Black" BorderStyle="Solid"
                              
                              CommandArgument='<%# DataBinder.Eval(Container.DataItem, "id") %>' CausesValidation="false" OnClientClick="return confirm('确定删除?');"

后臺代碼:

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "De")
        {
            //int i = e.Item.ItemIndex;
            //this.DataList1.Items[i].Visible = false;
            string Sql = "Delete from Topic where id="+Convert.ToInt32(e.CommandArgument);
            ClassConn conn = new ClassConn();
            SqlHelper.ExecuteNonQuery(conn.conn, CommandType.Text, Sql);
           // Data_bind1();
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "updateScript", "location.href='/bbs/ReplayBBS.aspx?subid=" + Request.QueryString["subid"] + "'", true);

        }
        if (e.CommandName == "Mo")
        {
            TextBox txt=(TextBox)this.DataList1.Items[e.Item.ItemIndex].FindControl("TextBox1");
            string tmp_sql = @"update Topic set Brief='" + txt.Text.ToString().Trim() + "' where id=" + Convert.ToInt16(e.CommandArgument);
            try
            {
                ClassConn bconn = new ClassConn();
                SqlHelper.ExecuteNonQuery(bconn.conn, CommandType.Text, tmp_sql);
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "updateScript", "alert('修改成功!');location.href='/bbs/ReplayBBS.aspx?subid=" + Request.QueryString["subid"] + "'", true);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
    }

還有另外一個例子:

在写一个DataList按钮事件时,遇到一个问题:虽然不是很有难度,但是可能会有人遇到而且会和我一样一筹莫展,直到看MSDN时,无意中发现了一句话~ 

事情是这样子的: 

     我需要在DataList中插入一个普通按钮,实现对Item的操作,于是,我就编辑项模版 -- 插入按钮,设置该按钮的CommandName为Button2,然后结束编辑项模版,选中该DataList,在属性对话框内按了那个黄色的小闪电标志,双击“ItemCommand”,在事件中写: 

        if(e.CommandName == "Button2") 
    { 
     Label LabelX = (Label)e.Item.FindControl("Label1"); 
     string strIn = "insert into orderlog (nusername,nmoname) values (’"+username+"’,’"+LabelX.Text+"’)"; 
     string reu = SC.orderAlready(username,LabelX.Text); 
     if(reu == "0") 
     { 
      SC.INS(strIn); 
      Page.RegisterStartupScript("","<script>alert(’您已经成功定制了该内容,您可以返回到控制台页面点击“使用模块”直接使用,也可以在定制模版时添加到模版中。’)</script>"); 
     } 
     if(reu == "1") 
     { 
      Page.RegisterStartupScript("","<script>alert(’该项内容您已经定制过了,不需要重复定制。’)</script>"); 
     } 
     if(reu != "0" && reu != "1") 
     { 
      Page.RegisterStartupScript("","<script>alert(’有一个异常问题:请您联系我们的客户服务人员。’)</script>"); 
     } 

    原本以为,可以很轻松地实现代码定义的操作,可是不然,断点调试发现根本就不进!于是反复调试,反复失败! 

    无奈中,去到 MSDN(http://msdn.microsoft.com/library/chs/default.asp?url=/library/chs/vbcon/html/vbtskrespondingtobuttoneventsindatalistitems.asp)看看,发现有这么一段提示:“注意    如果您调用了父控件(DataList、Repeater 或 DataGrid 控件)的 DataBind 方法,ItemCommand 事件将不会发生,原因是父控件的内容已经重置。因此,您通常不需要在每次往返时调用 DataBind 方法(即在初始化页时无需检查发回)。” 

   于是,到cs文件中,将PageLoad中的DataList    DataBind代码段外加上if(!Page.IsPostBack),再调试,一切如所期望的那样,顺利执行。


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值