Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally hide CommandField or ButtonField in Gridview

I have a GridView displaying person records. I want to conditionally show a CommandField or ButtonField based on some property of the underlying record. The idea is to only allow a command to be performed on specific people.

What is the best way to do this? I'd prefer a declarative solution to a procedural one.

like image 399
Larsenal Avatar asked Sep 22 '09 17:09

Larsenal


People also ask

How to hide CommandField in GridView asp net?

If you want to hide the entire column with that field, you can just do a GridView1. Columns[0]. Visible = false; Change the index to whatever column your edit one is.

How to hide button in GridView in asp net?

Using Inline Expression, the value of the EVAL function is compared and if the Status value is A then the Delete Button is displayed else the Button is hidden. Note: The syntax of using EVAL function within Inline Expression is different for C# and VB.Net.


2 Answers

First, convert your ButtonField or CommandField to a TemplateField, then bind the Visible property of the button to a method that implements the business logic:

<asp:GridView runat="server" ID="GV1" AutoGenerateColumns="false">     <Columns>         <asp:BoundField DataField="Name" HeaderText="Name" />         <asp:BoundField DataField="Age" HeaderText="Age" />         <asp:TemplateField>             <ItemTemplate>                 <asp:Button runat="server" Text="Reject"                  Visible='<%# IsOverAgeLimit((Decimal)Eval("Age")) %>'                  CommandName="Select"/>             </ItemTemplate>         </asp:TemplateField>     </Columns> </asp:GridView> 

Then, in the code behind, add in the method:

protected Boolean IsOverAgeLimit(Decimal Age) {     return Age > 35M; } 

The advantage here is you can test the IsOverAgeLimit method fairly easily.

like image 118
ViNull Avatar answered Sep 16 '22 17:09

ViNull


it could be done when the RowDataBound event fires

  protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)   {     if(e.Row.RowType == DataControlRowType.DataRow)     {       // Hide the edit button when some condition is true       // for example, the row contains a certain property       if (someCondition)        {           Button btnEdit = (Button)e.Row.FindControl("btnEdit");            btnEdit.Visible = false;       }     }      } 

Here's a demo page

markup

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DropDownDemo._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">     <title>GridView OnRowDataBound Example</title> </head> <body>     <form id="form1" runat="server">         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">             <Columns>                 <asp:BoundField HeaderText="Name" DataField="name" />                 <asp:BoundField HeaderText="Age" DataField="age" />                 <asp:TemplateField>                     <ItemTemplate>                                         <asp:Button ID="BtnEdit" runat="server" Text="Edit" />                     </ItemTemplate>                 </asp:TemplateField>             </Columns>         </asp:GridView>     </form> </body> </html> 

Code Behind

using System; using System.Collections.Generic; using System.Web.UI.WebControls;  namespace GridViewDemo {     public partial class _Default : System.Web.UI.Page     {         protected void Page_Load(object sender, EventArgs e)         {             GridView1.DataSource = GetCustomers();             GridView1.DataBind();         }          protected override void OnInit(EventArgs e)         {             GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);             base.OnInit(e);         }          void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)         {             if (e.Row.RowType != DataControlRowType.DataRow) return;              int age;             if (int.TryParse(e.Row.Cells[1].Text, out age))                 if (age == 30)                 {                     Button btnEdit = (Button) e.Row.FindControl("btnEdit");                     btnEdit.Visible = false;                 }         }          private static List<Customer> GetCustomers()         {             List<Customer> results = new List<Customer>();              results.Add(new Customer("Steve", 30));             results.Add(new Customer("Brian", 40));             results.Add(new Customer("Dave", 50));             results.Add(new Customer("Bill", 25));             results.Add(new Customer("Rich", 22));             results.Add(new Customer("Bert", 30));              return results;         }     }      public class Customer     {         public string Name {get;set;}         public int Age { get; set; }          public Customer(string name, int age)         {             Name = name;             Age = age;         }     } } 

In the demo, the Edit Button is not Visible (the HTML markup is not sent to the client) in those rows where the Customer's age is 30.

like image 31
Russ Cam Avatar answered Sep 19 '22 17:09

Russ Cam