使用自定义命令时如何获取 RadGrid ItemCommand 事件处理程序上文本框的值?
我正在使用 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>
<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 具有值,因此我可以简单地在那里找到文本框的值。
请帮忙。
谢谢,
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>
<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,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我修复了它:)
插入时,e.Item 是 GridEditFormItem。更新时,e.Item 是 GridDataItem!
I fixed it :)
When Inserting, the e.Item is a GridEditFormItem. When Updating, the e.Item is a GridDataItem!
一种方法是将字段值存储在 RadGrid 数据键内。当引发 OnRadGridItemCommand 时,尝试获取如下值:
不确定它是否正确,我现在无法测试此代码。试一试吧。
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:
Not sure if it's the right sintax, I can't test this code right now. Just give it a go.
我在 itemcommand 中测试了 ASP.NET 的打击代码,它是正确的。
i tested blow code for asp.net in itemcommand and it correct.