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

19.9k views Asked by At

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,

3

There are 3 answers

0
The Light On BEST ANSWER

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!

3
Razvan Trifan On

One way you could do it is to store the field values inside the RadGrid Datakeys. When OnRadGridItemCommand is raised, try getting the value like this:

string tbServerNameValue = RadGridID.MasterTableView.DataKeyValues[e.Item.ItemIndex]["field_name"];

Not sure if it's the right sintax, I can't test this code right now. Just give it a go.

0
Phoenix On

i tested blow code for asp.net in itemcommand and it correct.

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "TestConnection")
            {               
                var FormItem = e.Item as GridDataItem;
                if (FormItem == null)
                    throw new Exception("GridDataItem is null");
                var serverNameTextBox = FormItem.EditFormItem.FindControl("tbServerName") as TextBox;
        }

}