Radgrid中RadNumericTextbox的KeyPress事件,用于交换列值

时间:2012-09-25 13:37:32

标签: c# telerik radgrid

我正在使用我的一个表单中的RadGrid在C#中开发一个网站。下面是RadGrid的ASPX代码:

<telerik:RadGrid ID="GridViewAllocation" runat="server" Height="200px" Width="100%"
AutoGenerateColumns="False" GridLines="None"
OnItemDataBound="GridViewAllocation_ItemDataBound"
OnItemCommand="GridViewAllocation_ItemCommand">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn HeaderText="Amount O/S"   UniqueName="AMT" DataField="AMT">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn HeaderText="Allocate" UniqueName="Acc_Allocated"
                DataField="Acc_Allocated">
                <ItemTemplate>
                    <telerik:RadNumericTextBox ID="TextGirdAmntAlloc" runat="server" Value="0"
                    CssClass="gridTextEntry" OnTextChanged="TextGirdAmntAlloc_TextChanged"
                    AutoPostBack="true">
                        <NumberFormat GroupSeparator="" DecimalDigits="4" />
                    </telerik:RadNumericTextBox>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridBoundColumn UniqueName="ACC_SOURCE" DataField="ACC_SOURCE" Visible="false">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

在此网格中,我想在单击或按键或其他任何事件时将coloumn AMT 的确切值传输到文本框 Acc_Allocated 。转移值有一个条件: 当 Acc_Source 值为“3”时,则传输该值,否则不传输。目前我正在通过TextChanged事件从服务器端进行检查。

还有其他方法吗?

这是我的TextChanged事件:

   protected void TextGirdAmntAlloc_TextChanged(object sender, EventArgs e)
   {
       try
       {
         foreach (GridDataItem GrdItm in GridViewAllocation.MasterTableView.Items)
          {
            decimal GrdAccAllocAmnt = Convert.ToDecimal(((RadNumericTextBox)GrdItm.FindContro("TextGirdAmntAlloc")).Text);
            if (GrdItm["ACC_SOURCE"].Text == "3")
            {                   
                LblAmnt_Alloc.Text = GrdItm.Cells[4].Text;
                ((RadNumericTextBox)GrdItm.FindControl("TextGirdAmntAlloc")).Text = GrdItm.Cells[4].Text;                
            }
          }
        }
        catch { }
    }

1 个答案:

答案 0 :(得分:0)

请尝试使用以下代码段。

JS

 <script type="text/javascript">
        function ValueChanged(sender, args) {
            var grid = $find("<%= RadGrid1.ClientID %>");
            if (grid) {
                var MasterTable = grid.get_masterTableView();
                var Rows = MasterTable.get_dataItems();
                for (var i = 0; i < Rows.length; i++) {
                    var row = Rows[i];
                    var getCellText = row.get_cell("Name").innerHTML;
                    if (getCellText == "Name1") {
                        var TextGirdAmntAlloc = $telerik.findControl(row.get_element(), "TextGirdAmntAlloc");
                        TextGirdAmntAlloc.set_value(row.get_cell("ID").innerHTML);
                    } 
                }
            }
        }
    </script>

的.aspx

 <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource"
        OnItemCreated="RadGrid1_ItemCreated">
        <MasterTableView DataKeyNames="ID">
            <Columns>
                <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn HeaderText="Allocate" UniqueName="Acc_Allocated" DataField="Acc_Allocated">
                    <ItemTemplate>
                        <telerik:RadNumericTextBox ID="TextGirdAmntAlloc" runat="server">
                            <ClientEvents OnValueChanged="ValueChanged" />
                        </telerik:RadNumericTextBox>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridEditCommandColumn>
                </telerik:GridEditCommandColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>