GridViewColumn 编辑时只读

我有一个带有一些数据的网格视图,并且每一行都可以编辑数据,所以他们看到的是 EditTemplateField 而不是 templateField。现在具有特定职位的用户应该无法编辑列任务名称。


我第一次尝试是这样的:


protected void gvSubtasks_RowEditing(object sender, GridViewEditEventArgs e)

        {

            GridView gv = sender as GridView;

            gvSubtasks.Columns[0].ReadOnly= true;

            gv.EditIndex = e.NewEditIndex;

            BindGridView();

        }

但它没有找到只读属性。


后来我这样尝试:


   protected void gvSubtasks_RowEditing(object sender, GridViewEditEventArgs e)

    {

        GridView gv = sender as GridView;

        gv.EditIndex = e.NewEditIndex;

        BindGridView();

        foreach (GridViewRow row in gvSubtasks.Rows)

        {

            TextBox txtTaskName = row.Cells[0].FindControl("txtTaskName") as TextBox;


            if (IsRightUserPosition()){

                txtTaskName.Enabled = true;

            }

            else

            {

                txtTaskName.Enabled = false;

            }

        }

    }

但它没有找到文本框。


qq_花开花谢_0
浏览 205回答 1
1回答

慕盖茨4494581

您需要将 Readonly 属性设置为 true :((BoundField)gridView1.Columns[columnIndex]).ReadOnly = true;您可以尝试在 gridView1 的 RowDataBound 事件中执行此操作尝试在编辑模板中使用标签。C#:protected bool ShowButton(object DataItem){&nbsp; &nbsp; //Here you can place as many conditions as you like&nbsp;&nbsp; &nbsp; //Provided you always return either true or false&nbsp; &nbsp; if (Put Access condition here)&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; return false;}HTML 代码:&nbsp; &nbsp; <Columns>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <asp:TemplateField>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <asp:Label ID="lbldate" runat="server" Visible='<%# ShowButton(Eval("Process")) %>'></asp:Label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <EditItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <asp:Label ID="lbldate" Visible='<%# ShowButton(Eval("Process")) %>' runat="server" ></asp:Label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <asp:TextBox ID="lbldate" Visible='<%# ShowButton(Eval("Process")) %>' runat="server" ></asp:Label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </EditItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </asp:TemplateField>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </Columns>&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP