Gridview页脚文本框javascript调用两次插入后无法正常工作

时间:2014-07-16 11:10:09

标签: c# javascript asp.net gridview

我在页脚中有文本框,带有插入按钮。调用javascript计算文本框onchange()以获取值。

它适用于前两个插入。从第三个我得到这个错误。

“Microsoft JScript运行时错误:需要对象”

以下是代码:

 function calculate() {
    var Num1 = parseFloat(document.getElementById('<%=((TextBox)gvDowntime.FooterRow.FindControl("txtAddImpactedNoEmployee")).ClientID %>').value);
    var Num2 = parseInt(document.getElementById('<%=((TextBox)gvDowntime.FooterRow.FindControl("txtAddDowntimePerEmployee")).ClientID %>').value);
    var Num3 = Num1 * Num2;
    if (!isNaN(Num3)) {
        document.getElementById('<%=((TextBox)gvDowntime.FooterRow.FindControl("txtAddTotalDowntimeImpact")).ClientID %>').value = Num3;
    }

}



<asp:GridView ID="gvDowntime" runat="server" AutoGenerateColumns="False" ShowHeaderWhenEmpty="True"
EnableModelValidation="True" ShowFooter="True" Width="100%" 
CellPadding="4" ForeColor="#333333" GridLines="None" 
    onrowdatabound="gvDowntime_RowDataBound" 
    onrowcommand="gvDowntime_RowCommand">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />    
<Columns>
    <asp:TemplateField HeaderText="Downtime" FooterStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">            
        <ItemTemplate>
            <asp:Label ID="lblEntryID" runat="server" Visible="false"></asp:Label>
            <asp:Label ID="lblDowntime" runat="server"></asp:Label>
        </ItemTemplate>
        <FooterTemplate>
            <asp:DropDownList ID="ddlAddDowntime" runat="server"></asp:DropDownList>
        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="StartTime" FooterStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
        <ItemTemplate>
            <asp:Label ID="lblStartTime" runat="server"></asp:Label>
        </ItemTemplate>
        <FooterTemplate>
            <asp:DropDownList ID="ddlAddStartTime" runat="server"></asp:DropDownList>
        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="EndTime" FooterStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
        <ItemTemplate>
            <asp:Label ID="lblEndTime" runat="server"></asp:Label>                
        </ItemTemplate>
        <FooterTemplate>
            <asp:DropDownList ID="ddlAddEndTime" runat="server"></asp:DropDownList>
            <asp:Label ID="lblAddTimeValidation" runat="server" ForeColor="Red"></asp:Label>
        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Impacted # of<BR>Employees" FooterStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
        <ItemTemplate>
            <asp:Label ID="lblImpactedNoEmployee" runat="server"></asp:Label>
        </ItemTemplate>
        <FooterTemplate>

            <asp:TextBox ID="txtAddImpactedNoEmployee" runat="server" Width="100px" Font-Size="Smaller" onchange="calculate();"></asp:TextBox>

            <asp:RequiredFieldValidator ID="reqImpact" runat="server" Display="Dynamic" Text="*" 
            ControlToValidate="txtAddImpactedNoEmployee" ValidationGroup="NewEntry" ForeColor="Red"></asp:RequiredFieldValidator>

            <asp:RegularExpressionValidator runat="server" id="rexNumber" controltovalidate="txtAddImpactedNoEmployee" ValidationGroup="NewEntry" 
            validationexpression="[0-9]+(\.[0-9][0-9]?)?" errormessage="<BR>Please enter number!" ForeColor="Red" Display="Dynamic"/>

        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Downtime per<BR>Employee(Mins)" FooterStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
        <ItemTemplate>
            <asp:Label ID="lblDowntimePerEmployee" runat="server"></asp:Label>
        </ItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="txtAddDowntimePerEmployee" runat="server" Width="100px"  Font-Size="Smaller" onchange="calculate();"></asp:TextBox>

            <asp:RequiredFieldValidator ID="req" runat="server" Display="Dynamic" Text="*" 
            ControlToValidate="txtAddDowntimePerEmployee" ValidationGroup="NewEntry" ForeColor="Red"></asp:RequiredFieldValidator>

            <asp:RegularExpressionValidator runat="server" id="rex" controltovalidate="txtAddDowntimePerEmployee" ValidationGroup="NewEntry" 
            validationexpression="^[0-9]+$" errormessage="<BR>Please enter number!" ForeColor="Red" Display="Dynamic"/>

        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Total Downtime<BR>Impact(Mins)" FooterStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
        <ItemTemplate>
            <asp:Label ID="lblTotalDowntimeImpact" runat="server"></asp:Label>
        </ItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="txtAddTotalDowntimeImpact" runat="server" Width="100px" Font-Size="Smaller" ReadOnly="true"></asp:TextBox>
        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Production Units" FooterStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
        <ItemTemplate>
            <asp:Label ID="lblProductionUnits" runat="server" ></asp:Label>
        </ItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="txtAddProductionUnits" Width="100px" Font-Size="Smaller" runat="server"></asp:TextBox>  
             <asp:RequiredFieldValidator ID="reqDowntime" runat="server" Display="Dynamic" Text="*" 
            ControlToValidate="txtAddProductionUnits" ValidationGroup="NewEntry" ForeColor="Red"></asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator runat="server" id="rexProdUnit" controltovalidate="txtAddProductionUnits" ValidationGroup="NewEntry" 
            validationexpression="^[0-9]+$" errormessage="<BR>Please enter number!" ForeColor="Red" Display="Dynamic"/>              
        </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField FooterStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
        <ItemTemplate>
            <asp:LinkButton ID="btnDelete" runat="server" CommandName="DEL">Delete</asp:LinkButton>
        </ItemTemplate>
        <FooterTemplate>
            <asp:Button ID="btnInsert" runat="server" Text="Insert" ValidationGroup='NewEntry' CausesValidation='true' CommandName="insert" />
        </FooterTemplate>
    </asp:TemplateField>               
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" CssClass="gradientDemo" Height="35px"/>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />

