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

Programmatic/Dynamic control of ReadOnly for GridTemplateColumn

2 Answers 354 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Carl
Top achievements
Rank 1
Carl asked on 23 Jan 2009, 12:45 AM
Is it possible to change the ReadOnly property of a GridTemplateColumn by dynamic programmatic control?

If so, what is the recommended way to do this? I looked for a code snippet but have not found it yet...

I want to develop a *.ascx control built using Telerik RadGrid such that there are various GridTemplateColumns for which I can dynamically change the ReadOnly status. Then I can put the *.ascx control with ReadOnly=true on certain *.aspx web pages and ReadOnly=False on other *.aspx web pages.

Thanks

CT

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 23 Jan 2009, 07:09 AM
Hi Carl,

Yes, you can change the ReadOnly property of the GridTemplateColumn dynamically. I guess you are having a RadGrid with several Template columns in an ascx and calling the ascx in different aspx pages. The ReadOnly property of the Template Columns will be differing in different aspx pages depending on some condition.

Here I have set the ReadOnly property of the Template column depending on the bool value that I have set in the corresponding aspx page.

CS:
protected void Page_Load(object sender, EventArgs e) 
    { 
        bool ReadOnly = false
 
       //access UserControl 
        UserControl usCtl = (UserControl)this.FindControl("WebUserControl1"); 
         
      // access Grid in the UserControl 
       RadGrid Grid = (RadGrid)usCtl.FindControl("RadGrid1"); 
        if (ReadOnly) 
        { 
            foreach (GridColumn col in Grid.MasterTableView.Columns) 
            { 
                if (col.ColumnType == "GridTemplateColumn"
                { 
                   // Setting the ReadOnly Property 
                    GridTemplateColumn tempCol = (GridTemplateColumn)col; 
                    tempCol.ReadOnly = ReadOnly; 
                } 
            } 
        } 
         
 
    } 


Thanks
Shinu
0
Carl
Top achievements
Rank 1
answered on 23 Jan 2009, 07:02 PM
Thanks for the advice. Here's the relevant code for an alternative approach that I'm using now:

        Protected Sub Page_Load(ByVal so As Object, ByVal sea As System.EventArgs) Handles Me.Load 
             
            If Me.IsOnAdminPage AndAlso Not Me.IsPostBack Then 
                Dim col As New Telerik.Web.UI.GridTemplateColumn 
                col = CType(telRadGrid.Columns.FindByUniqueName("MyCol1"), GridTemplateColumn) 
                col.ReadOnly = False 
                col = CType(telRadGrid.Columns.FindByUniqueName("MyCol2"), GridTemplateColumn) 
                col.ReadOnly = False 
                col = CType(telRadGrid.Columns.FindByUniqueName("MyCol3"), GridTemplateColumn) 
                col.ReadOnly = False 
            End If 
 

This does work OK for me (although I haven't yet tested with AJAX turned on). Note that the default is to have the control on a regular user page where ReadOnly = True for the GridTemplateColumns, and I'm now testing for IsPostBack and for presence of the control on an admin page before turning off the ReadOnly property, ie, resetting it to False. If anybody has any suggestions for a more efficient approach, then please let me know. Thanks.


Tags
Grid
Asked by
Carl
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Carl
Top achievements
Rank 1
Share this question
or