2010-11-08 66 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> 

希望有所帮助。