使用相同的数据表在asp.net PageMethod的和的WebMethod? [英] Using same datatable for pagemethod and webmethod in asp.net?

查看:119
本文介绍了使用相同的数据表在asp.net PageMethod的和的WebMethod?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个新网页,我需要显示近10种不同的每个网格数据GridView的和图表。

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

GridView的被绑定在页面加载事件和图表使用jQuery的Ajax方法(使用amcharts以及highcharts)调用的WebMethod显示。

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

起初我实现了网页的方式,执行存储后的过程gridview的绑定和里面的WebMethod还执行绘制chart.So同一个SP在同一SP在此页面执行两次(一个用于电网,另一个用于图表)。10 SPS需要执行网格和图表相同的10。

Initially i implemented the page in a way that after executing stored procedure gridview binded and inside webmethod also executing the same sp for drawing chart.So same sp is executed twice for this page(one for grid and another for chart).10 sps needs to execute for grid and same 10 for charts.

因此,对于提高页面的表现我已经创建了静态数据表这样的

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没有关系页面和all.Please告诉我这种方法的抽奖背上。

Now Please tell 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 draw backs of this method.

如果这是错误的方法,请给一些想法提高页面的表现?

If this is wrong way,Please give some idea to improve the page performance?

推荐答案

没有,这不是正确的方法。既然你已经宣布了数据表静态(静态变量具有应用范围和不能被实例化)所有

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).

您可以实现这并发测试。

请查看以下情形:

考虑 DTBL 是它在主页上初始化静态的dataTable ,与您共创DataTable的`索引页面上的另一个实例,实例(无论是页面加载下面给出)。

Consider dtbl is the static dataTable which is initialized on the home page, and you create another instance 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创建新实例)。它会影响数据表,现在你会得到在家里也是新的DataTable。

  • 因此,如果这两个过程/页并发执行的最新值将获得两个页面。这就是为什么我说这将在并发测试意识到这一点。

  • Open both the pages in separate tab.
  • Refresh the home page and check whether the columns are showing
  • Now go to next tab (index) and refresh it (a new instance is created for dt). It will affect the datatable now you will get the new datatable in home also.
  • So if these two processes/pages are concurrently executed the latest value will get for both the pages. That's why I am saying it will realize this in concurrency testing.

您可以使用一个会话在这种情况下。考虑下面的代码:

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天全站免登陆