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

How do I loop through rows in a EditItemTemplate server side?

3 Answers 364 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 06 Jan 2014, 12:33 AM

I have a grid that has a GridTemplateColumn, with an EditItemTemplate inside. Inside the EditItemTemplate is a RadNumericTextBox. I'm trying to loop through the rows in the grid server side, outside the normal RadGrid events. I have a button that when clicked sends me server side and in that event I need to loop through and grab the values from all the rows.



I have no problem grabbing the DataKeyValue for the row, it's just the value of txtQuantity I am unable to get.


Here is my ASCX:

<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" DataSourceID="Products"
    Skin="Forest" GridLines="None" OnItemDataBound="RadGrid1_ItemDataBound" Width="50%">
    <ClientSettings>
         <Scrolling AllowScroll="True" UseStaticHeaders="True" SaveScrollPosition="true" ></Scrolling>
    </ClientSettings>
    <MasterTableView ShowFooter="true" AutoGenerateColumns="False"
        CurrentResetPageIndexAction="SetPageIndexToFirst" DataKeyNames="ID" DataSourceID="Products"
        Dir="LTR" Frame="Border" TableLayout="Auto" EditMode="Batch" BatchEditingSettings-OpenEditingEvent="Click" CommandItemDisplay="None">
        <Columns>
            <telerik:GridBoundColumn DataField="ID" DataType="System.Int32"  ReadOnly="True" UniqueName="ID" />
            <telerik:GridBoundColumn DataField="ProductName" ReadOnly="true" HeaderText="Product" UniqueName="ProductName" />
             
            <telerik:GridTemplateColumn DataField="Quantity" UniqueName="Quantity" HeaderText="Quantity" DataType="System.Int32">                               
                <ItemTemplate>
                        <%# Eval("Quantity")%>
                </ItemTemplate>
                <EditItemTemplate>
                    <telerik:RadNumericTextBox ID="txtQuantity" runat="server" Width="40px" DataType="System.Int32"
                         NumberFormat-DecimalDigits="0" Skin="Forest">
                        <ClientEvents OnBlur="Blur" OnFocus="Focus" />
                    </telerik:RadNumericTextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <telerik:RadNumericTextBox ID="TextBox2" runat="server" Width="40px" DataType="System.Int32" NumberFormat-DecimalDigits="0" Skin="Forest" ReadOnly="True">
                        <ClientEvents OnLoad="Load" />
                    </telerik:RadNumericTextBox>
                </FooterTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
    <PagerStyle Mode="NextPrevAndNumeric" />
</telerik:RadGrid>




Here is my Server side:



Protected Sub btnProcess_Click(sender As Object, e As EventArgs)
    Try
        For Each row As GridDataItem In RadGrid1.MasterTableView.Items
            Dim key As String = row.GetDataKeyValue("ID")
            Dim qty As RadNumericTextBox = DirectCast(row("Quantity").FindControl("txtQuantity"), RadNumericTextBox)
 
        Next
        
    Catch ex As Exception
        Response.Write(ex.ToString)
    End Try
End Sub




3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 06 Jan 2014, 04:33 AM
Hi Steve,

I'm not clear about your requirement, if you want to trigger the saving of the changes from an outer button. This can be achieved by obtaining a reference to the batch editing manager of the grid and calling saveChanges as follows:

ASPX:

<
telerik:RadButton AutoPostBack="false" Text="SaveChanges" ID="SaveChangesButton" runat="server" OnClientClicked="SaveChangesInGrid"></telerik:RadButton>

JavaScript:
function SaveChangesInGrid(sender,args) {
            var grid = $find('<%=RadGrid1.ClientID%>');
            grid.get_batchEditingManager().saveChanges(grid.get_masterTableView());
        }

Please Note, When EditMode is set to Batch an editor is created for each column and is only shown when a user clicks on a certain cell to edit it's content. Later when the cell value is changed a client side object is created which reflect the changes. If another cell from the same column is opened for editing the editor is being repopulated in order to expose the value from that cell. This logic is being executed on the client using JavaScript. The values are passed to the server once the Save Changes button is pressed.

Thanks,
Princy

0
Steve
Top achievements
Rank 1
answered on 06 Jan 2014, 11:43 AM
I'm saving the data to a different table, with additional information from other controls on the page. That is why I need to loop through each row independently of what the grid can do to save. So, I need to save the data myself. Only one cell per row is edited. All others are read only.
0
Accepted
Princy
Top achievements
Rank 2
answered on 07 Jan 2014, 06:00 AM
Hi Steve,

If you are trying to access the changing value of Quantity in RadNumericTextBox, its not possible from ServerSide but from ClientSide in the OnBatchEditCellValueChanged as shown below. As said in my previous post the values are passed to the server only when the Save Changes button is pressed.

ASPX:
<ClientSettings>
    <ClientEvents OnBatchEditCellValueChanged="CellValueChanged" />
</ClientSettings>

JS:
<script type="text/javascript">
    function CellValueChanged(sender, args) {  
        var row = args.get_row();
        var rowIndex = row.rowIndex;
        var columnName = args.get_columnUniqueName();
        var cell = args.get_cell();
        var oldValue = args.get_cellValue();
        var newValue = args.get_editorValue();    
    }
</script>

On the OnClick event of the Button, you can access the values in the view mode using GridDataItem, in this we cant access Edit items. To access the value of the GridTemplateColumn in ItemTemplate, please bind the DataField to a label as follows:

ASPX:
<ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Quantity")%>'></asp:Label>
</ItemTemplate>

C#:
protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridDataItem items in RadGrid1.MasterTableView.Items)
    {
        string id = items.GetDataKeyValue("ID").ToString();
        Label lbl = (Label)items.FindControl("Label1");
        string quantity = lbl.Text;           
    }
}

Thanks,
Princy
Tags
Grid
Asked by
Steve
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Steve
Top achievements
Rank 1
Share this question
or