repeater或datagird在Form_Load事件中
1. 如果你使用了if(!postback){databind();}
   那么动态创建的控件将不能触发ItemCommand事件2. 如果你的绑定语句没有加postback的判断
   那么就更严重了,itemcommand会执行,但是执行的是第二次绑定的commandargument,就产生逻辑BUG了我的repeater里面的没一行数据都对应着创建了对应个数不一的按钮,状态可能在其他用户操作下发生改变,
所以不能由第二次在postback时绑定的数据做itemcommand大家有什么解决办法没?

解决方案 »

  1.   

    VS2003版吗?这个好象能产生吧,我用2008使用if(!postback){databind();} 很正常啊,能角触发
      

  2.   

    忘了说,我是用GridView,08里好象没有datagird
      

  3.   

    动态创建的控件会回发掉,试试放在Page_Int里面动态添加
      

  4.   

     <ItemTemplate>
     <tr>
     <td><%#Eval("id") %></td>
     <td><asp:Button ID="Button1" runat="server" Text="Button" OnCommand="btnGrid_Command" CommandArgument='<%#Eval("name") %>' /></td>
      </tr>
    </ItemTemplate>
        
            <div runat="server" id="Panel1"></div>
            protected void Page_Load(object sender, EventArgs e)
            {            Button btn = new Button();
                btn.Text = "按钮";
                btn.CommandArgument = "CommandArgument";
                btn.Command += new CommandEventHandler(btnGrid_Command);
                this.Panel1.Controls.Add(btn);        }
            protected void btnGrid_Command(object sender, CommandEventArgs e)
            {
             }
      

  5.   

    我用的是vs2008,自己的项目里面是repeater, datagrid是笔误,我是想说DataList的。
      

  6.   

    这个button的文字,和argument要根据绑定的数据创建,没一行的按钮都不一样的。我是在 ItemDataBound 里面创建的,我也比较清楚动态创建的按钮会回发掉,有什么解决办法吗?
      

  7.   

    //编辑
        //protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        //{
        //    if (e.CommandName == "MyCommand")
        //    {
        //        Button button = (Button)e.CommandSource;
        //        GridViewRow row = (GridViewRow)button.Parent.Parent;
        //        string a = row.Cells[1].Text.ToString();//获得第一个单元格的值   
        //        string b = this.GridView1.DataKeys[row.DataItemIndex].Values[0];//获得DataKeys的值   
        //    }
        //}  
      

  8.   


    看看问题,现在根本不能触发Command,如果不加回发判断,数据已经改变了。
      

  9.   

     protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                、、、、、        
            }
            
        }
    这里面的问题 我的已经解决了
      

  10.   

    刚刚发现   是关了Viewstate的原因