如何隐藏自动生成的删除和编辑按钮在用户的asp.net gridview中?

时间:2016-01-18 12:06:58

标签: c# asp.net gridview autogeneratecolumn

我想隐藏客户角色中的删除编辑按钮,但管理员可以看到它们。我的Gridview已设置autogenarated column = 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>

这是行数据绑定事件,我尝试执行show and hide column

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;
    }
}

1 个答案:

答案 0 :(得分:0)

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

$('[data-toggle="tooltip"]').tooltip('hide');