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

Changing columns to readonly

3 Answers 203 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 01 Jul 2015, 05:27 PM

I have a page which contains a batch mode grid (created declaratively in the aspx file) and all of the code to support the grid.  This grid page is contained within an IFrame in various pages on the site.  Depending upon which page is hosting the IFrame, we want to override the behavior of the grid so that the columns are read-only.  The default behavior allows the columns to be edited, but when the grid is included within certain pages, we want all of the columns to be read-only. 

Using (column as GridBoundColumn).ReadOnly = true; during the server-side PreRender event can fail if the column type is GridTemplateColumn (with Object reference not set).  That said,  I know I can respond to the BatchEditOpening event and use args.set_cancel(true) to prevent users from editing cells, but was hoping for a simple server-side solution to change the column to read-only. 

For the moment, I am responding to the PreRender event and testing for ColumnType before setting the column to read-only.  That just happens to work for me since the only GridTemplateColumn column is declaratively set to ReadOnly and all of the other column types seem to work.  However, that seems error prone.  Is there a way to easily set every column to read-only in my c# code?

Thanks.

3 Answers, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 06 Jul 2015, 08:55 AM
Hello,

Generally you should be able to set the GridTemplateColumn as ReadOnly on the server by subscribing to the PreRender event. Can you please send us the markup and code-behind of the page so we could examine it and research what might be causing this behavior.

Additionally bear in mind that I tried reproducing the problem in an isolated sample but was not able to do so. Please examine the attached web site and tell us what differs in your case. If you can modify it so that the problem can be replicated that would be great.

Regards,
Angel Petrov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Mike
Top achievements
Rank 1
answered on 10 Aug 2015, 08:16 PM

Hi Angel,

As a follow up, is there a way to change a column's readonly status with client-side javascript?   In this case, the user selects from two choices in a dropdown list.  Based upon the selection, I need to modify the content of ​a field (I update the innerText).  I can update the innerText, but if I click on the field, I get an error saying "Object doesn't support property or method 'getElementsByTagName'".  Looking at the call stack, it appears to fail while attempting to invoke _openEditFromEvent.  I suspect this is because the field is editable.  Perhaps, if I set the column to read-only, the edit event would not be generated. I want to do this on the client since the user must be able to seamlessly switch their selection to restore the original value and allow editing of the column. 

If I execute the following code, it replaces the innerText as expected.  However, if I select any of the updated fields I get the error. If I don't change the innerText, I can click on the cells without getting the error (but the value is not correct).

// ... get the 'editable' Local Currency price cells and ...
var regAvgPriceLCCell = dataItem.get_cell("REG_PRICE_LC");
var dirAvgPriceLCCell = dataItem.get_cell("DIR_PRICE_LC");
var yoiAvgPriceLCCell = dataItem.get_cell("YOI_AVG_PRICE_LC");
var y1AvgPriceLCCell = dataItem.get_cell("Y1_AVG_PRICE_LC");

//... and if we are asked to display dollar values ...
if (document.getElementById("<%=hiddenUseLocalCurrency.ClientID%>").value == "false") {
    regAvgPriceLCCell.innerText = dataItem.get_cell("REG_PRICE_DOLLARS").innerText.replace("$", "");
    dirAvgPriceLCCell.innerText = dataItem.get_cell("DIR_PRICE_DOLLARS").innerText.replace("$", "");
    yoiAvgPriceLCCell.innerText = dataItem.get_cell("YOI_AVG_PRICE_DOLLARS").innerText.replace("$", "");
    y1AvgPriceLCCell.innerText = dataItem.get_cell("Y1_AVG_PRICE_DOLLARS").innerText.replace("$", "");
}

 So, is it possible to change the column to readonly, and back again, using javascript?

Thank you

 

 

 
0
Mike
Top achievements
Rank 1
answered on 10 Aug 2015, 08:18 PM

I forgot to include an example of the column markup:

                    <telerik:GridBoundColumn  DataFormatString="{0:N2}" DataField="REG_PRICE_LC" DataType="System.Decimal" FilterControlAltText="Filter REG_PRICE_LC column" HeaderText="Suggested Regular Price" SortExpression="REG_PRICE_LC" UniqueName="REG_PRICE_LC">
                        <HeaderStyle Width="75px" BackColor="LightGreen" />
                        <ItemStyle Wrap="False"  HorizontalAlign="Right" />
                    </telerik:GridBoundColumn>​


 
Tags
Grid
Asked by
Mike
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Mike
Top achievements
Rank 1
Share this question
or