How to get the value of a textbox on RadGrid ItemCommand event handler when using a Custom Command?

The Light picture The Light · Oct 26, 2011 · Viewed 19.8k times · Source

I'm using RadGrid Form Templates as below;

<EditFormSettings EditFormType="Template">
    <FormTemplate>
        <table id="tblEditForm" cellpadding="2" cellspacing="2" width="100%" border="2px"
            class="tblEditForm">                           
            <tr>
                <th>
                    Server Name:
                </th>
                <td>
                    <asp:TextBox ID="tbServerName" runat="server" Text='<%# Bind("ServerName") %>' CssClass="tbServerName">
                    </asp:TextBox>
                </td>
            </tr>                                        
            <tr>
                <td colspan="2">
                    <div style="text-align: left; padding-left: 10px;display: inline; width: 50%">

                        <asp:LinkButton ID="lbTestConnection" runat="server" Text="Test Connection" CommandName="TestConnection" />
                        (It may take up to 15 seconds.)
                        <br />                                                                         
                    </div>
                    <asp:Label ID="lblTestConnectionResult" runat="server" CssClass="testConnectionResult"></asp:Label>      
                    <div style="text-align: right; padding-right: 10px;display: inline; float: right;">
                        <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                            runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>
                        </asp:Button>&nbsp;
                        <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                            CommandName="Cancel"></asp:Button>
                    </div>
                </td>
            </tr>
        </table>
    </FormTemplate>
</EditFormSettings>

When the Update link button is clicked on my RadGrid, the Edit Form is displayed. Then I click the Test Connection link button and ItemCommand event is raised.

public void OnRadGridItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == "TestConnection")
    {               
        var gridEditFormItem = e.Item as GridEditFormItem;
        if (gridEditFormItem == null)
            throw new ApplicationException("gridEditFormItem is null");
        var serverNameTextBox = gridEditFormItem.FindControl("tbServerName") as TextBox;
    }
}

The problem is that the gridEditFormItem variable is null at this stage so I can't figure out the value of the server name text box for example.

How to get the value of the textbox on RadGrid ItemCommand event handler?

If I click the default insert link button of the RadGrid instead, the gridEditFormItem has value so I can simply find the value of my textbox there.

Please help.

Thanks,

Answer

The Light picture The Light · Oct 26, 2011

I fixed it :)

 var gridEditFormItem = e.Item as GridEditFormItem ?? ((GridDataItem)(e.Item)).EditFormItem;

                if (gridEditFormItem == null)
                    throw new ApplicationException("gridEditFormItem is null");

When Inserting, the e.Item is a GridEditFormItem. When Updating, the e.Item is a GridDataItem!