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

Update and Delete Programmatically

11 Answers 466 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chase Florell
Top achievements
Rank 1
Chase Florell asked on 10 Sep 2008, 12:34 AM
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 

11 Answers, 1 is accepted

Sort by
0
binbin
Top achievements
Rank 1
answered on 10 Sep 2008, 03:37 AM
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() 

}
0
Chase Florell
Top achievements
Rank 1
answered on 10 Sep 2008, 04:11 AM
The grid is binding to the data as expected... the problem is calling the RadGrid1_ItemDeleted command
0
aabdan
Top achievements
Rank 1
answered on 10 Sep 2008, 04:41 AM
i think you should delete 
OnItemDeleted="RadGrid1_ItemDeleted"
OnItemUpdated="RadGrid1_ItemUpdated"
because the grid call this methods by default
0
Chase Florell
Top achievements
Rank 1
answered on 10 Sep 2008, 05:57 AM
Yeah I'm pretty sure that's not it.
0
Sebastian
Telerik team
answered on 10 Sep 2008, 08:25 AM
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.
0
Chase Florell
Top achievements
Rank 1
answered on 10 Sep 2008, 04:49 PM

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.

0
Michele
Top achievements
Rank 2
answered on 10 Sep 2008, 05:54 PM
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

0
Chase Florell
Top achievements
Rank 1
answered on 10 Sep 2008, 09:25 PM
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"
0
Accepted
Sebastian
Telerik team
answered on 11 Sep 2008, 08:27 AM
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.
0
Chase Florell
Top achievements
Rank 1
answered on 11 Sep 2008, 01:44 PM
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 

0
Chase Florell
Top achievements
Rank 1
answered on 11 Sep 2008, 01:50 PM
Ah I was kinda close

I was using GridClientDeleteColumn when I should have been using GridButtonColumn

Solved Now
Tags
Grid
Asked by
Chase Florell
Top achievements
Rank 1
Answers by
binbin
Top achievements
Rank 1
Chase Florell
Top achievements
Rank 1
aabdan
Top achievements
Rank 1
Sebastian
Telerik team
Michele
Top achievements
Rank 2
Share this question
or