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

FindControl only sees ItemTemplate in Batch edit mode

7 Answers 417 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 15 Jul 2013, 03:53 PM
I'm having an issue trying to convert one of my RadGrids to Batch edit mode from EditForms. The following is all code that worked natively prior to changing the EditMode.

Here is an example of the problem, starting with the GridTemplateColumn causing problems:
<telerik:GridTemplateColumn HeaderText="Unit #" ColumnGroupName="GeneralInformation" UniqueName="EmpORUnitID" DataField="EmpORUnitID" HeaderStyle-Width="100px">
                <ItemTemplate>
                    <asp:Label ID="lblUnitID" runat="server" Text=<%#DataBinder.Eval(Container, "DataItem.EmpORUnitID")%>></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <telerik:RadComboBox runat="server" ID="rcbUnitNumber"
                        EnableLoadOnDemand="True" ShowMoreResultsBox="true" BorderStyle="None" Width="90px"
                        EnableVirtualScrolling="true" EmptyMessage="Choose a Unit #"
                        DataTextField="EmpORUnitID" MarkFirstMatch="True" Filter="StartsWith"
                        HighlightTemplatedItems="true" Height="200px" Text='<%#DataBinder.Eval(Container, "DataItem.EmpORUnitID")%>'>
                        <WebServiceSettings Method="GetUnitNumberList" Path="Timesheet.aspx" />
                    </telerik:RadComboBox>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="Odo Start" ColumnGroupName="GeneralInformation" UniqueName="OdoStart" DataField="OdoStart" HeaderStyle-Width="100px">
                <ItemTemplate>
                    <%#DataBinder.Eval(Container, "DataItem.OdoStart")%>
                </ItemTemplate>
                <EditItemTemplate>
                    <telerik:RadNumericTextBox runat="server" ID="rtbOdoStart" Width="50px" Text='<%#DataBinder.Eval(Container, "DataItem.OdoStart")%>'>
                    </telerik:RadNumericTextBox>
                    <telerik:RadButton ID="btnFindOdoStart" runat="server" Width="20px"
                                            OnClick="btnFindOdoStart_Click" Icon-PrimaryIconUrl="~/Images/Icons/gauge.png"></telerik:RadButton>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>

Then, I have the following (with some lines cut out) to extract values from the EditItemTemplate controls (bolded the relevant sections to this problem)

