我们可以在 ASP.NET 中为 pagemethod 和 webmethod 使用相同的数据表吗? [英] Can we use same datatable for pagemethod and webmethod in ASP.NET?

查看:20
本文介绍了我们可以在 ASP.NET 中为 pagemethod 和 webmethod 使用相同的数据表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个新网页,我需要在其中显示近 10 个不同的网格视图和图表.

I am trying to create a new webpage where i need to display almost 10 different gridviews and charts.

Gridviews 绑定在 pageload 事件上,图表通过调用 WebMethod 使用 jquery-ajax 方法(使用 amcharts 和 highcharts)显示.

Gridviews are binded on pageload event and charts are displayed using jquery-ajax method (using amcharts as well as highcharts) by calling WebMethod.

最初我以这样一种方式实现了页面,即在为 gridviews(用于显示网格视图数据)和 webmethods(用于绘制图表)执行相同的一组存储过程之后.所以相同的 sps 为这个页面执行了两次(一个用于网格和另一个用于图表).需要执行 10 个 sps 才能获取数据.

Initially i implemented the page in a way that after executing same set of stored procedures for gridviews(for showing grid view data) and webmethods(for drawing charts).So same sps are executed twice for this page(one for grid and another for chart).There are 10 sps required to execute for fetching the data.

所以为了提高页面性能,我创建了这样的静态数据表

So for improving the page performance i have created static datatable like this

static DataTable Report1;

并像这样绑定gridview.

and binded the gridview like this.

private void gvbindReport1()
    {
        try
        {            
            Report1 = new DataTable();//refreshed datatable 
            DataSet ReportDS1 = objmvbl.GetReportGraph(ClientID, date_From, date_To);
            if (ReportDS1.Tables.Count > 0)
            {
                Report1 = ReportDS1.Tables[0];//bindinding data to static datatable

            }
            GdReport.DataSource = Report1;
            GdReport.DataBind();
        }
        catch (Exception ex)
        {
            Log.Errlog("Error Occured in  gvbindReport1 : " + ex.Message.ToString());
        }

    }

在 webmethod 中,我使用了相同的数据表来绘制图表像这样

and inside the webmethod i have used the same datatable for drawing the chart like this

 [System.Web.Services.WebMethod]
    public static string GetDataReport1()
    {
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
        Dictionary<string, object> row;
        try
        {
            //processing for the data inside static datatable
            if (Report1.Rows.Count > 0)
            {
                foreach (DataRow dr in Report1.Rows)
                {
                    row = new Dictionary<string, object>();
                    foreach (DataColumn col in Report1.Columns)
                    {
                        row.Add(col.ColumnName, dr[col]);
                    }
                    rows.Add(row);
                }
            }
        }
        catch (Exception ex)
        {
            Log.Errlog("Error Occured in  GetDataReport WebMethod of Report Page : " + ex.Message.ToString());
        }

        return serializer.Serialize(rows);

    }

有了这个,我可以同时显示网格和图表.

with this i am able to show both grid and charts.

现在请告诉我,这是处理 webmethods 的正确方法吗?我已经读过 webmethod 与页面无关.请告诉我这种方法的缺点.

Now Please tell me that, is this a correct approach to deal with webmethods? i have read that webmethod have no relation to the page and all.Please Tell me the drawbacks of this method.

如果这是错误的,请提出更好的方法来提高页面性能?

If this is wrong,Please suggest a better way to improve the page performance?

推荐答案

不,这不是正确的方法.由于您已将 DataTable 声明为 static(静态变量具有应用范围且无法实例化)所有

No, this is not the correct method. Since you have declared the DataTable as static (a static variable has application scope and cannot be instantiated) all

用户将得到相同的结果(最后更新的值).

users will get the same result (last updated values).

您可以在并发测试中实现这一点.

You can realize this in concurrency testing.

请检查以下场景:

考虑 dtbl 是在主页上初始化的静态 dataTable,并且您在索引页面上创建了另一个 `datatable 实例(两者都在页面加载中作为下面给出).

Consider dtbl is the static dataTable which is initialized on the home page, and you create another instance of `datatable on the index page (both are in page load as given below).

首页

public static DataTable dtbl;
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        dtbl = new DataTable();
        dtbl.Columns.Add("id");
        dtbl.Columns.Add("name");
        for (int i = 0; i < 10; i++)
        {
            DataRow dr = dtbl.NewRow();
            dr["id"] = i.ToString();
            dr["name"] = i + 1;
            dtbl.Rows.Add(dr);
        }
    }
}

索引页

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        home.dtbl = new DataTable();
    }
}

现在在每个页面加载和运行应用程序中放置一个断点,

Now put a breakpoint in each page load and run the application,

  • 单独的标签中打开两个页面.
  • 刷新主页并检查列是否显示
  • 现在转到下一个选项卡(索引)并刷新它(为 dt 创建一个新实例).它会影响数据表,现在您也可以在家中获得新的数据表.
  • 因此,如果这两个进程/页面同时执行,则两个页面都会获得最新值.这就是为什么我说它会在并发测试中实现这一点.

在这种情况下,您可以使用会话.考虑以下代码:

You can make use of a session in this case. Consider the following code:

首页

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        dtbl = new DataTable();
        dtbl.Columns.Add("id");
        dtbl.Columns.Add("name");
        for (int i = 0; i < 10; i++)
        {
            DataRow dr = dtbl.NewRow();
            dr["id"] = i.ToString();
            dr["name"] = i + 1;
            dtbl.Rows.Add(dr);
        }
        if (((DataTable)Session["MyDatatable"]).Columns.Count < 0)
        {
            Session["MyDatatable"] = dtbl;
        }
        else
        {
            dtbl = (DataTable)Session["MyDatatable"];
        }
    }
}

这篇关于我们可以在 ASP.NET 中为 pagemethod 和 webmethod 使用相同的数据表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