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

How do I turn off AddNew

10 Answers 194 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Declan
Top achievements
Rank 2
Declan asked on 17 Sep 2008, 02:12 PM
Under certain circumstances I may wish to remove the ability for a user to add a new item to the grid.

e.g.

User adds clicks:
            <CommandItemSettings
                  AddNewRecordText="Click here to add new item"

After clicking save\update and certain conditions are met I wish to block the user from being able to add any more items. Ideally I would like to set AddNewRecordText = ""
AddNewRecordImageUrl = "spacer.gif" to hide it

Is there a property I can set to say AddNew.Enabled = false or something similar.

This is a hierarchical grid and I do not wish to prevent the user from continuing to add items to the detail grid. Only the Master should be disabled.

10 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 17 Sep 2008, 02:50 PM
Hello Declan,

You can try the following:
RadGrid1.MasterTableView.DetailTables[0].CommandItemDisplay = GridCommandItemDisplay.None; 

Alternatively you can try to handle ItemCommand and cancel the event (setting e.Canceled = true) when your conditions are met.

Kind regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Accepted
Princy
Top achievements
Rank 2
answered on 18 Sep 2008, 04:46 AM
Hello Declan,

You can check for the CommandName in the ItemCommand event and check for the conditions and then disable the AddNewRecord button as shown in the code below.
cs:
 protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        if (e.CommandName == "Save") 
        { 
           //check if conditions are met 
            foreach (GridCommandItem cmditm in RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)) 
            { 
                Button btn1 = (Button)cmditm.FindControl("AddNewRecordButton"); 
                btn1.Enable = false
                LinkButton lnkbtn1 = (LinkButton)cmditm.FindControl("InitInsertButton"); 
                lnkbtn1.Enable = false
            } 
        }  
    } 

Princy.
0
Declan
Top achievements
Rank 2
answered on 18 Sep 2008, 10:55 AM
Thanks Princy you put me in the right direction. I found that I had to use the PreRender event:

Private Sub radDeedsGrid_PreRender(ByVal sender As ObjectByVal e As System.EventArgs) Handles radDeedsGrid.PreRender 
 
    log.Debug("PreRender: " & PropertyCount.ToString) 
 
    For Each cmditm As GridCommandItem In radDeedsGrid.MasterTableView.GetItems(GridItemType.CommandItem) 
        Dim lnkbtn1 As LinkButton = DirectCast(cmditm.FindControl("InitInsertButton"), LinkButton) 
        If InStr(lnkbtn1.Text, "Click here to add a property") > 0 Then 
            If PropertyCount > 0 Then 
 
                lnkbtn1.Enabled = False 
                lnkbtn1.Visible = False 
 
            End If 
        End If 
    Next 
          
End Sub 


0
Softec
Top achievements
Rank 1
answered on 18 Aug 2009, 08:14 AM
How can I achieve this on DetailTables?
this.MasterTableView.DetailTables[0].GetItems(GridItemType.CommandItem) always returns 0 Items on a DetailTableView.

Thanks beforehand!
0
Princy
Top achievements
Rank 2
answered on 18 Aug 2009, 09:57 AM
Hello Studach Joerg,

You can use the Name property of the detail table to achieve the same scenario. Try out the following code:
aspx:
 <telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" runat="server" OnItemDataBound="RadGrid1_ItemDataBound" > 
        <MasterTableView Name="Master" DataSourceID="SqlDataSource1" >         
        <DetailTables> 
        <telerik:GridTableView Name="Detail" DataSourceID="SqlDataSource2" CommandItemDisplay="Top" runat="server">         

c#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item.OwnerTableView.Name == "Detail"
        { 
            foreach (GridCommandItem cmditm in e.Item.OwnerTableView.GetItems(GridItemType.CommandItem)) 
            { 
                Button btn1 = (Button)cmditm.FindControl("AddNewRecordButton"); 
                btn1.Enabled = false
                LinkButton lnkbtn1 = (LinkButton)cmditm.FindControl("InitInsertButton"); 
                lnkbtn1.Enabled = false
            } 
        } 
    } 

Thanks
Princy.
0
Softec
Top achievements
Rank 1
answered on 18 Aug 2009, 12:37 PM
I tried to achieve this on PreRender, when I moved my code to ItemDataBound it works. Thx for your input!
0
Daniel Elliott
Top achievements
Rank 1
answered on 02 Apr 2010, 07:33 PM
The last post Princy made is exactly what I need, but I need it to be conditionally disabled only when there are records PRESENT. How do I check for no records found?
0
Dimo
Telerik team
answered on 05 Apr 2010, 12:14 PM
Hello Daniel,

You can check

RadGrid_Instance.MasterTableView.Items.Count

in PreRender and if it is zero, then you have no DataItems.

Sincerely yours,
Dimo
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.
0
Daniel Elliott
Top achievements
Rank 1
answered on 05 Apr 2010, 01:48 PM
Thanks for your help,

How do I find out if a DetailsTable's Count is zero?
0
Dimo
Telerik team
answered on 05 Apr 2010, 02:53 PM
Hello Daniel,

The following help article should give you a clue:

http://www.telerik.com/help/aspnet-ajax/grdhideexpandcollapseimageswhennorecords.html

Greetings,
Dimo
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
Declan
Top achievements
Rank 2
Answers by
Daniel
Telerik team
Princy
Top achievements
Rank 2
Declan
Top achievements
Rank 2
Softec
Top achievements
Rank 1
Daniel Elliott
Top achievements
Rank 1
Dimo
Telerik team
Share this question
or