[英]How to hide autogenarated Delete and Edit button In asp.net gridview for user?
我想隱藏客戶角色的刪除編輯按鈕,但是管理員可以看到它們。 我的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>
<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>
<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;
}
}
您還可以如下使用Gridview prerender事件。
protected void grdview_RowDataBound(object sender, EventArgs e)
{
if(!admin())
{
grdview.Columns[5].Visible = true;
}
else
{
//do something
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.