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