简体   繁体   English

如何在ASP.net的gridview控件中禁用/隐藏编辑按钮

[英]How can I disable/hide edit button in gridview control of ASP.net

I have a gridview control in which there are two command buttons (linkButton), one is "select" and the other is "edit", now I have a session variable named as department and it contains values like "WRITING", "EDITING", "ADMIN" etc... Now the thing is I want the edit button to disappear when the department is "WRITING" but when it is "EDITING" or "ADMIN" I want edit button appear. 我有一个gridview控件,其中有两个命令按钮(linkBut​​ton),一个是“select”,另一个是“edit”,现在我有一个名为department的会话变量,它包含“WRITING”,“EDITING”等值,“管理员”等...现在问题是当部门是“写作”时我想要编辑按钮消失但是当它是“编辑”或“管理员”时我想要编辑按钮出现。

I was searching some forums for this problem and found this 我正在搜索一些论坛来解决这个问题并找到了这个

row.Cells[0].Controls.Clear();

The problem with this code is that it hides the entire command column including "select" and "edit" buttons while I only want to control "edit" button, select should remain visible for all departments. 这段代码的问题在于它隐藏了整个命令列,包括“选择”和“编辑”按钮,而我只想控制“编辑”按钮,选择应该对所有部门保持可见。

if I do it like (on row data bound event) 如果我喜欢(在行数据绑定事件上)

e.row.Cells[0].Controls[1].visible = false OR
e.row.Cells[0].Controls[0].visible = false

Specified argument was out of the range of valid values. 指定的参数超出了有效值的范围。

How can I do this properly ? 我该怎么做呢?

Thanks. 谢谢。

You can use the Visible property in the following way. 您可以通过以下方式使用Visible属性。

  if (Session(mode) == "WRITING") { (e.Row.FindControl("btnEdit")).Visible = false; } 

First of all - your code does not work because your gridViewCell at index 0 contains only one control. 首先 - 您的代码不起作用,因为索引0处的gridViewCell只包含一个控件。 Every CommandButton gets a single cell! 每个CommandButton都有一个单元格! Furthermore I am not quite sure why you make use of the RowDataBound event. 此外,我不太清楚为什么要使用RowDataBound事件。 As you are using a Session-Var this one may not change while binding your rows. 在使用Session-Var时,绑定行时可能不会更改。

I tried the following piece of code in my pageLoad eventHandler - and it worked out for me: 我在pageLoad eventHandler中尝试了以下代码 - 这对我来说很有用:

/// just replace CAPITAL letters with your names and 
/// (Session["department"] == "WRITING") with your boolean expression
gvYOURGV.Columns[INDEXOFSELECTCOLUMN].Visible = (Session["department"] == "WRITING");

In case you do want to have (need to have?) more flexibilty on modifying the appearance of your command columns - you may even consider, changing your command columns to templated columns. 如果您希望在修改命令列的外观时(需要具有?)更灵活 - 您甚至可以考虑将命令列更改为模板化列。 Doing so you may have the possibility for this code: 这样做你可能有这个代码:

<asp:GridView ID="gvYOURGV" runat="server">
        <Columns>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" 
                        visible='<%# Session["department"] != "WRITING" %>'
                        runat="server" CausesValidation="False" 
                        CommandName="Select" Text="Auswählen"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

hth 心连心

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在asp.net FormView控件中隐藏“编辑”按钮? - How to hide the “Edit” button in an asp.net FormView control? 使用ASP.NET GridView控件,如何在PageIndexChanged之后禁用GridViewRow中的控件或单个单元格? - With the ASP.NET GridView control, how can I disable controls or individual cells in a GridViewRow after PageIndexChanged? 在asp.net中查询gridview控件中的自动编辑按钮 - Query regarding auto edit button in gridview control in asp.net 单击asp.net中的gridview编辑按钮时如何禁用导出按钮 - How to Disable export button when click gridview edit button in asp.net 如何在ASP.Net中使用Button控件代替LinkBut​​ton进行gridview排序 - How can I use a Button control instead of LinkButton for gridview sorting in ASP.Net 如何在asp.net gridview中为用户隐藏自动生成的“删除和编辑”按钮? - How to hide autogenarated Delete and Edit button In asp.net gridview for user? 如何根据单选按钮选择隐藏字段集(并禁用该字段集的关联 ASP.NET 验证)? - How can I hide a fieldset (and disable the associated ASP.NET validation for that fieldset) based on a radio button selection? Gridview禁用1列asp.net上的编辑 - Gridview disable edit on 1 column asp.net ASP.NET GridView:如何在编辑模式下控制列的格式? - ASP.NET GridView: How to Control the Format of a Column in Edit Mode? 访问asp.net gridview编辑控件 - Access asp.net gridview edit control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM