在没有数据库交互的情况下绑定gridview时如何解决性能问题?

时间:2019-04-07 16:46:10

标签: c# asp.net

我正在将一些数据添加到gridview中,而无需数据库交互。在将数据添加到gridview时,片刻看起来还不错。添加20多行之后,它花费的时间比预期的要多。当然,完成添加过程需要几秒钟。如果有人帮我找出解释的问题,将会很有帮助。

我正在使用Viewstate来存储数据。在寻找解决方案之后。我了解了ViewState的一些缺点,因此回到了Session来存储数据。但这也无济于事。

这是aspx页面

<asp:GridView ID="gvPayFrmAccount" runat="server" Width="100%" CssClass="table table-striped table-bordered table-condensed"
RowStyle-CssClass="zero" AutoGenerateColumns="false" OnRowDataBound="GvPayFrmAccount_RowDataBound" OnRowDeleting="GvPayFrmAccountOnRowDeleting">
    <HeaderStyle CssClass="headerStyle"></HeaderStyle>
    <RowStyle CssClass="rowStyle"></RowStyle>
    <Columns>
        <asp:BoundField DataField="CrAccCode" ItemStyle-HorizontalAlign="Center" HeaderText="Account Code" />
        <asp:BoundField DataField="CrAccAccount" ItemStyle-HorizontalAlign="Center" HeaderText="Allocate Account" />
        <asp:BoundField DataField="CrAmount" ItemStyle-HorizontalAlign="Center" HeaderText="Amount"></asp:BoundField>
        <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Delete">
        <ItemTemplate>
        <asp:Button ID="ButtonDelete" runat="server" CommandName="Delete" Text="X" />
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

这是后面的代码====================

private void BindPayfrmAcc()
{
    DataRow dr1;
    DataColumn CrAccCode, CrAccAccount, CrAmount;
    CrAccCode = new DataColumn("CrAccCode", Type.GetType("System.String"));
    CrAccAccount = new DataColumn("CrAccAccount", Type.GetType("System.String"));
    CrAmount = new DataColumn("CrAmount", Type.GetType("System.String"));
    Current.Columns.Add(CrAccCode);
    Current.Columns.Add(CrAccAccount);
    Current.Columns.Add(CrAmount);
    int temp = 0;
    txtCrAmount.Text = Math.Round(Convert.ToDouble(txtCrAmount.Text), 4).ToString(".0000");
    if (btnCrSave.Text == "Add")
    {
        if (Session["Current"] != null)
        {
            Current = (DataTable)Session["Current"];
            for (int j = 0; j < Current.Rows.Count; j++)
            {
                if (Current.Rows[j]["CrAccCode"].ToString() == txtPaidAccCode.Text)
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "Save",
                    "alert('This Charge Head has already been added...');", true);
                    temp++;
                    break;
                }
            }
        if (temp == 0)
        {
            dr1 = Current.NewRow();
            dr1[0] = txtPaidAccCode.Text;
            dr1[1] = ddlPfrmAcc.SelectedItem.Text;
            dr1[2] = txtCrAmount.Text;
            Current.Rows.Add(dr1);
            double x = Math.Truncate(Convert.ToDouble(txtCrAmount.Text) * 10000) / 10000;
            lblCrTotalAmount.InnerText = (Convert.ToDouble(lblCrTotalAmount.InnerText) + x).ToString();
        }
    }
    else
    {
        dr1 = Current.NewRow();
        dr1[0] = txtPaidAccCode.Text;
        dr1[1] = ddlPfrmAcc.SelectedItem.Text;
        dr1[2] = txtCrAmount.Text;
        Current.Rows.Add(dr1);
        lblCrTotalAmount.InnerText = "0";
        lblCrTotalAmount.InnerText = txtCrAmount.Text;
        double x = Math.Truncate(Convert.ToDouble(txtCrAmount.Text) * 10000) / 10000;
        lblCrTotalAmount.InnerText = x.ToString();
        lblCrTotalAmount.InnerText = (Math.Truncate(Convert.ToDouble(lblCrTotalAmount.InnerText) * 10000) / 10000).ToString();
    }
}
if (Session["Current"] != null)
{
    gvPayFrmAccount.DataSource = Current;
    gvPayFrmAccount.DataBind();
    if (btnSave.Text == "Save")
    ClearData();
}
else
{
    gvPayFrmAccount.DataSource = Current;
    gvPayFrmAccount.DataBind();
    if (btnSave.Text == "Save")
    ClearData();
}
    Session["Current"] = Current;
}

1 个答案:

答案 0 :(得分:0)

ViewState是臭名昭著的资源消耗者时,

SessionInProc。您可以通过在web.configsystem.web部分)中添加以下内容来提高性能

<sessionState mode="StateServer" stateConnectionString="tcpip=localhost:42424" timeout="60"/>

然后确保 ASP.NET状态服务aspnet_state)正在运行

并覆盖页面上的PageStatePersister。 (这是VB.NET。希望您可以将其翻译为C#。)

Private _persister As PageStatePersister = Nothing
Protected Overrides ReadOnly Property PageStatePersister As PageStatePersister
    Get
        If _persister Is Nothing Then
            _persister = New SessionPageStatePersister(Me)
        End If
        Return _persister
    End Get
End Property

然后将您的DataTable放在ViewState中。