我在RadGrid内的GridTemplateColumn中有一个RadnumericTextBox,我希望能够在用户更改之前将MinValue属性设置为正在编辑的初始值。这个想法是用户可以继续增加值,但一旦保存就不能减少它。
我一直试图想出一些解决方案服务器端,但从我可以看出网格模板列不是网格渲染的一部分。
<telerik:RadGrid ID="StudentGrid" AutoGenerateColumns="false" AllowFilteringByColumn="True" AllowSorting="True" AllowPaging="True"
OnNeedDataSource="StudentGrid_OnNeedDataSource"
runat="server">
<telerik:GridTemplateColumn AllowFiltering="False" HeaderStyle-Width="25px" DataField="Sunday"
HeaderText="Sunday" UniqueName="SundayColumn">
<EditItemTemplate>
<telerik:RadNumericTextBox ID="editTextBox" Type="Number" MinValue="0" MaxValue="9"
DisplayText='<%# Bind("Sunday") %>' AllowOutOfRangeAutoCorrect="true" runat="server">
<NumberFormat GroupSeparator="" DecimalDigits="0"></NumberFormat>
</telerik:RadNumericTextBox>
</EditItemTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Sunday") %>
</ItemTemplate>
</telerik:GridTemplateColumn>
</telerik:RadGrid>
UPDATE :事实证明,网格模板在页面上呈现一次,并重新用于该列中的每个单元格。它可以在任何正在编辑的单元格之上重新定位和实现。由于它没有绑定到任何特定单元格,因此在呈现页面之前没有关于正在编辑的单元格的信息。请参阅下面的我的客户端解决方案。
答案 0 :(得分:0)
解决方案是拦截客户端OnCellSelecting网格事件以及OnFocus / OnBlur RadNumerictextBox事件。
<telerik:RadGrid ID="StudentGrid" AutoGenerateColumns="false" AllowFilteringByColumn="True" AllowSorting="True" AllowPaging="True"
OnNeedDataSource="StudentGrid_OnNeedDataSource"
runat="server">
<ClientSettings>
<Selecting CellSelectionMode="SingleCell"></Selecting>
<ClientEvents OnCellSelecting="StudentGrid_OnCellSelecting"/>
</ClientSettings>
<telerik:GridTemplateColumn AllowFiltering="False" HeaderStyle-Width="25px" DataField="Sunday"
HeaderText="Sunday" UniqueName="SundayColumn">
<EditItemTemplate>
<telerik:RadNumericTextBox ID="editTextBox" Type="Number" MinValue="0" MaxValue="9"
DisplayText='<%# Bind("Sunday") %>' AllowOutOfRangeAutoCorrect="true" runat="server">
<NumberFormat GroupSeparator="" DecimalDigits="0"></NumberFormat>
<ClientEvents OnFocus="onEditTextBoxFocus" OnBlur="onEditTextBoxBlur"></ClientEvents>
</telerik:RadNumericTextBox>
</EditItemTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Sunday") %>
</ItemTemplate>
</telerik:GridTemplateColumn>
</telerik:RadGrid>
然后添加这些javascript事件处理程序。
<script type="text/javascript">
function onEditTextBoxFocus(sender, eventArgs)
{
// sender is the RadNumericTextBox and does not contain any information
// about the grid row so we have to look at it's parent to get the
// grid row cells
var cells = sender.get_parent().get_element().cells;
// Switch from Telerik to jQuery to locate selected cell and get it's initial value
var selectedCell = $(cells).filter('.rgSelectedCell');
var initialValue = selectedCell.attr("InitialValue");
sender.set_minValue(initialValue);
}
function onEditTextBoxBlur(sender, eventArgs)
{
// Have to reset this value because it's interfering with
//validation on next cell being edited
sender.set_minValue(0);
}
function StudentGrid_OnCellSelecting(sender, eventArgs)
{
// Get the grid cell being selected
var columnName = eventArgs.get_column().get_uniqueName();
var data = eventArgs.get_gridDataItem();
var cell = data.get_cell(columnName);
var initialValue = cell.getAttribute("InitialValue");
// Set inital value if it doesn't already exist
if (initialValue == null)
{
var cellValue = cell.innerText.charAt(0);
if (cellValue == '') cellValue = 0;
// Set an InitialValue attribute in the cell about to be edited
// This will be picked up by the RadNumericTextBox when it gets focus (see above)
cell.setAttribute("InitialValue", cellValue);
}
}
</script>
OnCellSelecting事件处理程序在单元格选择上触发(第一个)并在编辑开始之前捕获单元格的初始值,并将其存储在网格单元格的InitialValue属性中。这是必需的,因为相同的RadNumericTextBox用于编辑使用网格模板的每个单元格。
稍后,当RadNumerictextBox获得焦点时,将从网格单元属性中检索初始值,并将其应用于RadNumerictextBox的MinValue。当RadNumericTextBox失去焦点时,MinValue需要重置为0,因为它似乎会继续在下一个被编辑的单元格上进行验证。