protected void gvDowntime_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {                
            if (e.CommandName == "insert")
            {
                GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
                DropDownList ddlAddDowntime = (DropDownList)gvr.FindControl("ddlAddDowntime");
                DropDownList ddlAddStartTime = (DropDownList)gvr.FindControl("ddlAddStartTime");
                DropDownList ddlAddEndTime = (DropDownList)gvr.FindControl("ddlAddEndTime");
                TextBox txtAddImpactedNoEmployee = (TextBox)gvr.FindControl("txtAddImpactedNoEmployee");
                TextBox txtAddDowntimePerEmployee = (TextBox)gvr.FindControl("txtAddDowntimePerEmployee");
                TextBox txtAddTotalDowntimeImpact = (TextBox)gvr.FindControl("txtAddTotalDowntimeImpact");
                TextBox txtAddProductionUnits = (TextBox)gvr.FindControl("txtAddProductionUnits");
                Label lblAddTimeValidation = (Label)gvr.FindControl("lblAddTimeValidation");
                txtAddTotalDowntimeImpact.Text = Request.Form[txtAddTotalDowntimeImpact.UniqueID];
                DowntimeEntry objEntry = new DowntimeEntry();
                if (!BLL.DowntimeEntryDetail.isValidTimes(ddlAddStartTime.SelectedItem.Text, ddlAddEndTime.SelectedItem.Text))
                {
                    lblAddTimeValidation.Text = "EndTime should be greater!";
                    return;
                }
                objEntry.Downtime = ddlAddDowntime.SelectedItem.Text;
                objEntry.StartTime = ddlAddStartTime.SelectedItem.Text;
                objEntry.EndTime = ddlAddEndTime.SelectedItem.Text;
                objEntry.ImpactedNoEmployees = Convert.ToDouble(txtAddImpactedNoEmployee.Text);
                objEntry.DowntimePerEmployee = Convert.ToInt32(txtAddDowntimePerEmployee.Text);
                objEntry.TotalDowntimeImpact = Convert.ToDouble(txtAddTotalDowntimeImpact.Text);
                objEntry.ProductionUnit = Convert.ToInt32(txtAddProductionUnits.Text);
                objEntry.ResolutionAction = "";
                objEntry.ResolutionOwner = "";
                    objEntry.RequestID = Convert.ToInt32(Request.QueryString["ReqID"]);
                    BLL.DowntimeEntryDetail.UpdateDowntimeEntryRecord(objEntry);
                loadDowntimeEntry();

            }
        }
        catch (Exception ex)
        {
            BLL.DowntimeSummaryDetail.AddException(ex);
        }
    }

