再描叙一下,就是在DataGrid中只包含
“绑定列”
“超级链接列”
“模板列”
时,程序会处理ImageButton的事件但是如果含有
“按钮列”则程序不执行!

解决方案 »

  1.   

    请问你的datagrid是怎么绑定的?
    还有你的按钮列的代码贴出来看看吧
      

  2.   

    //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;
    } }
      

  3.   

    问题在于,只要是LinkButton,一点问题都没有,但是ImageButton就是不行
      

  4.   

    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
      

  5.   

    你在编辑按扭列的每个按扭事件方法的末尾添上this.DataGrid1.EditIndex = -1,然后再重新绑定DataGrid里的数据
      

  6.   

    问题的关键在于,程序根本就是不处理Button、ImageButton等类似的控件的事件,单单只处理LinkButton事件我原来在另外一个项目中用过ImageButton、Button甚至更加负责的控件,但是都没有问题,会不会是项目配置的问题??
      

  7.   

    到底是什么因素导致了程序只处理LinkButton事件呢?大家一定要回答我啊!!!!
      

  8.   

    响应 DataList、Repeater 和 DataGrid 控件中的按钮事件 在控件模板中包括 Button、LinkButton 或 ImageButton。将按钮的 CommandName 属性设置为标识其功能的字符串,如“排序”或“复制”。 
    创建用于控件的 ItemCommand 事件的方法。在该方法中,执行下列操作: 
    检查事件参数对象的 CommandSource 属性的 CommandName 属性,查看传递的是什么字符串。 
    为用户单击的按钮执行相应的逻辑。 
    注意   如果您调用了父控件(DataList、Repeater 或 DataGrid 控件)的 DataBind 方法,ItemCommand 事件将不会发生,原因是父控件的内容已经重置。因此,您通常不需要在每次往返时调用 DataBind 方法(即在初始化页时无需检查发回)。有关详细信息,请参见数据绑定多记录 Web 服务器控件和在设计时数据绑定单值 Web 服务器控件。
      

  9.   

    还是要看MSDN啊,问题解决了
    一定要写
    if( !IsPostBack)
    {
       GridBind()
    }