0

问题:如何修改UpdateColumns()方法(如下)或OnTextChanged事件调用以仅使用触发事件的行?

现在,我正在通过UpdateColumns()使用事件调用方法来更新 GridView 单元格值,OnTextChanged如下所示:

<asp:TemplateField HeaderStyle-Width="35px">
    <ItemTemplate>
        <asp:TextBox OnTextChanged="UpdateColumns" ID="txtJan" CssClass="row"
        runat="server" AutoPostBack="True" Text='<%# Eval("Jan","{0:#0}") %>'>
        </asp:TextBox> 
    </ItemTemplate>
    <HeaderTemplate>
        Jan
    </HeaderTemplate>
    <FooterTemplate>
        <asp:TextBox ID="txtJan" CssClass="row" runat="server"></asp:TextBox>
    </FooterTemplate>
</asp:TemplateField>

下面是事件触发的UpdateColumns()方法。OnTextChanged它遍历表中的每一行并进行计算。我的 GridView 中有 15 列和 10 行,因此该操作需要大约 1 秒才能完成。如何修改我的方法以仅查看调用它的行?本质上,我只是想让操作执行得更快,而不是做无用的计算。

Protected Sub UpdateColumns(ByVal sender As Object, ByVal e As EventArgs)
    Dim errors As New StringBuilder

    Try
        For Each row As GridViewRow In gvBuildingBlocks.Rows

            _tjan = CType(row.FindControl("txtJan"), TextBox)
            _tfeb = CType(row.FindControl("txtFeb"), TextBox)
            _tmar = CType(row.FindControl("txtMar"), TextBox)
            _tapr = CType(row.FindControl("txtApr"), TextBox)
            _tmay = CType(row.FindControl("txtMay"), TextBox)
            _tjun = CType(row.FindControl("txtJun"), TextBox)
            _tjul = CType(row.FindControl("txtJul"), TextBox)
            _taug = CType(row.FindControl("txtAug"), TextBox)
            _tsep = CType(row.FindControl("txtSep"), TextBox)
            _toct = CType(row.FindControl("txtOct"), TextBox)
            _tnov = CType(row.FindControl("txtNov"), TextBox)
            _tdec = CType(row.FindControl("txtDec"), TextBox)
            _tq1 = CType(row.FindControl("txtQ1"), TextBox)
            _tq2 = CType(row.FindControl("txtQ2"), TextBox)
            _tq3 = CType(row.FindControl("txtQ3"), TextBox)
            _tq4 = CType(row.FindControl("txtQ4"), TextBox)
            _th1 = CType(row.FindControl("txtH1"), TextBox)
            _th2 = CType(row.FindControl("txtH2"), TextBox)
            _tt = CType(row.FindControl("txtYear"), TextBox)

            If IsNumeric(_tjan.Text) And IsNumeric(_tfeb.Text) _
            And IsNumeric(_tmar.Text) And IsNumeric(_tapr.Text) _
            And IsNumeric(_tmay.Text) And IsNumeric(_tjun.Text) _
            And IsNumeric(_tjul.Text) And IsNumeric(_taug.Text) _
            And IsNumeric(_tsep.Text) And IsNumeric(_toct.Text) _
            And IsNumeric(_tnov.Text) And IsNumeric(_tdec.Text) Then
                _bbjan = Decimal.Parse(_tjan.Text)
                _bbfeb = Decimal.Parse(_tfeb.Text)
                _bbmar = Decimal.Parse(_tmar.Text)
                _bbapr = Decimal.Parse(_tapr.Text)
                _bbmay = Decimal.Parse(_tmay.Text)
                _bbjun = Decimal.Parse(_tjun.Text)
                _bbjul = Decimal.Parse(_tjul.Text)
                _bbaug = Decimal.Parse(_taug.Text)
                _bbsep = Decimal.Parse(_tsep.Text)
                _bboct = Decimal.Parse(_toct.Text)
                _bbnov = Decimal.Parse(_tnov.Text)
                _bbdec = Decimal.Parse(_tdec.Text)
                _bbq1 = _bbjan + _bbfeb + _bbmar
                _bbq2 = _bbapr + _bbmay + _bbjun
                _bbq3 = _bbjul + _bbaug + _bbsep
                _bbq4 = _bboct + _bbnov + _bbdec
                _bbh1 = _bbq1 + _bbq2
                _bbh2 = _bbq3 + _bbq4
                _bbtt = _bbh1 + _bbh2

                _tq1.Text = _bbq1.ToString()
                _tq2.Text = _bbq2.ToString()
                _tq3.Text = _bbq3.ToString()
                _tq4.Text = _bbq4.ToString()
                _th1.Text = _bbh1.ToString()
                _th2.Text = _bbh2.ToString()
                _tt.Text = _bbtt.ToString()

                _totalJan += _bbjan
                _totalFeb += _bbfeb
                _totalMar += _bbmar
                _totalApr += _bbapr
                _totalMay += _bbmay
                _totalJun += _bbjun
                _totalJul += _bbjul
                _totalAug += _bbaug
                _totalSep += _bbsep
                _totalOct += _bboct
                _totalNov += _bbnov
                _totalDec += _bbdec
                _totalQ1 += _bbq1
                _totalQ2 += _bbq2
                _totalQ3 += _bbq3
                _totalQ4 += _bbq4
                _totalH1 += _bbh1
                _totalH2 += _bbh2
                _totalT += _bbtt
            Else
                errors.Append("Error!  Input string must be numeric.  Please correct error(s).")
            End If
        Next

        If errors.Length > 0 Then
            'gvBuildingBlocks.DataSource = CType(Session("buildingBlocks"), DataTable)
            'gvBuildingBlocks.DataBind()
            ShowMessage(errors.ToString)
        End If

        _tjan = CType(gvBuildingBlocks.FooterRow.FindControl("txtJan"), TextBox)
        _tfeb = CType(gvBuildingBlocks.FooterRow.FindControl("txtFeb"), TextBox)
        _tmar = CType(gvBuildingBlocks.FooterRow.FindControl("txtMar"), TextBox)
        _tapr = CType(gvBuildingBlocks.FooterRow.FindControl("txtApr"), TextBox)
        _tmay = CType(gvBuildingBlocks.FooterRow.FindControl("txtMay"), TextBox)
        _tjun = CType(gvBuildingBlocks.FooterRow.FindControl("txtJun"), TextBox)
        _tjul = CType(gvBuildingBlocks.FooterRow.FindControl("txtJul"), TextBox)
        _taug = CType(gvBuildingBlocks.FooterRow.FindControl("txtAug"), TextBox)
        _tsep = CType(gvBuildingBlocks.FooterRow.FindControl("txtSep"), TextBox)
        _toct = CType(gvBuildingBlocks.FooterRow.FindControl("txtOct"), TextBox)
        _tnov = CType(gvBuildingBlocks.FooterRow.FindControl("txtNov"), TextBox)
        _tdec = CType(gvBuildingBlocks.FooterRow.FindControl("txtDec"), TextBox)
        _tq1 = CType(gvBuildingBlocks.FooterRow.FindControl("txtQ1"), TextBox)
        _tq2 = CType(gvBuildingBlocks.FooterRow.FindControl("txtQ2"), TextBox)
        _tq3 = CType(gvBuildingBlocks.FooterRow.FindControl("txtQ3"), TextBox)
        _tq4 = CType(gvBuildingBlocks.FooterRow.FindControl("txtQ4"), TextBox)
        _th1 = CType(gvBuildingBlocks.FooterRow.FindControl("txtH1"), TextBox)
        _th2 = CType(gvBuildingBlocks.FooterRow.FindControl("txtH2"), TextBox)
        _tt = CType(gvBuildingBlocks.FooterRow.FindControl("txtYear"), TextBox)

        _tjan.Text = _totalJan.ToString()
        _tfeb.Text = _totalFeb.ToString()
        _tmar.Text = _totalMar.ToString()
        _tapr.Text = _totalApr.ToString()
        _tmay.Text = _totalMay.ToString()
        _tjun.Text = _totalJun.ToString()
        _tjul.Text = _totalJul.ToString()
        _taug.Text = _totalAug.ToString()
        _tsep.Text = _totalSep.ToString()
        _toct.Text = _totalOct.ToString()
        _tnov.Text = _totalNov.ToString()
        _tdec.Text = _totalDec.ToString()
        _tq1.Text = _totalQ1.ToString()
        _tq2.Text = _totalQ2.ToString()
        _tq3.Text = _totalQ3.ToString()
        _tq4.Text = _totalQ4.ToString()
        _th1.Text = _totalH1.ToString()
        _th2.Text = _totalH2.ToString()
        _tt.Text = _totalT.ToString()

    Catch ex As Exception
        ShowError("Exception: ", ex.ToString())
    End Try
End Sub
4

1 回答 1

2

“sender”参数应该包含触发事件的控件。您应该能够根据您的发件人找出发生了什么变化,并且只对发生变化的内容进行重做计算。

于 2013-02-18T20:41:29.220 回答