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

Delete Not Working

5 Answers 171 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Naveen kumar
Top achievements
Rank 1
Naveen kumar asked on 20 Dec 2011, 12:44 PM
My Code is below and the Delete Button is not working?


aspx

 <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <asp:PlaceHolder ID="phdrGrid" runat="server" ></asp:PlaceHolder>
        <asp:SqlDataSource ID="sqldsGrid" runat="server"
            ConnectionString="<%$ ConnectionStrings:EFDBConnectionString %>"
            SelectCommand="SELECT * FROM [sites]"
            UpdateCommand="UPDATE [sites] SET [SiteName]=@SiteName WHERE [SiteId]=@SiteId"
            DeleteCommand="DELETE FROM [sites] WHERE [SiteId]=@SiteId">
        <UpdateParameters>
            <asp:Parameter Name="@SiteId" />
        </UpdateParameters>
        <DeleteParameters>
            <asp:Parameter Name="@SiteId" />
        </DeleteParameters>
        </asp:SqlDataSource>   

aspx.vb

Imports System.Data
Imports System.Data.SqlClient
Imports Telerik.Web.UI


Partial Public Class _Default
    Inherits System.Web.UI.Page

    Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Dim RadGrid1 As New RadGrid()
        'RadGrid1.NeedDataSource += New GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource)
        'RadGrid1.ItemCommand += New GridCommandEventHandler(RadGrid1_ItemCommand)
        'RadGrid1.ItemCreated += New GridItemEventHandler(RadGrid1_ItemCreated)
        'RadGrid1.ID = "rgDemo"
        RadGrid1.Width = Unit.Percentage(100)
        RadGrid1.PageSize = 3
        RadGrid1.AllowPaging = True
        RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric
        RadGrid1.AutoGenerateColumns = False
        RadGrid1.GroupingEnabled = True
        RadGrid1.ShowGroupPanel = True
        RadGrid1.ShowStatusBar = True
        RadGrid1.ClientSettings.AllowDragToGroup = True
        RadGrid1.DataSourceID = "sqldsGrid"
        RadGrid1.Skin = "Outlook"

        RadGrid1.MasterTableView.PageSize = 15
        RadGrid1.MasterTableView.DataKeyNames = New String() {"SiteId"}
        RadGrid1.MasterTableView.AllowAutomaticUpdates = True
        'RadGrid1.MasterTableView.EditFormSettings.EditFormType = GridEditFormType.Template
        RadGrid1.MasterTableView.EditMode = GridEditMode.InPlace
        'Dim gtc As New GridTemplateColumn()
        'gtc.ItemTemplate = New ItemTemplateLabels("Description")
        'gtc.HeaderText = "Description"
        'gtc.EditItemTemplate = New EditItemTemplateTextBoxes("Description")
        'RadGrid1.MasterTableView.Columns.Add(gtc)

        'Dim gt2 As New GridTemplateColumn()
        'gt2.ItemTemplate = New ItemTemplateLabels("Name")
        'gt2.HeaderText = "Name"
        'gt2.EditItemTemplate = New EditItemTemplateTextBoxes("Name")
        'RadGrid1.MasterTableView.Columns.Add(gt2)
        Dim boundColumn1 As New GridBoundColumn()
        boundColumn1.DataField = "SiteId"
        boundColumn1.UniqueName = "SiteId"
        boundColumn1.HeaderText = "SiteId"
        RadGrid1.MasterTableView.Columns.Add(boundColumn1)
        Dim boundColumn2 As New GridBoundColumn()
        boundColumn2.DataField = "SiteName"
        boundColumn2.UniqueName = "SiteName"
        boundColumn2.HeaderText = "SiteName"
        RadGrid1.MasterTableView.Columns.Add(boundColumn2)


        Dim gt3 As New GridTemplateColumn()
        gt3.ItemTemplate = New ItemTemplateLinks("Links")
        gt3.UniqueName = "EditCommandColumn"
        gt3.HeaderText = "Links"
        gt3.EditItemTemplate = New EditItemTemplateLinks("Links", "Update", "Update")
        gt3.EditItemTemplate = New EditItemTemplateLinks("Links", "Cancel", "Cancel")
        RadGrid1.MasterTableView.Columns.Add(gt3)

        Dim gBtnColumn As New GridButtonColumn()
        gBtnColumn.CommandName = "Delete"
        gBtnColumn.Text = "Delete"
        gBtnColumn.ButtonType = GridButtonColumnType.LinkButton
        RadGrid1.MasterTableView.Columns.Add(gBtnColumn)

        RadGrid1.MasterTableView.EditFormSettings.EditFormType = GridEditFormType.Template

        phdrGrid.Controls.Add(RadGrid1)
    End Sub

    Private Class ItemTemplateLinks
        Implements ITemplate
        Protected rBtn As RadButton
        Private colname As String = String.Empty
        Private colValue As String = String.Empty

        Public Sub New(ByVal cName As String)
            colname = cName
        End Sub

        Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
            rBtn = New RadButton()
            rBtn.Text = "Edit"
            rBtn.ButtonType = RadButtonType.StandardButton
            'rBtn.Command += New CommandEventHandler(rBtn_Command)
            rBtn.CommandName = "Edit"
            Dim rnd As New Random()
            rBtn.ID = "test"
            container.Controls.Add(rBtn)
        End Sub

        Private Sub rBtn_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
            Dim a As String = TryCast(sender, RadButton).CommandName
        End Sub

    End Class


    Private Class EditItemTemplateLinks
        Implements IBindableTemplate

        Private lstItemType As ListItemType
        Protected rBtn As RadButton
        Protected rBtn1 As RadButton
        Private colname As String = String.Empty
        Private btnText As String = String.Empty
        Private cmdName As String = String.Empty
        Private colValue As String = String.Empty

        Public Sub New(ByVal cName As String, ByVal bText As String, ByVal bCmdName As String)
            btnText = bText
            bCmdName = cmdName
            colname = cName
        End Sub

        Private Sub rBtn_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
            Dim a As String = TryCast(sender, RadButton).CommandName
        End Sub


        Public Function ExtractValues(ByVal container As System.Web.UI.Control) As System.Collections.Specialized.IOrderedDictionary Implements System.Web.UI.IBindableTemplate.ExtractValues

            Dim od As New OrderedDictionary()
            Return od

        End Function

        Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn

            rBtn = New RadButton()
            rBtn.Text = "Update"
            rBtn.ButtonType = RadButtonType.StandardButton
            rBtn.CommandName = "Update"
            'rBtn.Command += New CommandEventHandler(rBtn_Command)
            Dim rnd As New Random()
            rBtn.ID = "ssss"

            container.Controls.Add(rBtn)

            rBtn1 = New RadButton()
            rBtn1.Text = "Cancel"
            rBtn1.ButtonType = RadButtonType.StandardButton
            rBtn1.CommandName = "Cancel"
            Dim rnd1 As New Random()
            container.Controls.Add(rBtn1)

        End Sub
    End Class


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

