Disable ShowAddNewRecordButton

7 posts, 0 answers
  1. Tim
    Tim avatar
    7 posts
    Member since:
    Aug 2012

    Posted 24 Oct 2013 Link to this post

    I want clients to only be able to insert a single row in my Grid.  After the insert, i'd like to remove the ADD button...
    any pointers plz ?

    Tim
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 24 Oct 2013 Link to this post

    Hi Tim,

    You can check if the first insert has occurred then disable the AddNewRecord button in the PreRender event as shown below:

    C#:
    bool IsInsert=false;
     
    protected void RadGrid1_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            IsInsert = true;
          //Code to Insert
        }
     
     protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            if (IsInsert)
            {
                RadGrid1.MasterTableView.CommandItemSettings.ShowAddNewRecordButton = false;
                RadGrid1.Rebind();
            }
        }

    Thanks,
    Princy
  3. Tim
    Tim avatar
    7 posts
    Member since:
    Aug 2012

    Posted 25 Oct 2013 Link to this post

    Thanx Princy, however when I use the Rebind, my DetailsTableView doesn't get bound?  I'm using Advanced DataBinding.

        Public Shared RevenuesList As New List(Of Revenues)
        Public Shared IsInsert As Boolean = False
     
    #Region "RevenuesGrid Grid"
     
        Private Sub RevenuesGrid_DetailTableDataBind(sender As Object, e As GridDetailTableDataBindEventArgs) Handles RevenuesGrid.DetailTableDataBind
            Dim dataItem As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)
     
            If e.DetailTableView.Name = "Tracking" Then
     
                Dim id As Integer = CType(dataItem.GetDataKeyValue("SourceID"), Integer)
                e.DetailTableView.DataSource = RevenuesList.ToList.Where(Function(c As Revenues) c.ParentID = id)
     
            End If
        End Sub
     
        Private Sub RevenuesGrid_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles RevenuesGrid.ItemDataBound
     
            If "Tracking".Equals(e.Item.OwnerTableView.Name) Then
     
                If e.Item.IsInEditMode Then
     
                    Dim EditItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
                    Dim parentItem = CType(e.Item.OwnerTableView.ParentItem, GridDataItem)
                    Dim txtRecipient As TextBox = TryCast(EditItem.FindControl("txtRecipient"), TextBox)
                    Dim pnlRecipient As Panel = TryCast(EditItem.FindControl("pnlRecipient"), Panel)
     
                    'Do not do if PS
                    If (Not parentItem Is Nothing) Then
                        Dim ParentId As Integer = DirectCast(parentItem.OwnerTableView.DataKeyValues(parentItem.ItemIndex)("SourceID"), Integer)
                        If Not ParentId = 1 Then
                            If Not (TypeOf e.Item Is IGridInsertItem) Then
                                txtRecipient.Text = DataBinder.Eval(e.Item.DataItem, "Source")
                            End If
                        Else
                            txtRecipient.Visible = False
                            pnlRecipient.Visible = False
                        End If
                    End If
     
                    If (TypeOf e.Item Is GridFooterItem) Then
                        Dim footerItem As GridFooterItem = DirectCast(e.Item, GridFooterItem)
                        Dim lblFinancialAggregateTotal As Label = DirectCast(footerItem.FindControl("lblRevenuesGridFinancialAggregateTotal"), Label)
                        Dim lblInKindAggregateTotal As Label = DirectCast(footerItem.FindControl("lblRevenuesGridInKindAggregateTotal"), Label)
     
                        lblFinancialAggregateTotal.Text = "Total: "
                        lblInKindAggregateTotal.Text = "Total: "
     
                    End If
     
                End If
     
            End If
     
     
        End Sub
     
        Private Sub RevenuesGrid_InsertCommand(sender As Object, e As GridCommandEventArgs) Handles RevenuesGrid.InsertCommand
     
            If "Tracking".Equals(e.Item.OwnerTableView.Name) Then
     
                Page.Validate("Tracking")
     
                If Page.IsValid Then
     
                End If
                If (e.Item.IsInEditMode) Then
     
                    If (TypeOf e.Item Is GridDataInsertItem) AndAlso (e.Item.OwnerTableView.IsItemInserted) Then
     
                        Dim gridEditFormItem As GridEditableItem = DirectCast(e.Item, GridEditableItem)
                        Dim parentItem = CType(e.Item.OwnerTableView.ParentItem, GridDataItem)
     
                        If (Not parentItem Is Nothing) Then
     
                            Dim ParentId As Integer = DirectCast(parentItem.OwnerTableView.DataKeyValues(parentItem.ItemIndex)("SourceID"), Integer)
                            Dim txtRecipient As TextBox = TryCast(gridEditFormItem.FindControl("txtRecipient"), TextBox)
                            Dim RecipientYesNo As RadioButtonList = TryCast(gridEditFormItem.FindControl("RecipientYesNo"), RadioButtonList)
     
                            Dim newValues As New Hashtable()
                            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, gridEditFormItem)
     
                            Dim Financial As Decimal = newValues("Financial").ToString()
                            Dim InKind As Decimal = newValues("InKind").ToString()
     
                            'Give a Unique Index to track this Entry
                            Dim TrackingId As String = System.Guid.NewGuid().ToString().Split("-"c)(0)
     
                            'Is Source PS
                            If ParentId = 1 Then
                                RevenuesList.Add(New Revenues(TrackingId, "PS", Financial, InKind, ParentId, 1))
                                IsInsert = True
                            Else
                                RevenuesList.Add(New Revenues(TrackingId, txtRecipient.Text, Financial, InKind, ParentId, RecipientYesNo.SelectedValue))
                            End If
     
                            RevenuesList = RevenuesList.OrderBy(Function(c) c.Source).ToList
     
                            'Update the Amounts in the footer
                            Dim footerItem As GridFooterItem = DirectCast(RevenuesGrid.MasterTableView.GetItems(GridItemType.Footer)(0), GridFooterItem)
                            Dim FinancialAggregateTotal As Label = DirectCast(footerItem.FindControl("RevenuesGridFinancialAggregateTotal"), Label)
                            Dim InKindAggregateTotal As Label = DirectCast(footerItem.FindControl("RevenuesGridInKindAggregateTotal"), Label)
                            Dim lblSourceTotal As Label = DirectCast(footerItem.FindControl("lblSourceTotal"), Label)
                            Dim RevenuesGridAggregateTotal As Label = DirectCast(footerItem.FindControl("RevenuesGridAggregateTotal"), Label)
     
                            FinancialAggregateTotal.Text = String.Format("{0:$###,##0.00}", RevenuesList.Where(Function(x) x.ParentID <> 4).Sum(Function(x) x.Financial))
                            InKindAggregateTotal.Text = String.Format("{0:$###,##0.00}", RevenuesList.Where(Function(x) x.ParentID <> 4).Sum(Function(x) x.InKind))
     
                        End If
     
                    End If
     
                End If
     
            End If
     
        End Sub
     
        Private Sub RevenuesGrid_UpdateCommand(sender As Object, e As GridCommandEventArgs) Handles RevenuesGrid.UpdateCommand
     
            Dim gridEditFormItem As GridEditableItem = DirectCast(e.Item, GridEditableItem)
            Dim parentItem = CType(e.Item.OwnerTableView.ParentItem, GridDataItem)
     
            If (Not parentItem Is Nothing) Then
     
                Dim ParentId As Integer = DirectCast(parentItem.OwnerTableView.DataKeyValues(parentItem.ItemIndex)("SourceID"), Integer)
                Dim txtRecipient As TextBox = TryCast(gridEditFormItem.FindControl("txtRecipient"), TextBox)
                Dim RecipientYesNo As RadioButtonList = TryCast(gridEditFormItem.FindControl("RecipientYesNo"), RadioButtonList)
                Dim oldTrackingId As String = gridEditFormItem.OwnerTableView.DataKeyValues(gridEditFormItem.ItemIndex)("TrackingId").ToString()
     
                'DELETE THE OLD VALUES FIRST
                RevenuesList = RevenuesList.Where(Function(c) c.TrackingId <> oldTrackingId).ToList
     
                Dim newValues As New Hashtable()
                e.Item.OwnerTableView.ExtractValuesFromItem(newValues, gridEditFormItem)
     
                Dim Financial As Decimal = newValues("Financial").ToString()
                Dim InKind As Decimal = newValues("InKind").ToString()
     
                'Give a Unique Index to track this Entry
                Dim TrackingId As String = System.Guid.NewGuid().ToString().Split("-"c)(0)
     
                RevenuesList.Add(New Revenues(TrackingId, txtRecipient.Text, Financial, InKind, ParentId, RecipientYesNo.SelectedValue))
                RevenuesList = RevenuesList.OrderBy(Function(c) c.Source).ToList
     
                'Update the Amounts in the footer
                Dim footerItem As GridFooterItem = DirectCast(RevenuesGrid.MasterTableView.GetItems(GridItemType.Footer)(0), GridFooterItem)
                Dim FinancialAggregateTotal As Label = DirectCast(footerItem.FindControl("RevenuesGridFinancialAggregateTotal"), Label)
                Dim InKindAggregateTotal As Label = DirectCast(footerItem.FindControl("RevenuesGridInKindAggregateTotal"), Label)
     
                FinancialAggregateTotal.Text = String.Format("{0:$###,##0.00}", RevenuesList.Where(Function(x) x.ParentID <> 4).Sum(Function(x) x.Financial))
                InKindAggregateTotal.Text = String.Format("{0:$###,##0.00}", RevenuesList.Where(Function(x) x.ParentID <> 4).Sum(Function(x) x.InKind))
     
            End If
     
     
        End Sub
     
        Private Sub RevenuesGrid_NeedDataSource(sender As Object, e As GridNeedDataSourceEventArgs) Handles RevenuesGrid.NeedDataSource
            Dim RevenuesSource As New List(Of RevenuesSource)
            RevenuesSource.Add(New RevenuesSource("PS", 1))
            RevenuesSource.Add(New RevenuesSource("Other Government", 2))
            RevenuesSource.Add(New RevenuesSource("Non-Government", 3))
            RevenuesGrid.DataSource = RevenuesSource.ToList
        End Sub
     
        Private Sub RevenuesGrid_PreRender(sender As Object, e As EventArgs) Handles RevenuesGrid.PreRender
     
            If IsInsert Then
                RevenuesGrid.MasterTableView.CommandItemSettings.ShowAddNewRecordButton = False
                RevenuesGrid.Rebind()
            End If
     
        End Sub
     
    #End Region
     
     
        Public Class Revenues
             
            Public Sub New(ByVal TrackingId As String, ByVal Source As String, ByVal Financial As Integer, ByVal InKind As Integer, ParentID As Integer, RecipientType As Integer)
                _TrackingId = TrackingId
                _Source = Source
                '_SourceId = SourceId
                _Financial = Financial
                _InKind = InKind
                _ParentID = ParentID
                _RecipientType = RecipientType
            End Sub
            Private _TrackingId As String = "Empty"
            Property TrackingId As String
                Get
                    Return _TrackingId
                End Get
                Set(ByVal value As String)
                    _TrackingId = value
                End Set
            End Property
            Private _Source As String = "Empty"
            Public Property Source() As String
                Get
                    Return _Source
                End Get
                Set(ByVal value As String)
                    _Source = value
                End Set
            End Property
            Private _SourceId As Integer
            Property SourceId As Integer
                Get
                    Return _SourceId
                End Get
                Set(ByVal value As Integer)
                    _SourceId = value
                End Set
            End Property
            Private _Financial As Integer
            Property Financial As Integer
                Get
                    Return _Financial
                End Get
                Set(ByVal value As Integer)
                    _Financial = value
                End Set
            End Property
     
            Private _InKind As Integer
            Property InKind As Integer
                Get
                    Return _InKind
                End Get
                Set(ByVal value As Integer)
                    _InKind = value
                End Set
            End Property
     
            Private _ParentID As Integer
            Property ParentID As Integer
                Get
                    Return _ParentID
                End Get
                Set(ByVal value As Integer)
                    _ParentID = value
                End Set
            End Property
            Private _RecipientType As Integer
            Property RecipientType As Integer
                Get
                    Return _RecipientType
                End Get
                Set(ByVal value As Integer)
                    _RecipientType = value
                End Set
            End Property
        End Class
     
        Public Class RevenuesSource
     
            Private _Source As String = String.Empty
            Property Source As String
                Get
                    Return _Source
                End Get
                Set(ByVal value As String)
                    _Source = value
                End Set
            End Property
     
            Private _SourceID As Integer
            Property SourceID As Integer
                Get
                    Return _SourceID
                End Get
                Set(ByVal value As Integer)
                    _SourceID = value
                End Set
            End Property
     
            Sub New(ByVal Source As String, SourceID As Integer)
                _Source = Source
                _SourceID = SourceID
            End Sub
        End Class

  4. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2393 posts

    Posted 30 Oct 2013 Link to this post

    Hi Tim,

    In the provided code the Rebind() method is called in the PreRender event handler. This should be avoided. When an item in the RadGrid is expanded the Rebind() method is called implicitly.

    I am attaching a sample project created with the provided code. I have made a few modifications. The ShowAddNewRecordButton property is set to false in the InsertCommand handler. This way there is no need for the IsInserted variable.

    Regards,
    Viktor Tachev
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  5. Tim
    Tim avatar
    7 posts
    Member since:
    Aug 2012

    Posted 30 Oct 2013 Link to this post

    Victor, while your example does work, I cannot get mine grid to do the same.  I'm wondering if its becuase i'm using EditMode="InPlace"?

     

     

     

     

  6. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2393 posts

    Posted 04 Nov 2013 Link to this post

    Hi Tim,

    The InsertCommand is fired after clicking the Insert button in the form. The EditForm property should not matter in this case. The scenario is working as expected on my side with both InPlace and EditForms edit mode.

    Try removing all code from the RadGrid's PreRender event handler and ensure that the following line is executed in the InsertCommand handler.

    RevenuesGrid.MasterTableView.CommandItemSettings.ShowAddNewRecordButton = False

    You could first try commenting out the rest of the code in ItemCommand to make sure that the "Add new Item" button disappears after clicking Insert.

    If the issue persists would you send a support ticket with runnable project where the problem is observed. This would enable us to better understand your scenario and provide appropriate solution.

    Regards,
    Viktor Tachev
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  7. Ramya
    Ramya avatar
    2 posts
    Member since:
    Aug 2013

    Posted 19 Dec 2017 in reply to Viktor Tachev Link to this post

    I am trying to hide the ShowAddNewRecordButton  using the following code after checking a condition that my grid is empty. if there is a record already present then I have hide the ShowAddNewRecordButton  button else it should be visible as default.

    In UI <CommandItemSettings ShowAddNewRecordButton="true" ShowRefreshButton="false" />

    is added

    In RadGridAdditionalInfo_PreRender(object sender, EventArgs e)

    RadGridAdditionalInfo.MasterTableView.CommandItemSettings.ShowAddNewRecordButton = false;

    is added. But it is not working eventhough this method is triggered and the even after clr reads this line there no change.

    The Grid is displayed with Add New Record.

    FootNote:

    AddNewRecordButton and InitInsertButton are null in my case.

     

Back to Top