2014-09-30 91 views
-1

在RadGrid内部的GridTemplateColumn中有一个RadnumericTextBox,我希望能够在用户可以更改之前将MinValue属性设置为正在编辑的初始值它。这个想法是,用户可以继续增加值,但一旦保存就不能减少它。如何将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

罗兰,这与[标签:asp-classic]无关。请[了解其中的差异](http://www.dotnetspider.com/tutorials/AspNet-Tutorial-26.aspx)。 – Paul 2014-10-01 08:16:30

回答

0

解决方案是与OnFocus/OnBlur Ra​​dNumerictextBox事件一起拦截客户端OnCellSelecting网格事件。

<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,因为它似乎继续验证下一个编辑的单元格。

相关问题