使用自定义命令时如何在RadGrid ItemCommand事件处理程序中获取文本框的值?

时间:2011-10-26 13:16:57

标签: c# asp.net html radgrid dataitem

我正在使用RadGrid表单模板,如下所示;

<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>

在我的RadGrid上单击更新链接按钮时,将显示编辑表单。 然后我单击Test Connection链接按钮并引发ItemCommand事件。

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;
    }
}

问题是gridEditFormItem变量在此阶段为null,因此我无法弄清楚服务器名称文本框的值。

如何在RadGrid ItemCommand事件处理程序中获取文本框的值?

如果我点击RadGrid的默认插入链接按钮,gridEditFormItem有值,所以我可以在那里找到我的文本框的值。

请帮忙。

谢谢,

3 个答案:

答案 0 :(得分:2)

我修好了它:)

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

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

当插入时,e.Item是一个GridEditFormItem。更新时,e.Item是一个GridDataItem!

答案 1 :(得分:0)

您可以这样做的一种方法是将字段值存储在RadGrid Datakeys中。引发OnRadGridItemCommand时,尝试获取如下值:

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

不确定它是否是正确的sintax,我现在无法测试此代码。试试吧。

答案 2 :(得分:0)

我在itemcommand中测试了asp.net的打击代码并且它正确。

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;
        }

}