我试图阻止为我的GridView删除一行,如下所示:
void GridView1_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
if (GridView1.Rows[e.RowIndex].Cells[1].ToString() == "root")
{
e.Cancel = true;
}
}
但它不起作用,我在那里放了一个断点,但它永远不会停止我不认为这个函数正在捕获这个事件,就像MSDN所说的那样?
这是GridView:
<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);
答案 0 :(得分: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);