1

我正在使用 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 上单击更新链接按钮时,将显示编辑表单。然后我单击测试连接链接按钮并引发 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 变量在这个阶段是空的,所以我无法弄清楚例如服务器名称文本框的值。

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

如果我改为单击 RadGrid 的默认插入链接按钮,则 gridEditFormItem 具有值,因此我可以在那里简单地找到我的文本框的值。

请帮忙。

谢谢,

4

3 回答 3

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!

于 2011-10-26T14:10:18.787 回答
0

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

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

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

于 2011-10-26T13:24:21.033 回答
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;
        }

}
于 2012-12-26T04:53:47.600 回答