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

How to findcontrol inside radgrid ItemCommand Event

3 Answers 523 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Encarna
Top achievements
Rank 1
Encarna asked on 11 Feb 2015, 12:06 PM
Hello,

I have this grid structure, in which the field is in the PlaceHolder only have to show the insertion of a new record.

<EditFormSettings EditFormType="Template">
                <EditColumn ButtonType="ImageButton" />
                <FormTemplate>
                    <table>
                        <tr>
                            <td>Nombre de usuario: </td>
                            <td>
                                <asp:TextBox ID="login" runat="server" Text='<%# Bind("Login") %>' />
                            </td>
                        </tr>
                        <asp:PlaceHolder ID="phInsert" runat="server">
                            <tr>
                                <td>ContraseƱa: </td>
                                <td>
                                    <asp:TextBox ID="password" runat="server" Text='<%# Bind("Password") %>' />
                                </td>
                            </tr>
                        </asp:PlaceHolder>
                        </tr>
                    </table>
                </FormTemplate>
            </EditFormSettings>

In code -behind I have the following:

protected void RadGridUserAccount_ItemCommand(object sender, GridCommandEventArgs e)
        {
            RadGrid grid = sender as RadGrid;

            if (e.CommandName == "InitInsert")
            {
                //var gridEditFormItem = e.Item as GridEditFormItem ?? ((GridDataItem)(e.Item)).EditFormItem;
                GridItem cmdItem = grid.MasterTableView.GetItems(GridItemType.CommandItem)[0];
                //GridEditFormItem editform = (GridEditFormItem)((Telerik.Web.UI.GridDataItem)(e.Item)).EditFormItem;
                PlaceHolder insert = cmdItem.FindControl("phInsert") as PlaceHolder;
                //GridEditFormItem gridEditFormItem = (GridEditFormItem)e.Item;

                //PlaceHolder insert = (PlaceHolder)gridEditFormItem.FindControl("phInsert");
               
                insert.Visible = true;
            }
        }

This code is not working for me, always return NullReferenceException.

Would appreciate help as I have tried in many ways and I always get the same exception.


3 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 16 Feb 2015, 09:14 AM
Hello Encarna,

As I understand the scenario, you would like to display the phInsert PlaceHolder only in insert mode. On order to implement the functionality you can use the ItemDataBound event. In the handler you should add a condition in order to determine if RadGrid is in Insert mode.

Check out the following resources that illustrate how you can determine when RadGrid is in Insert or Edit mode. The second article illustrates how you can access controls in the FormTemplate.



Regards,
Viktor Tachev
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.

 
0
Encarna
Top achievements
Rank 1
answered on 17 Feb 2015, 02:47 PM
Hello,

Thanks for the reply. 

I finally solved it.

<EditFormSettings EditFormType="Template">
    <FormTemplate>
        <asp:Panel ID="GridInsertPanel" runat="server" Visible='<%# Container is IGridInsertItem %>'>
            Inserting...
        </asp:Panel>
        <asp:Panel ID="GridEditPanel" runat="server" Visible='<%# !(Container is IGridInsertItem) %>'>
            Editing...
        </asp:Panel>
    </FormTemplate>
</EditFormSettings>

protected void RadGrid_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                //Code here
            }
        }
0
Viktor Tachev
Telerik team
answered on 19 Feb 2015, 02:06 PM
Hello Encarna,

Thank you for sharing your solution with the community.

Regards,
Viktor Tachev
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
Encarna
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Encarna
Top achievements
Rank 1
Share this question
or