This is a migrated thread and some comments may be shown as answers.

how to disable rest of Grid when editing/inserting an item

4 Answers 425 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Margo Noreen
Top achievements
Rank 1
Iron
Veteran
Margo Noreen asked on 12 Apr 2010, 06:06 PM
I'm still coming up to speed on RadGrid (Telerik's controls general!)... So the scenario is I've got a RadControl on my page.  I've added an Edit button and the Add New Record button in the command row.    What I'd like to do is put the rest of the grid in a "disabled" state while on row is being worked on (whether that's editing an existing row after clicking the Edit button for that row, or having clicked the "Add New..." button). 

I already have an event handler setup to handle the edit/insert, so that part is easy enough.  What I don't see is an easy way to disable clicking on all other edit/delete buttons on the other rows in the grid, or any item in the command row.

Suggestions?

In a somewhat related vein... When the user is editing a row (edit or insert again), I'd like to have the return key be the equivalent of clicking the Update or Insert command.  Or at least get the tab order such that after tabbing out of the last editable column of the row that it goes next to the Update/Insert command/button.

Any help would be greatly appreciated.

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 13 Apr 2010, 08:21 AM
Hello Margo,

I guess you want to disable the Edit buttons in all the rows in grid when clicking the "Add New Record" button which is placed in the CommandItem. The code below shows how to access the Edit buttons and to disable it. Access the CommandItem and use the FindControl() method to access the button placed in CommandItemTemplate. The following code snippet would be helpful for you.

CS:
 
protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) 
    { 
        if (e.CommandName == "InitInsertCommandName"// Check for the CommandName 
        { 
            //for disabling buttons in CommandItemTemplate 
            GridCommandItem cmditem = (GridCommandItem)e.Item; 
            Button editbtn = (Button)cmditem.FindControl("editbtn"); 
            editbtn.Enabled = false
 
            //for disabling buttons for each row 
            foreach (GridDataItem item in e.Item.OwnerTableView.Items) 
            { 
                LinkButton editbutton = (LinkButton)item["EditButton"].Controls[0]; 
                editbutton.Enabled = false
                LinkButton deletebutton = (LinkButton)item["DeleteButton"].Controls[0]; 
                deletebutton.Enabled = false
            } 
         } 
     } 

And here is the markup that I used:
 
   . . .  
   <MasterTableView Name="Master" CommandItemDisplay="Top" EditMode="EditForms">  
                <CommandItemTemplate>  
                    <asp:Button ID="Button2" runat="server" Text="Add New Record" CommandName="InitInsertCommandName" />  
                    <asp:Button ID="Button3" runat="server" Text="Edit" CommandName="EditCommand" />  
                </CommandItemTemplate>                 
                <Columns>  
                    <telerik:GridButtonColumn CommandName="Edit" Text="Edit" UniqueName="EditButton">  
                    </telerik:GridButtonColumn>  
                      . . .  

Regards,
Shinu.
0
Margo Noreen
Top achievements
Rank 1
Iron
Veteran
answered on 13 Apr 2010, 09:37 PM
Hello Shinu, thanks for the reply.  I have a couple of follow-up questions.

First, I wasn't able to use the first part of the code to get a reference to the commanditem row.  When the code:

GridCommandItem cmditem = (GridCommandItem)e.Item;  

is called from within my RadGrid1_ItemCommand event handler, it throws an exception:

"Unable to cast object of type 'Telerik.Web.UI.GridDataItem' to type 'Telerik.Web.UI.GridCommandItem'.

I did a slight workaround by adding something like:

RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.None; 

but I would like to know how to properly get a reference to the commanditem row, and possibly generically loop through any controls contained therein.  There did not seem to be a RadGrid1.MasterTableView.CommandItem property, for example.

2nd... when I ran the foreach loop it didn't seem to "take".  I stepped through it in the debugger and the code runs and dutifully disables my Edit/Delete buttons, no errors.  But... then the NeedDataSource event fires after that.  I'm guessing that that rebind essentially wipes out the work that foreach loop did in the ItemCommand handler.   Any direction on when/where to handle this code then?

0
Margo Noreen
Top achievements
Rank 1
Iron
Veteran
answered on 14 Apr 2010, 05:16 PM
So I found a tip in the How To section of the RadControls Q1 2010 help doc (RadGrid -> Insert/Update/Delete records -> How-to) to loop and disable rows on the *PreRender* method and modified it a bit.  So this works nice

    protected void RadGrid1_PreRender(object sender, EventArgs e)  
    {  
        // in insert or edit mode, disable all the grid rows  
        if (RadGrid1.MasterTableView.IsItemInserted || RadGrid1.EditItems.Count > 0)  
        {  
            foreach (GridItem item in RadGrid1.Items)  
            {  
                item.Enabled = false;  
            }  
         }  
    } 

However, I'm still struggling w/ the CommandItem / row, where I have the "+ Add new record" link.  I'd like to disable that too when a user has clicked an Edit button (or that Add New link previoulsy) and has an editform open.

Adding a line like

"RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.None;"

right after the foreach loop doesn't seem to work; in fact, it seems to create some very weird results.

That same line of code does work if I add it to an ItemCommand method.  But, it strikes me as odd to have to do one particular piece of business logic (disable the grid when in edit/insert mode) in 2 different disparate places in code.

Also, for some reason, I'm just still struggling with how to get a reference to the CommandItem row.  I'd like to disable it.  Although setting the display setting to None seems to work, I'd rather just visually have it disabled like the rest of the rows.

Any guidance?
0
Iana Tsolova
Telerik team
answered on 16 Apr 2010, 03:33 PM
Hi Margo,

You can acces the grid CommandItem on ItemCreated, ItemDataBound or using the GetItems()method of the MasterTableView. Find more information on how to access grid cells and rows here.

I also suggest that you check out this online demo, illustrating how you can have custom CommandItem with different buttons depending on the items mode.

I hope this helps.

Best wishes,
Iana
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Margo Noreen
Top achievements
Rank 1
Iron
Veteran
Answers by
Shinu
Top achievements
Rank 2
Margo Noreen
Top achievements
Rank 1
Iron
Veteran
Iana Tsolova
Telerik team
Share this question
or