End Class



When i click the Delete Button I am getting the Following error


Server Error in '/' Application.

Unable to cast object of type 'ItemTemplateLinks' to type 'System.Web.UI.IBindableTemplate'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'ItemTemplateLinks' to type 'System.Web.UI.IBindableTemplate'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[InvalidCastException: Unable to cast object of type 'ItemTemplateLinks' to type 'System.Web.UI.IBindableTemplate'.]
Telerik.Web.UI.GridTemplateColumn.FillValues(IDictionary newValues, GridEditableItem editableItem) +388
Telerik.Web.UI.GridEditableItem.ExtractValues(IDictionary newValues) +164
Telerik.Web.UI.GridTableView.ExtractValuesFromItem(IDictionary newValues, GridEditableItem editedItem) +116
Telerik.Web.UI.GridTableView.PerformDelete(GridEditableItem editedItem, Boolean suppressRebind) +175
Telerik.Web.UI.GridCommandEventArgs.ExecuteCommand(Object source) +4861
Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e) +191
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e) +137
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +118
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +135
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565


Version Information: Microsoft .NET Framework Version:2.0.50727.3623; ASP.NET Version:2.0.50727.3618

5 Answers, 1 is accepted

Sort by
0
Naveen kumar
Top achievements
Rank 1
answered on 31 Jan 2012, 02:24 PM





yes
0
Shinu
Top achievements
Rank 2
answered on 01 Feb 2012, 06:01 AM
Hello Naveen,

When you are doing automatic datasource operations, make sure that you have set AllowAutomaticDeletes as true. Check the following help documentation which explains more about this.
Automatic DataSource Operations.

Also check the following forum thread which discussed similar scenario.
Problem with EditItemTemplate returning Unable to cast object to type IBindableTemplate.

-Shinu.
0
Dan
Top achievements
Rank 1
answered on 09 Apr 2012, 02:20 PM
Naveen,

Did you ever get a solution to this? I have the same issue.

Thanks,
Dan
0
Maria Ilieva
Telerik team
answered on 12 Apr 2012, 08:01 AM
Hi Dan,

In case you are facing issue with deleting in RadGrid it will be best to open a regular support ticket and send us runnable version of application which demonstrates the problematic behaviour. Thus we will be able to debug it locally and do our best to provide a proper solution in a timely manner.


Greetings,
Maria Ilieva
the Telerik team
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 their blog feed now.
0
Dan
Top achievements
Rank 1
answered on 12 Apr 2012, 01:05 PM
Maria,

I also generated the grid programmatically on Page_Init and received a similar error. I'm not sure it mattered, but my sqldatasource was dynamically generated, also at Page_Init.

I was able to fix the error by setting AllowAutomaticDeletes to false and deleting the record on the Delete_Command event.

Thanks,
Dan
Tags
Grid
Asked by
Naveen kumar
Top achievements
Rank 1
Answers by
Naveen kumar
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Dan
Top achievements
Rank 1
Maria Ilieva
Telerik team
Share this question
or