我做了一个DataGrid,代码如下:
<asp:DataGrid id="DG1" style="Z-INDEX: 101; LEFT: 72px; POSITION: absolute; TOP: 160px" runat="server" AutoGenerateColumns="False" Width="600px" Height="264px" DataKeyField="ZJKZ_GCHT_ch_id"  OnDeleteCommand="DG1_DeleteCommand" OnItemCommand ="DG1_ItemCommand" AllowCustomPaging="True" AllowPaging="True" CellPadding="3" >
<SelectedItemStyle Font-Size="X-Small"></SelectedItemStyle>
<EditItemStyle Font-Size="X-Small"></EditItemStyle>
<AlternatingItemStyle Font-Size="X-Small"></AlternatingItemStyle>
<ItemStyle Font-Size="X-Small" HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
<HeaderStyle Font-Size="X-Small" HorizontalAlign="Center"></HeaderStyle>
<Columns>
<asp:BoundColumn Visible="False" DataField="ZJKZ_GCHT_ch_id"></asp:BoundColumn>
<asp:BoundColumn DataField="ZJKZ_GCHT_ch_name" HeaderText="工程名称"></asp:BoundColumn>
<asp:BoundColumn DataField="ZJKZ_GCHT_ch_num" HeaderText="起止桩号"></asp:BoundColumn>
<asp:BoundColumn DataField="ZJKZ_GCHT_ch_time" HeaderText="记录日期" DataFormatString="{0:d}"></asp:BoundColumn>
<asp:ButtonColumn Text="详细信息" CommandName="Select"></asp:ButtonColumn>
<asp:ButtonColumn Text="删除" CommandName="Delete"></asp:ButtonColumn>
</Columns>
<PagerStyle Font-Size="X-Small" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>按钮“选择”的代码如下:
Sub DG1_ItemCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs) Handles DG1.ItemCommand
        Dim strid As String = DG1.DataKeys(e.Item.ItemIndex)
        Dim repage As String = "modify_1.aspx?catid=" & strid        'Response.Redirect(repage)
    End Sub
按钮“删除”的代码如下:
Sub DG1_DeleteCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs) Handles DG1.DeleteCommand
        Dim strid As String = DG1.DataKeys(e.Item.ItemIndex)        Dim strdeletech As String
        Dim strdeletesub As String
        Dim cmddeletech As SqlCommand
        Dim cmddeletesub As SqlCommand        strdeletech = "delete ZJKZ_GCHT_ch where ZJKZ_GCHT_ch_id='" & strid & "'"
        cmddeletech = New SqlCommand(strdeletech, con)
        strdeletesub = "delete ZJKZ_GCHT_sub where ZJKZ_GCHT_ch_id='" & strid & "'"
        cmddeletesub = New SqlCommand(strdeletesub, con)        con.Open()
        cmddeletesub.ExecuteNonQuery()
        cmddeletech.ExecuteNonQuery()
        con.Close()        'datagrid数据绑定
        dgdatebind()
   End Sub
问题是当我点击“选择”时,执行Sub DG1_ItemCommand,一切正常,当我点击“删除”时,也执行Sub DG1_ItemCommand,不过当我把datagri的OnItemCommand ="DG1_ItemCommand"属性删除后,点击“删除”时可以执行Sub DG1_DeleteCommand,简直邪门了,请各位大虾指点迷津。

解决方案 »

  1.   

    在这种情形下,你就不应该有2个event handler,因为控制Command事件触发的函数总是先调用ItemCommandSub DG1_ItemCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs) Handles DG1.ItemCommand
            dim text1 as string = e.CommandName
            if text1.ToLower() <> "delete" then               Dim strid As String = DG1.DataKeys(e.Item.ItemIndex)
                   Dim repage As String = "modify_1.aspx?catid=" & strid              'Response.Redirect(repage)
            end if
        End Sub
      

  2.   

    完全同意,一点都不奇怪,ItemCommand事件无论是按钮列、翻页等都会触发,区分的方法是e.CommandName
      

  3.   

    datagrid的“删除”按钮可以由DeleteCommand单独触发,“选择”按钮怎么样单独触发?
      

  4.   

    if the button's CommandName is "Select", OnSelectedIndexChanged will be triggered
      

  5.   

    to  saucer(思归)
    如果使用了 OnSelectedIndexChanged ,
      Sub DG1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DG1.SelectedIndexChanged....    End Sub
    就无法使用e.Item.ItemIndex了,仍然无法让“选择”按钮单独触发?