如何在gridview中禁用editmode?

时间:2012-07-25 17:32:52

标签: c# asp.net

我有一个工作正常的gridview。但只要单击命令按钮,它就会进入编辑模式。反正有没有阻止这个?

<asp:GridView id="grv_purchaseOrder" runat="server" AutoGenerateColumns="False" 
    onrowdatabound="grv_purchaseOrder_RowDataBound"  ShowFooter="True"
    EmptyDataText="No Purchase Order were found." DataKeyNames="purchaseOrderID" 
onrowcommand="grv_purchaseOrder_RowCommand" 
    onrowediting="grv_purchaseOrder_RowEditing">
    <Columns>
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:Button ID="but_purchaseOrderDetails" runat="server" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'
                    CausesValidation="false" CommandName="edit" Text="Detail" />
            </ItemTemplate>
            <FooterTemplate>
                <asp:Button ID="but_newOrderDetails" runat="server" CausesValidation="false"  commandargument="0"
                    CommandName="new" Text="New" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="name" HeaderText="Name" />
        <asp:TemplateField HeaderText="Date">
            <ItemTemplate>
                <asp:Label ID="lbl_dateCreated" runat="server" Text='<%# Bind("dateCreated") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="purchaseOrderStatus" HeaderText="Status" />
    </Columns>
</asp:GridView>

具体来说,but_newOrderDetails将gridview置于编辑模式。

这是Row Command事件处理程序中的内容:

        string currentCommand = e.CommandName;
        ClientScriptManager cs = Page.ClientScript;
        int rowIndex = int.Parse(e.CommandArgument.ToString());
        string purchaseOrderID = grv_purchaseOrder.DataKeys[rowIndex]["purchaseOrderID"].ToString();
        if (currentCommand == "edit" || currentCommand == "new") {
            cs.RegisterStartupScript(this.GetType(), "popupScript", "window.open('PurchaseOrderDetail.aspx?purchaseOrderID=" + purchaseOrderID + "','_blank');", true);
        }

我也试着把grv_purchaseOrder.EditIndex = -1;在rowEditing事件中,但它没有做任何事情。

2 个答案:

答案 0 :(得分:0)

<asp:gridview id="GridView1" runat="server"> 
        <columns> 
                <asp:templatefield> 
                        <itemtemplate> 
                                <asp:linkbutton id="btnEdit" runat="server" commandname="Edit" enabled='<%# !(Eval("Status").ToString().Equals("Closed") || Eval("Status").ToString().Equals("Cancelled")) %>' 
text="Edit" /> 
                        </itemtemplate> 
                </asp:templatefield> 
        </columns> 
</asp:gridview>

答案 1 :(得分:-1)

你在gridview_RowEditing eventHandler中编写这段代码:

protected void grvMain_RowEditing(object sender, GridViewEditEventArgs e)
{
    grvMain.EditIndex = -1;
    FillGrid();
}