public void loadDowntimeEntry()         {             尝试             {                 bool blResolutionvisiblity = true;

                listDowntimeEntry = BLL.DowntimeEntryDetail.LoadDowntimeEntryDetail(Convert.ToInt32(Request.QueryString["ReqID"]));

            gvDowntime.DataSource = listDowntimeEntry;
            gvDowntime.DataBind();

            //if (listDowntimeEntry.FindAll(x => x.ResolutionAction == "" || x.ResolutionAction == null).Count > 0 && blResolutionvisiblity)
            //    lblHeader.Text = "Reporting & Resolution Pending";
            //else
            lblHeader.Text = "Reporting & Resolution";

            if (blResolutionvisiblity)
            {
                gvResolution.DataSource = listDowntimeEntry;
                gvResolution.DataBind();
            }
            //else
            //    panelHeader.Visible = false;
        }
        catch (Exception ex)
        {
            BLL.DowntimeSummaryDetail.AddException(ex);
        }
    }

protected void gvDowntime_RowDataBound(object sender,GridViewRowEventArgs e)         {             if(e.Row.RowType == DataControlRowType.DataRow)             {                 DowntimeEntry objEntry =(DowntimeEntry)e.Row.DataItem;                 if(objEntry.Downtime == null)                 {                     e.Row.Visible = false;                 }                 其他                 {                     标签lblDowntime =(标签)e.Row.FindControl(“lblDowntime”);                     标签lblStartTime =(标签)e.Row.FindControl(“lblStartTime”);                     标签lblEndTime =(标签)e.Row.FindControl(“lblEndTime”);                     标签lblImpactedNoEmployee =(Label)e.Row.FindControl(“lblImpactedNoEmployee”);                     标签lblDowntimePerEmployee =(Label)e.Row.FindControl(“lblDowntimePerEmployee”);                     标签lblTotalDowntimeImpact =(标签)e.Row.FindControl(“lblTotalDowntimeImpact”);                     标签lblProductionUnits =(Label)e.Row.FindControl(“lblProductionUnits”);                     标签lblEntryID =(标签)e.Row.FindControl(“lblEntryID”);

                lblEntryID.Text = Convert.ToString(objEntry.EntryID);
                lblDowntime.Text = objEntry.Downtime;
                lblStartTime.Text = objEntry.StartTime;
                lblEndTime.Text = objEntry.EndTime;
                lblImpactedNoEmployee.Text = Convert.ToString(objEntry.ImpactedNoEmployees);
                lblDowntimePerEmployee.Text = Convert.ToString(objEntry.DowntimePerEmployee);
                lblTotalDowntimeImpact.Text = Convert.ToString(objEntry.TotalDowntimeImpact);
                lblProductionUnits.Text = Convert.ToString(objEntry.ProductionUnit);

            }
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            DropDownList ddlAddDowntime = (DropDownList)e.Row.FindControl("ddlAddDowntime");
            DropDownList ddlAddStartTime = (DropDownList)e.Row.FindControl("ddlAddStartTime");
            DropDownList ddlAddEndTime = (DropDownList)e.Row.FindControl("ddlAddEndTime");
            TextBox txtAddImpactedNoEmployee = (TextBox)e.Row.FindControl("txtAddImpactedNoEmployee");
            TextBox txtAddDowntimePerEmployee = (TextBox)e.Row.FindControl("txtAddDowntimePerEmployee");
            TextBox txtAddTotalDowntimeImpact = (TextBox)e.Row.FindControl("txtAddTotalDowntimeImpact");
            TextBox txtAddProductionUnits = (TextBox)e.Row.FindControl("txtAddProductionUnits");

            List<DownTime> listDowntime = BLL.DowntimeDetail.LoadDowntimeDetail(true);
            foreach (DownTime objDowntime in listDowntime)
            {
                ListItem itmDowntime = new ListItem(objDowntime.DowntimeName, Convert.ToString(objDowntime.ID));
                itmDowntime.Attributes.Add("Title", objDowntime.Desc);
                ddlAddDowntime.Items.Add(itmDowntime);
            }
            List<TimeHour> listTime = BLL.TimeHourDetail.LoadTimeHourDetail(true);
            foreach (TimeHour objTime in listTime)
            {
                ddlAddStartTime.Items.Add(new ListItem(objTime.TimeHours, Convert.ToString(objTime.ID)));
                ddlAddEndTime.Items.Add(new ListItem(objTime.TimeHours, Convert.ToString(objTime.ID)));
            }
        }
    }

0 个答案:

没有答案