1

我试图阻止删除我的 GridView 的行,如下所示:

    void GridView1_RowDeleting(Object sender, GridViewDeleteEventArgs e)
    {

        if (GridView1.Rows[e.RowIndex].Cells[1].ToString() == "root")
        {
            e.Cancel = true;
        }
}

但它不起作用,我在那里放了一个断点,但它永远不会停止我不认为这个事件被这个函数捕获,就像 MSDN 所说的那样?

这是网格视图:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" DataKeyNames="id" DataSourceID="sqlWorkFlowItems" 
        ForeColor="#333333" GridLines="None">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True"   />
            <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" 
                ReadOnly="True" SortExpression="id" Visible="False" />
            <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            <asp:BoundField DataField="link" HeaderText="link" SortExpression="link" />
            <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" 
                ReadOnly="True" SortExpression="id" Visible="False" />
            <asp:TemplateField HeaderText="parent" SortExpression="parent">
                <EditItemTemplate>
                    <asp:DropDownList ID="cboParent" SelectedValue='<%# Bind("parent") %>' DataSourceID="sqlWorkFlowItems"
                    DataTextField="name" DataValueField="id" runat="server" />
                </EditItemTemplate>
                <ItemTemplate>
                   <asp:DropDownList ID="cboParent" SelectedValue='<%# Bind("parent") %>' DataSourceID="sqlWorkFlowItems"
                    DataTextField="name" DataValueField="id" runat="server" Enabled="false"  />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="company" SortExpression="company">
                <EditItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("company") %>'></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("company") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>

编辑:

还尝试将此添加到 Page_Load 函数中无济于事:

GridView1.RowDeleting = new GridViewDeleteEventHandler(this.GridView1_RowDeleting);
4

1 回答 1

2

您可以以声明方式执行此操作:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" DataKeyNames="id" DataSourceID="sqlWorkFlowItems" 
        ForeColor="#333333" GridLines="None" 
        OnRowDeleting="GridView1_RowDeleting">

或在代码中(我认为您只是错过了 +=):

GridView1.RowDeleting += new GridViewDeleteEventHandler(GridView1_RowDeleting);
于 2009-06-23T18:51:35.613 回答