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

GridTemplateColumn textbox access in ItemCommand

2 Answers 260 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gary
Top achievements
Rank 1
Gary asked on 14 Jul 2008, 01:25 PM
Hi,
I am trying to change the property of a edititemtemplate's textbox control in the ItemCommand event to make certain textboxes readonly when the user wants to insert a record.  Here's the template column-
                    <telerik:GridTemplateColumn HeaderText="Cage Code" SortExpression="CageCode" UniqueName="CageCodeTemplate" > 
                        <ItemTemplate> 
                            <asp:Label runat="server" ID="lblCageCode" Text='<%# Eval("CageCode") %>' /> 
                        </ItemTemplate> 
                        <EditItemTemplate > 
                            <asp:TextBox runat="server" CausesValidation="true" ID="tbCageCode" Text='<%# Bind("CageCode") %>' /> 
                            <span style="color: Red">*</span><asp:RequiredFieldValidator ID="rvalCageCode" ControlToValidate="tbCageCode" 
                                ErrorMessage="Required Field!"  runat="server"></asp:RequiredFieldValidator> 
                        </EditItemTemplate> 
                    </telerik:GridTemplateColumn> 
 

Here's the snippet for the itemCommand, IGNORE the GridBoundColumn statements - I change from a GridBoundColumn to a templateColumn to allow for a validator.

        protected void RadGridCageCodeLookup_ItemCommand(object source, GridCommandEventArgs e)  
        {  
            //objectDatasource is stateless, must change the selectMethod to manufacturer code and add parameter of static mfg id.  
            if (e.CommandName.Contains("Insert"))  
            {  
 
                //FOR INSERTS, make cage code writable since the user is adding a CAGE code   
                //FOR insert, hide manufacturer id b/c they are in the manufacturer entity-can not change  
                ((GridBoundColumn)RadGridCageCode.Columns.FindByUniqueName("ManufacturerID")).ReadOnly = true;  
                ((GridBoundColumn)RadGridCageCode.Columns.FindByUniqueName("ManufacturerID")).Visible = false;  
            }  
      } 

Thanks in advance!
Gary

2 Answers, 1 is accepted

Sort by
0
Accepted
Veli
Telerik team
answered on 14 Jul 2008, 03:34 PM
Hello Gary,

If you need to make certain controls invisible or disabled on edit/insert, a more suitable place would be the ItemDataBound event of RadGrid:

void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    if (e.Item is GridEditableItem && e.Item.IsInEditMode && RadGrid1.MasterTableView.IsItemInserted) //If using Inplace edit mode 
    { 
        GridEditableItem item = e.Item as GridEditableItem; 
        (item.EditManager.GetColumnEditor("CageCodeTemplate"as GridTextBoxColumnEditor).Visible = false;  
    } 
 
    if (e.Item is GridEditFormInsertItem && e.Item.IsInEditMode) //if using edit forms 
    { 
        ((e.Item as GridEditFormInsertItem).FindControl("tbCageCode"as TextBox).Visible = false
    } 

The two IF statements cover the two scenarios: if using an InPlace edit mode, your edit form would be a GridEditableItem and you would retrieve the column editor for the specified column. Of you are using InForms or popup edit mode, you would find your control inside the GridEditFormInsertItem (for inserts only) using the FindControl() method.

All the best,
Veli
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Gary
Top achievements
Rank 1
answered on 14 Jul 2008, 03:52 PM
Thanks Veli!  You guys are great!
Tags
Grid
Asked by
Gary
Top achievements
Rank 1
Answers by
Veli
Telerik team
Gary
Top achievements
Rank 1
Share this question
or