2010-11-08 69 views
0

假設我在頁面上有一個GridView。 GridView已啓用編輯列並顯示一些記錄。如何根據其他數據字段啓用/禁用行中的編輯?GridView行中的可選編輯?

+0

不確定你的意思。你是否想基於數據禁用/啓用對某一行的編輯能力(即,如果一個字段等於true,則禁用一行中的「編輯」鏈接按鈕)?或者,根據相同的條件類型自動將一行放入編輯模式? – Jamie 2010-11-08 11:52:55

+0

@Jamie:我的意思是「想根據數據在行上禁用/啓用編輯能力(即,如果字段等於true,則禁用行上的」編輯「LinkBut​​ton)」 – 2010-11-08 11:54:09

回答

2

您可以通過多種方式來做到這一點。其中兩個是:

首先將編輯列轉換爲模板字段。

無論您希望基於哪個字段啓用/禁用,您都可以添加GridView的DataKeyNames屬性。

然後在OnRowDataBound事件,你可以做到以下幾點:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Normal) 
    { 
     var LinkButton1 = (LinkButton)e.Row.FindControl("LinkButton1"); 
     LinkButton1.Enabled = GridView1.DataKeys[e.Row.RowIndex].Value == "SomeValue"; //Or some other logic, like converting to a boolean 
    } 
} 

或者,

在你的aspx頁面的HTML標記,編輯LinkBut​​ton的enabled屬性到您所需的字段綁定。如:

<asp:LinkButton ID="LinkButton1" runat="server" Text="Edit" Enabled='<%# Convert.ToBoolean(Eval("SomeField")%>'></asp:LinkButton> 

希望有所幫助。