OnClientClick没有开火

时间:2018-05-25 04:02:29

标签: javascript asp.net

当我点击datagrid里面的按钮时,我的OnClientClick事件没有做任何事情,请帮我弄清楚我在这段代码中做错了什么

这是我的aspx

<asp:DataGrid ID="grid3" runat="server" AutoGenerateColumns="False"
                CellPadding="4" ForeColor="#333333" Width="100%" OnItemCommand="dtgItemCommand" 
                OnPageIndexChanged="grid1_PageIndexChanging" EnableViewState="true" ShowFooter="true"
                OnRowDataBound="GridView_RowDataBound">
                <AlternatingItemStyle CssClass="tdgenap" />
                <ItemStyle CssClass="tdganjil" HorizontalAlign="Center" />
                <HeaderStyle HorizontalAlign="Center" Height="30px" CssClass="tdjudul"></HeaderStyle>
                    <Columns>
                        <asp:TemplateColumn>
                            <HeaderStyle Font-Underline="false" Height="15px" Width="5%" HorizontalAlign="Center" BackColor="#ccffcc"></HeaderStyle>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                            <ItemTemplate>
                                <asp:CheckBox ID="cbTest" runat="server"  Enabled="true" AutoPostBack="true" />
                            </ItemTemplate>
                        </asp:TemplateColumn>
                        <asp:TemplateColumn HeaderText="Row" >
                            <HeaderStyle Font-Underline="false" Height="15px" Width="5%" HorizontalAlign="Center" BackColor="#ccffcc"></HeaderStyle>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                            <ItemTemplate>
                                <asp:Literal ID="litID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Row_")%>'></asp:Literal>
                                <%--<asp:Label id="lblID" runat="server" text='<%#DataBinder.Eval(Container.DataItem, "ID_")%>'></asp:Label>--%>
                            </ItemTemplate>
                        </asp:TemplateColumn>

                        <asp:TemplateColumn HeaderText="Price">
                            <HeaderStyle Font-Underline="false" Height="30px" HorizontalAlign="Center" BackColor="#ccffcc"></HeaderStyle>
                            <ItemStyle HorizontalAlign="Left"></ItemStyle>
                            <ItemTemplate>
                                <asp:TextBox ID="txtPrice" BorderWidth="0" onkeypress="CheckNumeric(event);" onkeyup="FormatCurrency(this);" runat="server" Width="95%" Text='<%#DataBinder.Eval(Container.DataItem, "Price_", "{0:N2}")%>'> </asp:TextBox>
                                <%--<asp:Label id="lblName" runat="server" text='<%#DataBinder.Eval(Container.DataItem,"Name_")%>'></asp:Label> --%>                                                                            
                            </ItemTemplate> 
                        </asp:TemplateColumn>

                        <asp:TemplateColumn HeaderText="Quantity">
                            <HeaderStyle Font-Underline="false" Height="30px" HorizontalAlign="Center" BackColor="#ccffcc"></HeaderStyle>
                            <ItemStyle HorizontalAlign="Left"></ItemStyle>
                            <ItemTemplate>
                                <asp:TextBox ID="txtQty" BorderWidth="0" onkeypress="CheckNumeric(event);" onkeyup="FormatCurrency(this);" runat="server" Width="95%" Text='<%#DataBinder.Eval(Container.DataItem, "Qty_", "{0:N2}")%>'> </asp:TextBox>
                                <%--<asp:Label id="lblPhone" runat="server" text='<%#DataBinder.Eval(Container.DataItem,"Phone_")%>'></asp:Label>--%>                                                                           
                            </ItemTemplate> 
                        </asp:TemplateColumn>

                        <asp:TemplateColumn>
                            <HeaderStyle Font-Underline="false" Height="30px" HorizontalAlign="Center" BackColor="#ccffcc"></HeaderStyle>
                            <ItemStyle HorizontalAlign="Left" Width="30px"></ItemStyle>
                            <ItemTemplate>      
                                <%--<asp:LinkButton ID="lnkCheck" runat="server" Text="Check Total" CommandName="Check"></asp:LinkButton>--%>                                   
                                <asp:ImageButton ID="btnCheck" runat="server" OnClientClick="javascript: return calculate();" ImageUrl="~/images/ButtonCheck.gif"/>
                            </ItemTemplate> 
                        </asp:TemplateColumn>

                        <asp:TemplateColumn HeaderText="Total">
                            <HeaderStyle Font-Underline="false" Height="30px" HorizontalAlign="Center" BackColor="#ccffcc"></HeaderStyle>
                            <ItemStyle HorizontalAlign="Left"></ItemStyle>
                            <ItemTemplate>
                                <asp:Label ID="lblTotal" BorderWidth="0" runat="server" Width="95%" Text='<%#DataBinder.Eval(Container.DataItem, "Total_")%>'> </asp:Label>
                                <%--<asp:Label id="lblPhone" runat="server" text='<%#DataBinder.Eval(Container.DataItem,"Phone_")%>'></asp:Label>--%>                                                                           
                            </ItemTemplate> 
                        </asp:TemplateColumn>
                    </Columns>
                </asp:DataGrid>

,这是我的javascript

function calculate() {
            var qty = parseFloat(document.getElementById("txtQty").value);
            var price = parseFloat(document.getElementById("txtPrice").value);

            alert("qty + price " +qty+" "+price);

            if (isNaN(qty) || isNaN(price)) {
                alert("Not a number");
            } else {
                var values = ggt(qty, price);
                console.log(values);

                if (values.valid) {
                    alert(values.multiply);
                    document.getElementById("txtTotal").innerHTML = values.multiply;
                }
            }
            return false;
        }

        function ggt(x, y) {
            var res = {};
            if (y == 0) {
                res.valid = false;
            } else {
                res.valid = true;
                res.number = x;
                res.multiply = x * y;
            }
            return res
        }

1 个答案:

答案 0 :(得分:0)

你能写吗?

.onWrite((change, context) => {

    if (!change.before.exists() || !change.after.exists()) {
        return null;
    }

    const beforeScore = parseFloat(change.before.val()['score']);
    const afterScore = parseFloat(change.after.val()['score']);
    const userRef = change.after.ref;

    var promises = [
        userRef.parent.parent.child('userCount').once('value'),
        userRef.parent.parent.child('averageScore').once('value')
    ];

    return userRef.transaction(() => {
        return Promise.all(promises).then((snapshots) => {
            const userCount = snapshots[0].val();
            const averageScore = snapshots[1].val();

            const currentAverage = (( ( averageScore * userCount ) - beforeScore + afterScore ) / userCount ).toFixed(2);

            return userRef.parent.parent.child('averageScore').set(currentAverage);
        });
    });

});

ASP.Net也会更改DataGrid中控件的ID。所以你不会得到你的document.getElementbyId行。

OnClientClick="return calculate();"

这些行不会返回任何内容。