使用Javascript在RadGrid编辑模板中进行访问控制

时间:2015-10-21 06:39:01

标签: javascript jquery asp.net telerik radgrid

我在RadGrid编辑表单中有三个控件 我的目标是将textbox1和textbox2相乘,并使用javascript

在TextBox3上显示结果
 function multiply() {
                         var TextBox1 = document.getElementById('TextBox1').value;
                         var TextBox2 = document.getElementById('TextBox2').value;
                         var result = parseInt(TextBox1) * parseInt(TextBox2);
                         if (!isNaN(result)) {
                             document.getElementById('TextBox3').value = result;
                         }
                     }

当我将代码用于控件外侧radgrid或radgrid编辑模板时,代码工作正常,但如何在RadGRid编辑表单上实现相同的代码

1 个答案:

答案 0 :(得分:0)

如果没有弄错,这就是你想要的?
是在EditMode =“EditForms”

<asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
    <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" AutoGenerateColumns="false" OnItemCommand="RadGrid1_ItemCommand">
        <MasterTableView EditMode="EditForms">
            <EditFormSettings>
                <EditColumn Visible="false"></EditColumn>
            </EditFormSettings>
            <Columns>
                <telerik:GridTemplateColumn>
                    <ItemTemplate><asp:Label ID="lbl" runat="server" Text='<%# Eval("A") %>'></asp:Label></ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txt1" runat="server" onkeyup="calculate();"></asp:TextBox> * 
                        <asp:TextBox ID="txt2" runat="server" onkeyup="calculate();"></asp:TextBox> =
                        <asp:TextBox ID="txt3" runat="server" Text='<%# Eval("A") %>'></asp:TextBox>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn>
                    <ItemTemplate><asp:LinkButton ID="lbtnEdit" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton></ItemTemplate>
                    <EditItemTemplate>
                        <asp:LinkButton ID="lbtnSave" runat="server" Text="Save" CommandName="SaveValue"></asp:LinkButton>
                        &nbsp;
                        <asp:LinkButton ID="lbtnCancel" runat="server" Text="Cancel" CommandName="Cancel"></asp:LinkButton>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    <telerik:RadCodeBlock ID="rcb" runat="server">
        <script type="text/javascript">

            function calculate() {
                var grid = $find('<%=RadGrid1.ClientID %>');
                var masterTable = grid.get_masterTableView();
                var gridCount = masterTable.get_editItems().length;
                var item;
                var txt1;
                var txt2;
                var txt3;
                var num1 = 0;
                var num2 = 0;
                var result = 0;

                for (var i = 0; i < gridCount; i++) {
                    item = masterTable.get_editItems()[i];
                    txt1 = $(item.get_editFormItem()).find("input[id*='txt1']").get(0);
                    txt2 = $(item.get_editFormItem()).find("input[id*='txt2']").get(0);
                    txt3 = $(item.get_editFormItem()).find("span[id*='txt3']").get(0);

                    // Reset
                    num1 = 0;
                    num2 = 0;
                    result = 0;

                    // Check
                    if (txt1 != undefined && txt2 != undefined && lblResult != undefined) {
                        num1 = txt1.value != '' ? parseInt(txt1.value) : 0;
                        num2 = txt2.value != '' ? parseInt(txt2.value) : 0;
                        result = num1 * num2;

                        txt3.value = result + "";
                    }
                }
            }
        </script>
    </telerik:RadCodeBlock>