protected void rgTodaysVehicles_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                GridEditableItem editedItem = e.Item as GridEditableItem;
                SWG.Timesheet.WebApp.Entities.Timesheet timesheet = Session["CurrentTimesheet"] as SWG.Timesheet.WebApp.Entities.Timesheet;
                VehicleMileageSummary vehicleMileageSummary = timesheet.VehicleMileages[editedItem.ItemIndex];
                try
                {
                    vehicleMileageSummary = BuildVehicleMileageSummary(editedItem, vehicleMileageSummary);

The last line above leads to:

private VehicleMileageSummary BuildVehicleMileageSummary(GridEditableItem editedItem, VehicleMileageSummary vehicleMileage)
        {
            List<String> errorList = new List<string>();
            try
            {
                vehicleMileage.IsDirty = true;            
                RadComboBox cbxEmpOrUnitID = (RadComboBox)editedItem["EmpORUnitID"].FindControl("rcbUnitNumber");       
                vehicleMileage.EmpORUnitID = cbxEmpOrUnitID.Text;

I receive null Object errors for the bolded line above, as the control "rcbUnitNumber" isn't found. Using breakpoints and some testing I can confirm that "editedItem" only contains the controls from the column ItemTemplate rather than EditItemTemplate.

Can anyone see what I'm doing wrong here?



7 Answers, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 18 Jul 2013, 01:36 PM
Hi Jeff,

There are a few thing which you have to bare in mind when using the Batch EditMode. First data-binding expressions are not allowed in the EditItemTemplate. The edit mode is designed in such way that it will automatically populate the values. It finds the first input element in the template and sets it's value. When having more than one input control in the EditItemTemplate things should be handled manually like explained here. As for obtaining the old and new values in the OnUpdateCommand event handler you can use the code snippet below:
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        GridBatchEditingEventArgument argument = (e.CommandArgument as GridBatchEditingEventArgument);
        Hashtable newValues = argument.NewValues;
        Hashtable oldValues = argument.OldValues;
    }

Note that when the EditMode is set to Batch we are creating a fake editable item. This is done for optimizational purposes as only one editor is initialized for a column. Once you open a different cell for edit the editor is being repopulated. For accessing the editor you can use the code provided below:
RadGrid1.FindControl(tableView.ClientID + "_"+ column.UniqueName).Controls[0];


Regards,
Angel Petrov
Telerik
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 the blog feed now.
0
Jonathan
Top achievements
Rank 1
answered on 17 Oct 2013, 12:50 AM
Angel,

How would you get a handle of a control in Batch Edit mode on the client side?

Regards,
Jonathan
0
Angel Petrov
Telerik team
answered on 21 Oct 2013, 02:13 PM
Hi Jonathan,

You can subscribe to the OnBatchEditGetEditorValue or OnBatchEditSetEditorValue events and using findControl or findElement obtain a reference to the control like demonstrated in this help article.

Regards,
Angel Petrov
Telerik
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 the blog feed now.
0
Jonathan
Top achievements
Rank 1
answered on 21 Oct 2013, 03:10 PM
Hi Angel,

Thanks for responding.

I already know how to get the handle of a control in a GridTemplateColumn as demonstrated in that help topic. Perhaps my question wasn't clear enough and I do apologize for that.

What I meant to ask was this: How can I get the handle of an autogenerated control (like say, a RadComboBox in a GridDropDownColumn or a textbox in a GridBoundColumn, or a RadDatePicker in a GridDateTimeColumn) of a cell in Batch Edit mode, without first changing that column to a GridTemplateColumn and explicitly declaring the control in the <EditItemTemplate> markup?

The obvious challenge here is that this:

$telerik.findElement(container, "ControlName")

...will only work if you knew exactly what the control name was that you were trying to find (which would be the case if you were using a GridTemplateColumn). However, in the case of an autogenerated control, you won't know what the control name is.

So far, I've only been able to get around this by changing all of my columns in RadGrids that use Batch Edit mode to GridTemplateColumn so that I could apply custom JavaScript logic to the controls in them. I was just wondering if there was a way to do so without first changing all of those columns to GridTemplateColumn (which can be a little tedious if your project is fairly large).

Best regards,
Jonathan
0
Angel Petrov
Telerik team
answered on 24 Oct 2013, 02:19 PM
Hello Jonathan,

One possible solution to find the proper control in such scenarios it to first obtain a reference to the hidden editors that RadGrid renders. This is achievable by calling the GetBatchColumnEditor method. Once a reference is obtained you can assign a client-side event handler for the OnLoad event and store the id in a variable. Later this variable can be used for obtaining the client object. A sample demonstration of the mentioned is shown in the code snippets below:

C#
protected void RadGrid1_PreRender(object sender, EventArgs e)
   {
       RadComboBox combo = ((RadGrid1.MasterTableView.GetBatchColumnEditor("CategoryID") as GridDropDownListColumnEditor)).ComboBoxControl as RadComboBox;
       combo.OnClientLoad = "ClientLoad";
   }

JavaScript:
function OnBatchEditGetEditorValue(sender, args) {
            if (args.get_columnUniqueName() === "CategoryID") {
                var container = args.get_container();
                var combo = $telerik.findControl(container, comboID);
            }
        }
        var comboID;
        function ClientLoad(sender,args) {
            comboID = sender.get_id();
        }


Regards,
Angel Petrov
Telerik
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 the blog feed now.
0
Łukasz
Top achievements
Rank 1
answered on 14 Jul 2014, 07:09 AM
I registered on this forum only to tell thanks ! You're the best !
0
Angel Petrov
Telerik team
answered on 17 Jul 2014, 05:25 AM
Hello Ɓukasz,

Thank you for the nice words. I am glad that I was able to help you and other community members to resolve such type of problems.

Regards,
Angel Petrov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Jeff
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Jonathan
Top achievements
Rank 1
Łukasz
Top achievements
Rank 1
Share this question
or