KeyPress event for RadNumeric Textbox in Radgrid for exchanging coloumn values

半腔热情 提交于 2019-12-11 14:04:34

问题


I am developing a website in C# using a RadGrid in one of my forms. Below is my ASPX code for the RadGrid:

<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>

In this grid I want to transfer the exact value of coloumn AMT to textbox Acc_Allocated when I click or KeyPress or with any other event. There is one condition for transfering the values: when Acc_Source value is "3" then transfer that value, otherwise do not. Currently I am checking from server side via the TextChanged event.

Is there any other way of doing this?

Here is my TextChanged event:

   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:


Please try with below code snippet.

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>


来源:https://stackoverflow.com/questions/12584150/keypress-event-for-radnumeric-textbox-in-radgrid-for-exchanging-coloumn-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!