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

How to access RadGrid updated item

2 Answers 138 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Adhelp
Top achievements
Rank 1
Adhelp asked on 19 Oct 2012, 03:01 PM
I've been searching your forums and reading documentation for several days now but have been unable to figure out how to access an updated item in my RadGrid.  

Basically, I have a RadGrid with a Client Event where OnCommand is set to "RaiseCommand".

In my JavaScript RaiseCommand function, I am able to determine the command that was raised.  When the command raised is "Update" I am calling ValidateInput function, passing along the sender and eventArgs that was passed to the RaiseCommand function.

Inside ValidateInput, I need to access updated values and do some edits to ensure the new values meet specific requirements.  However I have been unable to figure out how to access the new values.

So I thought I'd try accessing the new values in my codebehind.  I thought that if I could figure out how to do it there, I could use the same method in my javascript.  I created subroutines that handle EditCommand, and UpdateCommand.

Inside my subroutine for EditCommand, I have been able to find and display in a message box, the values in the row where Edit was clicked.

When I execute my web page, after clicking Edit on a row in my RadGrid, I then modify the contents of one of the four fields.  I then click on Update and my subroutine for UpdateCommand is invoked.  Inside this subroutine, I have been able to succesfully find and display the SavedOldValues.  However, I am unable to find the new value that I entered into one of the fields. 

Here's some of my code:
<telerik:RadGrid ID="rgUsers" runat="server" BorderWidth="1" BorderColor ="#EBAB00" AutoGenerateColumns="false" AllowSorting="true" >
    <ClientSettings>
        <ClientEvents OnCommand="RaiseCommand" />
    </ClientSettings>
    <ItemStyle Font-Size="10pt" Font-Names="Sans-serif" />
    <HeaderStyle Font-Size="12pt" Font-Names="Sans-serif" />
    <MasterTableView EditMode="InPlace">
        <Columns>
            <telerik:GridBoundColumn UniqueName="UserId" DataField="UserId"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="username" DataField="username"></telerik:GridBoundColumn>
             <telerik:GridBoundColumn UniqueName="level" DataField="level"></telerik:GridBoundColumn>
             <telerik:GridBoundColumn UniqueName="subcatid" DataField="subcatid"></telerik:GridBoundColumn>
             <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
             <telerik:GridClientDeleteColumn ConfirmText="Are you sure you want to delete this user?></telerik:GridClientDeleteColumn>
         </Columns>
     </MasterTableView>
</telerik:RadGrid>

Private Sub rgUsers_EditCommand(ByVal sender As Object, ByVal e As
Telerik.Web.UI.GridCommandEventArgs) Handles rgUsers.EditCommand
        Dim gdi As GridDataItem = CType(e.Item, GridDataItem)
        Dim UserId As String
        Dim UserName As String
        Dim Level As Integer
        Dim SubCategoryID As Integer
 
        UserId = gdi.Item("UserId").Text
        UserName = gdi.Item("username").Text
        Level = CType(gdi.Item("level").Text, Integer)
        SubCategoryID = CType(gdi.Item("subcatid").Text, Integer)
        MsgBox("EditCommand on " & UserId & " " & UserName & " " & Level.ToString & " " & SubCategoryID.ToString)
 
    End Sub

Private Sub rgUsers_UpdateCommand(ByVal sender As Object, ByVal e As
Telerik.Web.UI.GridCommandEventArgs) Handles rgUsers.UpdateCommand
        Dim rg As RadGrid = CType(sender, RadGrid)
        Dim gdie As GridDataItem = CType(e.Item, GridDataItem)
        Dim UpdatedRow As Integer = gdie.ItemIndex
        Dim MasterTable As GridTableView = rg.MasterTableView
        Dim gdim As GridDataItem = MasterTable.Items(UpdatedRow)
        Dim cell As TableCell
        Dim i As Integer
 
        MsgBox(gdim.SavedOldValues("UserId").ToString)
        MsgBox(gdim.SavedOldValues("username").ToString)
        MsgBox(gdim.SavedOldValues("level").ToString)
        MsgBox(gdim.SavedOldValues("subcatid").ToString)
 
        i = 0
        For Each cell In gdie.Cells
            MsgBox(i.ToString & " " & cell.Text)
            i += 1
        Next
 
        i = 0
        For Each cell In gdim.Cells
            MsgBox(i.ToString & " " & cell.Text)
            i += 1
        Next
End Sub

After I click on Update, my UpdateCommand handler fires, and I see message boxes containing the four saved old values.  But the following messages boxes (2 sets of 8) all display &nbsp.

I know there must something fundamental that I'm missing, but for the life of me, I've been unable to figure it out.

Please tell me how I can access the updated values in my codebehind, and then also in javascript.

Thanks!

2 Answers, 1 is accepted

Sort by
0
Adhelp
Top achievements
Rank 1
answered on 19 Oct 2012, 04:02 PM
I finally figured out how to access the values in my codebehind:
If TypeOf e.Item Is GridEditableItem And e.Item.IsInEditMode Then
            Dim edi As GridEditableItem = CType(e.Item, GridEditableItem)
            Dim tb As TextBox = CType(edi.Item("UserId").Controls(0),TextBox)
            MsgBox(tb.Text)
            tb = CType(edi.Item("username").Controls(0), TextBox)
            MsgBox(tb.Text)
            tb = CType(edi.Item("level").Controls(0), TextBox)
            MsgBox(tb.Text)
            tb = CType(edi.Item("subcatid").Controls(0), TextBox)
            MsgBox(tb.Text)
        End If


But I am still at a loss as to how to do the same thing in javascript.  The following code results in a message "Object doesn't support this property or method" on the Cell = ...   line.
function ValidateInput(sender, eventArgs) {
 
    var Grid = sender
    var MasterTable = Grid.get_masterTableView()
    var Row = MasterTable.get_dataItems([eventArgs.get_commandArgument()])
    var Cell = MasterTable.getCellByColumnUniqueName(Row, "UserId")
    alert(Cell.innerHTML)
 
 
    return true
}

0
Andrey
Telerik team
answered on 24 Oct 2012, 10:49 AM
Hello,

I have answered your query in the support thread you have opened, however, I will post the answer here as well, so it is available to the community:

function ValidateInput(sender, eventArgs) {
    var Grid = sender;
    var MasterTable = Grid.get_masterTableView();
    var Row = MasterTable.masterTable.get_editItems()[0];
    var Cell = Row.get_cell("UserId");
    var cell_input = Cell.childNodes[0].value
    
    alert(cell_input);
    return true
}

All the best,
Andrey
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.
Tags
Grid
Asked by
Adhelp
Top achievements
Rank 1
Answers by
Adhelp
Top achievements
Rank 1
Andrey
Telerik team
Share this question
or