in
Forums
Blogs
DevExpress.com
Client Center
Support Center
DevExpress Channel

This Blog

Syndication

The ASPx Blog - Mehul Harry's DevExpress Blog

How to disable command buttons in ASPxGridView

Hey guys, check out this quick tip on how to disable command buttons like the Insert, Edit and Delete in the ASPxGridView.

The R&D team was also inspired to create a new event in the ASPxGridView to simply the approach described above. But more on that later.

Scenario

You've enabled edit/insert/delete functionality for your grid, however, you need to disable it for certain rows.

Solution

We'll dynamically check which rows need disabling because only certain rows will be disabled. The HtmlCommandCellPrepared event is perfect for this task because it allows you to change the settings of individual command column cells. For example, this code below will hide the 'New' button and disable the selection check box for every other row:

    protected void ASPxGridView1_HtmlCommandCellPrepared(object sender, ASPxGridViewTableCommandCellEventArgs e)
    {
        if (e.CommandCellType == DevExpress.Web.ASPxGridView.GridViewTableCommandCellType.Data)
        {
            if (e.VisibleIndex % 2 == 0)
            { // odd row
                e.Cell.Controls[1].Visible = false// hide the New button
                ((WebControl)e.Cell.Controls[3]).Attributes["disabled"] = "true"; // disable the selection checkbox
            }
        }
    }

You can download and see a live demo of this code at Code Central: How to customize command buttons in individual rows

New Event For Easier Approach

While the approach described above is good, the R&D team wanted to improve access to the command buttons at runtime. So a new event was implemented for the upcoming DXperience 2009 Volume 1 release. The new CommandButtonInitialize event allows you to change command button controls visibility and also enable/disable them easily. For example, the code below shows how to hide every third "Select" button and disable every second 'Select" button:

    protected void ASPxGridView1_CommandButtonInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCommandButtonEventArgs e)
    {
        if (e.Button.ButtonType != DevExpress.Web.ASPxGridView.ColumnCommandButtonType.Select) return;
        e.Visible = e.VisibleIndex % 3 != 1;
        e.Enabled = e.VisibleIndex % 2 != 1;
    }
Published Jan 13 2009, 02:22 PM by Mehul Harry (Developer Express)

Comments

 

Christopher D. Todd said:

Very nice! Hopefully, you will be discussing editmask fields in ASP shortly. ;)

Chris.

January 14, 2009 4:23 PM
 

Mehul Harry (Developer Express) said:

Thanks Christopher,

It's in the plans. Wink

Btw, quick tip. put the smiley in brackets [] for the graphic version.

January 14, 2009 5:10 PM
 

Andrew (Developer Express) said:

Christopher, We are working on mask for asp.net at the current moment and I believe it will be available in 2009.1 release.

January 15, 2009 1:52 AM
 

Paras Dave said:

Any idea of an approximate date when 2009.1 release will be released?

January 20, 2009 2:52 PM
 

Mehul Harry (Developer Express) said:

Hey Paras,

2009 volume 1 is scheduled to release sometime in March. There will be a beta available a few weeks before the release.

January 20, 2009 4:03 PM
 

Lukasz Rutkowski said:

How can I disable the command buttons only for specific rows (based on the data). E.g. you have a list tasks and each one has a stauts (e.g. 1,2,3) - i want to disable the command buttons for those tasks which have status=3.

By the way - i do not have CommandButtonInitialize on the event list (my version is 8.3.4).

February 14, 2009 4:33 AM
 

Mehul Harry (Developer Express) said:

Hello Lukasz,

Check out the forums where this question has several answers:

community.devexpress.com/.../68940.aspx

search.devexpress.com

Also, try the CodeCentral demo by clicking 'Download Sourcecode' button:

www.devexpress.com/.../E366.aspx

February 16, 2009 2:24 PM
 

Mihkel said:

Old and new approach is not working in firefox and new on does not let customize command button image. If I changes image of the button in CommandButtonInitialize for one row, all rows get new image.

April 20, 2009 9:35 AM
 

Mehul Harry (Developer Express) said:

Hi Mihkel,

The new approach works in FF. Make sure the Select button is enabled if you're using the code I posted above:

         <Columns>

           <dxwgv:GridViewCommandColumn ShowSelectCheckbox="True" VisibleIndex="0">

             <EditButton Visible="True">

             </EditButton>

             <SelectButton Visible="True">

             </SelectButton>

           </dxwgv:GridViewCommandColumn>

And yes, the images will be changed for all rows since this behavior is by design of the event. Unless, some specific row checking is done, the event is designed to loop through all the rows.

April 20, 2009 2:49 PM
 

Jonathan said:

I've been trying to get your gridview to work with a simple SQL Server database.  Select works fine, but it will not update or delete.  It keeps telling me "Incorrect syntax near '?'".  Well, I copied that from the devExp page itself.  

Here's the code:

<asp:SqlDataSource ID="sqlDataSource1" runat="server" ConnectionString="server=secret;User ID=secret;Password=secret;database=secret"

 SelectCommand="select CorpCode, CorpAbbr from NPR_Buyer_ConceptSupport"

 UpdateCommand="UPDATE [NPR_Buyer_ConceptSupport] SET [CorpCode] = ?, [CorpAbbr] = ? WHERE [CorpCode] = ?"

 DeleteCommand="DELETE FROM [NPR_Buyer_ConceptSupport] WHERE [CorpCode] = ?">

       <DeleteParameters>

           <asp:Parameter Name="CorpCode" Type="String" />

       </DeleteParameters>

       <UpdateParameters>

           <asp:Parameter Name="CorpCode" Type="String" />

           <asp:Parameter Name="CorpAbbr" Type="String" />

       </UpdateParameters>

 </asp:SqlDataSource>

Any ideas?  We are using the trial version.  Thanks.

April 23, 2009 9:12 PM
 

Mihkel said:

I made it work with writing following helper method. Now only image on certain row is changed and other remain as they were. I call this method in HtmlCommandCellPrepared event.

internal void MakeButtonDisabled(DevExpress.Web.ASPxGridView.ASPxGridViewTableCommandCellEventArgs e,string buttonTxt, string imgUrl)

{

DevExpress.Web.ASPxGridView.Rendering.GridViewCommandColumnButtonControl button = ((DevExpress.Web.ASPxGridView.Rendering.GridViewCommandColumnButtonControl)e.Cell.Controls.Cast<DevExpress.Web.ASPxGridView.Rendering.GridViewCommandColumnButtonControl>().FirstOrDefault(c => c.Text == buttonTxt));

if (button != null)

{

DevExpress.Web.ASPxClasses.Internal.InternalImage image = button.Controls[0] as DevExpress.Web.ASPxClasses.Internal.InternalImage;

button.Controls.Remove(image);

Image i = new Image();

i.ImageUrl = imgUrl;

button.Controls.Add(i);

}

}

I took ages to figure out that:(

April 24, 2009 2:48 AM

Leave a Comment

(required)  
(optional)
(required)  
Verification code: Required
   
Add

About Mehul Harry (Developer Express)

Mehul Harry is an ASP.NET technical evangelist at Developer Express. You can reach him directly at mharry@DevExpress.com. You can also follow him on Twitter: http://twitter.com/mehulharry
Copyright © 1998-2009 Developer Express Inc.
ALL RIGHTS RESERVED