Update and Delete Programmatically

12 posts, 1 answers
  1. Chase Florell
    Chase Florell avatar
    257 posts
    Member since:
    Apr 2005

    Posted 09 Sep 2008 Link to this post

    I like to keep all my code clean and thus I hate using datasources in my aspx pages.  Instead I like to try and do it all via the codebehind.  This however is posing a problem as I cannot delete records and I don't know where I am going wrong.

    aspx
            <telerik:RadGrid OnItemDeleted="RadGrid1_ItemDeleted"   
                             OnItemUpdated="RadGrid1_ItemUpdated"   
                             ID="RadGrid1"   
                             runat="server"   
                             Skin="Office2007"   
                             AllowPaging="true"   
                             AutoGenerateColumns="false"   
                             AllowSorting="true" 
                             AllowAutomaticDeletes="true"   
                             AllowAutomaticUpdates="true">  
                <PagerStyle Mode="NextPrevAndNumeric" /> 
                <MasterTableView TableLayout="Auto" DataKeyNames="ID">  
                    <RowIndicatorColumn> 
                        <HeaderStyle Width="20px" /> 
                    </RowIndicatorColumn> 
                    <ExpandCollapseColumn> 
                        <HeaderStyle Width="20px" /> 
                    </ExpandCollapseColumn> 
                    <Columns> 
                        <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn" /> 
                        <telerik:GridBoundColumn DataField="Title" HeaderText="Page Title" UniqueName="Title" Visible="true" ReadOnly="false" /> 
                        <telerik:GridBoundColumn DataField="MenuName" HeaderText="Menu Name" UniqueName="MenuName" Visible="true" ReadOnly="false" /> 
                        <telerik:GridTemplateColumn DataField="DateUpdated" HeaderText="Last Updated" UniqueName="DateUpdatedDisplay">  
                            <ItemTemplate> 
                                <asp:Label ID="lblDateUpdated" runat="server" Text='<%# FormatDate(DataBinder.Eval(Container.DataItem, "DateUpdated")) %>' /> 
                            </ItemTemplate> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridTemplateColumn DataField="DateCreated" HeaderText="Date Created" UniqueName="DateCreatedDisplay">  
                            <ItemTemplate> 
                                <asp:Label ID="lblDateCreated" runat="server" Text='<%# FormatDate(DataBinder.Eval(Container.DataItem, "DateCreated")) %>' /> 
                            </ItemTemplate> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridBoundColumn DataField="UpdatedBy" HeaderText="Updated By" UniqueName="UpdatedBy" Visible="true" ReadOnly="true" /> 
                        <telerik:GridBoundColumn DataField="isHomePage" HeaderText="Home Page" UniqueName="isHomePage" Visible="false" ReadOnly="false" /> 
                        <telerik:GridBoundColumn DataField="MetaKeywords" HeaderText="Meta Keywords" UniqueName="MetaKeywords" Visible="false" ReadOnly="false" /> 
                        <telerik:GridBoundColumn DataField="MetaDescription" HeaderText="Meta Description" UniqueName="MetaDescription" Visible="false" ReadOnly="false" /> 
                        <telerik:GridButtonColumn ConfirmText="Delete this page?" ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn">  
                            <ItemStyle HorizontalAlign="Center" Width="50px" /> 
                        </telerik:GridButtonColumn> 
                    </Columns> 
                </MasterTableView> 
            </telerik:RadGrid> 

    aspx.vb
        Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load  
            Dim PagesDC As New Dal.icms_PagesDataContext  
            Dim results = PagesDC.icms_Pages_GetPages  
            RadGrid1.DataSource = results  
            RadGrid1.DataBind()  
        End Sub 
     
        Protected Sub RadGrid1_ItemDeleted(ByVal source As ObjectByVal e As Telerik.Web.UI.GridDeletedEventArgs) Handles RadGrid1.ItemDeleted  
     
            Dim i As GridDataItem = DirectCast(e.Item, GridDataItem)  
            Dim ID As String = i.OwnerTableView.DataKeyValues(i.ItemIndex)("ID").ToString  
     
            Dim PagesDC As New Dal.icms_PagesDataContext  
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)  
            Dim results = PagesDC.icms_Pages_DeletePageByID(Convert.ToInt32(i))  
            RadGrid1.Rebind()  
        End Sub 
  2. binbin
    binbin avatar
    26 posts
    Member since:
    Sep 2006

    Posted 09 Sep 2008 Link to this post

    Hi
    Chase florell

    I think you must put your Bind code block on

    if (!Page.IsPostback)
    {

        Dim PagesDC As New Dal.icms_PagesDataContext 
            Dim results = PagesDC.icms_Pages_GetPages 
            RadGrid1.DataSource = results 
            RadGrid1.DataBind() 

    }
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Chase Florell
    Chase Florell avatar
    257 posts
    Member since:
    Apr 2005

    Posted 09 Sep 2008 Link to this post

    The grid is binding to the data as expected... the problem is calling the RadGrid1_ItemDeleted command
  5. aabdan
    aabdan avatar
    26 posts
    Member since:
    Apr 2007

    Posted 09 Sep 2008 Link to this post

    i think you should delete 
    OnItemDeleted="RadGrid1_ItemDeleted"
    OnItemUpdated="RadGrid1_ItemUpdated"
    because the grid call this methods by default
  6. Chase Florell
    Chase Florell avatar
    257 posts
    Member since:
    Apr 2005

    Posted 10 Sep 2008 Link to this post

    Yeah I'm pretty sure that's not it.
  7. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 10 Sep 2008 Link to this post

    Hi Chase,

    The ItemDeleted/ItemUpdated events will be raised when you have assigned data source control for the grid (setting its DataSourceID) and allowed automatic operations for the control.

    Since this does not seem to be your case, I suggest you set the AllowAutomatic[Action] properties of the grid to false and implement the update/delete operations manually intercepting the DeleteCommand/UpdateCommand events of the control.

    Additionally, consider using advanced binding through the NeedDataSource event and remove the code which uses simple binding with DataBind() from your PageLoad handler.

    Best regards,
    Stephen
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  8. Chase Florell
    Chase Florell avatar
    257 posts
    Member since:
    Apr 2005

    Posted 10 Sep 2008 Link to this post

    Can you elaborate on this at all?

    "Since this does not seem to be your case, I suggest you set the AllowAutomatic[Action] properties of the grid to false and implement the update/delete operations manually intercepting the DeleteCommand/UpdateCommand events of the control."

    Note: I am trying to use LINQ exclusively.

  9. Michele
    Michele avatar
    47 posts
    Member since:
    Dec 2006

    Posted 10 Sep 2008 Link to this post

    I'm doing exactly what you are trying to do.

    I'm using a DAL and BLL to accomplish this.

    Call the appropriate class in your BLL On RadGrid1_UpdateCommand OR the delete command

    Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)

    ID As String = (TryCast(editedItem("ID").Controls(0), TextBox)).Text

    Galleries.DeleteGallery(ID)

    And thats it. - CLEAN

    To edit I do the following.

    Protected Sub RadGrid1_UpdateCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.UpdateCommand

    'Get the GridEditableItem of the RadGrid

    Dim editedItem As GridEditableItem = TryCast(e.Item, GridEditableItem)

    'Access the textbox from the edit form template and store the values in string variables.

    Dim title As String = (TryCast(editedItem("Title").Controls(0), TextBox)).Text

    Dim ID As String = (TryCast(editedItem("ID").Controls(0), TextBox)).Text

    Dim i As Info = New Info

    i.ID = ID

    i.InfoName = title

    Info.EditInfoTitle(i)

    End Sub

  10. Chase Florell
    Chase Florell avatar
    257 posts
    Member since:
    Apr 2005

    Posted 10 Sep 2008 Link to this post

    I must be getting close
        Protected Sub RadGrid1_NeedDataSource(ByVal source As ObjectByVal e As GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource  
            Dim PagesDC As New Dal.icms_PagesDataContext  
            Dim results = PagesDC.icms_Pages_GetPages  
            RadGrid1.DataSource = results  
     
        End Sub 
     
        Protected Sub RadGrid1_ItemDeleted(ByVal source As ObjectByVal e As Telerik.Web.UI.GridDeletedEventArgs) Handles RadGrid1.ItemDeleted  
     
            Dim i As GridDataItem = DirectCast(e.Item, GridDataItem)  
            Dim theID As String = i.OwnerTableView.DataKeyValues(i.ItemIndex)("ID").ToString  
     
            Dim PagesDC As New Dal.icms_PagesDataContext  
            Dim results = PagesDC.icms_Pages_DeletePageByID(theID)  
            RadGrid1.Rebind()  
     
        End Sub 

    Am I calling the Method properly?
    Protected Sub RadGrid1_ItemDeleted(ByVal source As Object, ByVal e As Telerik.Web.UI.GridDeletedEventArgs) Handles RadGrid1.ItemDeleted

    <telerik:RadGrid OnItemDeleted="RadGrid1_ItemDeleted"   
                             OnItemUpdated="RadGrid1_ItemUpdated" 
                             ID="RadGrid1"   
                             runat="server"   
                             Skin="Office2007"   
                             AllowPaging="true"   
                             AutoGenerateColumns="false"   
                             AllowSorting="true" 
                             AllowAutomaticDeletes="false"   
                             AllowAutomaticUpdates="false"
  11. Answer
    Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 11 Sep 2008 Link to this post

    Hi Chase,

    As I explained in my previous reply, the ItemDeleted/ItemUpdated/ItemInserted events of RadGrid are meaningful only when you use automatic operations with data source control assigned for grid item generator. To perform update/delete with your own code, wire the UpdateCommand/DeleteCommand events of your grid instance.

    Best regards,
    Stephen
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  12. Chase Florell
    Chase Florell avatar
    257 posts
    Member since:
    Apr 2005

    Posted 11 Sep 2008 Link to this post

    I am trying your suggestion but it is still not firing the methods.

    ASPX
            <telerik:RadGrid OnDeleteCommand="RadGrid1_ItemDelete"   
                             OnUpdateCommand="RadGrid1_ItemUpdate" 
                             ID="RadGrid1"   
                             runat="server"   
                             Skin="Office2007"   
                             AllowPaging="true"   
                             AutoGenerateColumns="false"   
                             AllowSorting="true" 
                             AllowAutomaticDeletes="false"   
                             AllowAutomaticUpdates="false">  
                <PagerStyle Mode="NextPrevAndNumeric" /> 
                <MasterTableView TableLayout="Auto" DataKeyNames="ID">  
                    <RowIndicatorColumn> 
                        <HeaderStyle Width="20px" /> 
                    </RowIndicatorColumn> 
                    <ExpandCollapseColumn> 
                        <HeaderStyle Width="20px" /> 
                    </ExpandCollapseColumn> 
                    <Columns> 
                        <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn" /> 
                        <telerik:GridBoundColumn DataField="Title" HeaderText="Page Title" UniqueName="Title" Visible="true" ReadOnly="false" /> 
                        <telerik:GridBoundColumn DataField="MenuName" HeaderText="Menu Name" UniqueName="MenuName" Visible="true" ReadOnly="false" /> 
                        <telerik:GridTemplateColumn DataField="DateUpdated" HeaderText="Last Updated" UniqueName="DateUpdatedDisplay">  
                            <ItemTemplate> 
                                <asp:Label ID="lblDateUpdated" runat="server" Text='<%# FormatDate(DataBinder.Eval(Container.DataItem, "DateUpdated")) %>' /> 
                            </ItemTemplate> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridTemplateColumn DataField="DateCreated" HeaderText="Date Created" UniqueName="DateCreatedDisplay">  
                            <ItemTemplate> 
                                <asp:Label ID="lblDateCreated" runat="server" Text='<%# FormatDate(DataBinder.Eval(Container.DataItem, "DateCreated")) %>' /> 
                            </ItemTemplate> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridBoundColumn DataField="UpdatedBy" HeaderText="Updated By" UniqueName="UpdatedBy" Visible="true" ReadOnly="true" /> 
                        <telerik:GridBoundColumn DataField="isHomePage" HeaderText="Home Page" UniqueName="isHomePage" Visible="false" ReadOnly="false" /> 
                        <telerik:GridBoundColumn DataField="MetaKeywords" HeaderText="Meta Keywords" UniqueName="MetaKeywords" Visible="false" ReadOnly="false" /> 
                        <telerik:GridBoundColumn DataField="MetaDescription" HeaderText="Meta Description" UniqueName="MetaDescription" Visible="false" ReadOnly="false" /> 
                        <telerik:GridTemplateColumn DataField="isPublished" HeaderText="Published" UniqueName="isPublished">  
                            <ItemTemplate> 
                                <asp:Label ID="lblisPublished" runat="server" Text='<%# isPublished(DataBinder.Eval(Container.DataItem, "isPublished")) %>' /> 
                            </ItemTemplate> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridClientDeleteColumn ConfirmText="Delete this page?" ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn">  
                            <ItemStyle HorizontalAlign="Center" Width="50px" /> 
                        </telerik:GridClientDeleteColumn> 
                    </Columns> 
                </MasterTableView> 
            </telerik:RadGrid> 


    ASPX.VB
    #Region "RadGrid Functions"  
     
        Function FormatDate(ByVal input As DateTime) As String 
            Return Format(input, "MMMM dd, yyyy")  
        End Function 
     
        Function isPublished(ByVal input As BooleanAs String 
            If input = True Then : Return "Yes" 
            Else : Return "No" 
            End If 
        End Function 
     
        Protected Sub RadGrid1_NeedDataSource(ByVal source As ObjectByVal e As GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource  
            Dim PagesDC As New Dal.icms_PagesDataContext  
            Dim results = PagesDC.icms_Pages_GetPages  
            RadGrid1.DataSource = results  
     
        End Sub 
     
        Protected Sub RadGrid1_ItemDelete(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs)  
     
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)  
            Dim theID As String = item.OwnerTableView.DataKeyValues(item.ItemIndex)("ID").ToString  
     
            Dim PagesDC As New Dal.icms_PagesDataContext  
            Dim results = PagesDC.icms_Pages_DeletePageByID(theID)  
            RadGrid1.Rebind()  
     
        End Sub 
     
        Protected Sub RadGrid1_ItemUpdate(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs)  
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)  
            Dim id As String = item.GetDataKeyValue("ID").ToString()  
            RadGrid1.Rebind()  
        End Sub
    #End Region 

  13. Chase Florell
    Chase Florell avatar
    257 posts
    Member since:
    Apr 2005

    Posted 11 Sep 2008 Link to this post

    Ah I was kinda close

    I was using GridClientDeleteColumn when I should have been using GridButtonColumn

    Solved Now
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017