【问题标题】:How to hide autogenarated Delete and Edit button In asp.net gridview for user?如何在 asp.net gridview 中为用户隐藏自动生成的删除和编辑按钮?
【发布时间】:2016-01-18 12:06:58
【问题描述】:

我想对客户角色隐藏删除编辑按钮,但管理员可以看到它们。我的 Gridview 已设置自动生成的列 = true。

这是我所做的:

<asp:GridView ID="grdview" OnRowCreated="grdview_RowCreated" OnRowDataBound="grdview_RowDataBound" CssClass="table table-hover table-responsive" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="EntityDataSource1">
    <Columns>
        <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" Visible="False" />
        <asp:BoundField DataField="InvoiceNo" HeaderText="InvoiceNo" SortExpression="InvoiceNo" NullDisplayText="Null" >
            <ControlStyle CssClass="form-control" />
        </asp:BoundField>
        <asp:TemplateField HeaderText="InvoiceDate" SortExpression="InvoiceDate">
            <EditItemTemplate >
                <div class=" input-group">
                    <asp:TextBox ID="txtdate2" runat="server" CssClass="form-control" Text='<%# Bind("InvoiceDate") %>'></asp:TextBox> 
                    <span class="input-group-addon">        
                        <a class=" glyphicon glyphicon-calendar" id="cal2"></a>
                        <asp:CalendarExtender ID="CalendarExtender2" PopupButtonID="cal2" Format="dd/MM/yyyy" TargetControlID="txtdate2" runat="server"></asp:CalendarExtender>
                    </span>
                 </div>
              </EditItemTemplate>
              <ItemTemplate>
                  <asp:Label ID="Label1" runat="server" Text='<%# Bind("InvoiceDate") %>'></asp:Label>
              </ItemTemplate>
              <HeaderStyle Width="200px" />
              <ItemStyle Width="200px" />
          </asp:TemplateField>
          <asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" >
              <ControlStyle CssClass="form-control" />
          </asp:BoundField>
          <asp:BoundField DataField="Items" HeaderText="Items" SortExpression="Items" >
              <ControlStyle CssClass="form-control" />
          </asp:BoundField>
          <asp:BoundField DataField="category" HeaderText="Category" SortExpression="category" Visible="False" >
              <ControlStyle CssClass="form-control" />
          </asp:BoundField>
          <asp:BoundField DataField="description" HeaderText="Description" SortExpression="description" Visible="False" />
          <asp:BoundField DataField="qty" HeaderText="Qty" SortExpression="qty" >
              <ControlStyle CssClass="form-control" />
          </asp:BoundField>
          <asp:BoundField DataField="remarks" HeaderText="Remarks" SortExpression="remarks" >
              <ControlStyle CssClass="form-control" />
          </asp:BoundField>
          <asp:BoundField DataField="PartyId" HeaderText="PartyId" SortExpression="PartyId" Visible="False" />
          <asp:TemplateField HeaderText="Action"   ShowHeader="False" SortExpression="id">
              <EditItemTemplate>
                  <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
                  &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
              </EditItemTemplate>
              <ItemTemplate>
                  <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
                  &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton>
              </ItemTemplate>
          </asp:TemplateField>
      </Columns>
 </asp:GridView>

这是我尝试执行显示和隐藏列的行数据绑定事件

protected void grdview_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) {
        if (!admin())
           e.Row.Cells[5].Visible = false;
    }
    else if (e.Row.RowType == DataControlRowType.Header) {
        if(!admin()) 
           e.Row.Cells[6].Visible = false;
    }
}

【问题讨论】:

  • 如果你想隐藏列而不是为什么需要检查行类型?

标签: c# asp.net gridview autogeneratecolumn


【解决方案1】:

您还可以使用 Gridview 预渲染事件,如下所示。

    protected void grdview_RowDataBound(object sender, EventArgs e)
    {
        if(!admin())
        {
           grdview.Columns[5].Visible = true;
        }
        else
        {
           //do something
        }            
    }

【讨论】:

  • 不,它不工作 protected void grdview_PreRender(object sender, EventArgs e) { grdview.Columns[0].Visible = false; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 1970-01-01
  • 1970-01-01
  • 2011-01-06
  • 2018-10-19
相关资源
最近更新 更多