using System;
using System.Text;
using System.Collections;
using System.Configuration;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Winson.EntityOP;
using Winson.EntityOP.Entity;
using Winson.Framework.Utility;

public partial class Data_select : System.Web.UI.Page
{
    Test test;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!String.IsNullOrEmpty(Request["Action"]))
            {
                switch (Request["Action"].ToLower())
                {
                    case "getgrid":
                        ShowGrid();
                        break;

                }
            }
        }

    }



    private void ShowGrid()
    {
        int totalpage = 0;
        int page = Convert.ToInt32(Request["page"]);
        int limit = Convert.ToInt32(Request["rows"]);
        string orderby = Request["sidx"].Split('|')[0];
        string criteria = Request["sidx"].Split('|')[1].Replace("+", " ");
        string order = Request["sord"];

        test = new Test();

        int totalCount = test.Count(criteria);

        List<TestDS> LTest = test.GetList(limit, page, criteria, orderby, order);

        NameValueCollection conditionValue = new NameValueCollection();
        conditionValue["ID"] = "1";

        TestDS t = test.GetEntity(conditionValue);
        // Test for order 
        //NameValueCollection cond = new NameValueCollection();
        //cond["order"] = " id desc";
        //List<TestDS> LTest = test.GetList(cond);

        if (totalCount > 0)
        {
            totalpage = totalCount / limit;
            if ((totalCount % limit) > 0) totalpage++;
        }
        else
            totalpage = 0;

        if (page > totalpage) page = totalpage;

        StringBuilder sb = new StringBuilder();
        sb.Append("<?xml version='1.0' encoding='utf-8'?>");
        sb.Append("<rows>");
        sb.Append("<page>" + page + "</page>");
        sb.Append("<total>" + totalpage + "</total>");
        sb.Append("<records>" + totalCount + "</records>");

        if (LTest.Count > 0)
        {
            foreach (TestDS row in LTest)
            {
                sb.Append("<row id='" + row.Id + "'>");
                sb.Append("<cell>" + row.Id + "</cell>");
                sb.Append("<cell>" + row.Name + "</cell>");
                sb.Append("<cell>" + row.Age + "</cell>");
                sb.Append("<cell>" + row.Datetime + "</cell>");
                sb.Append("<cell>" + row.Isuse + "</cell>");
                sb.Append("<cell><![CDATA[" + row.Context + "]]></cell>");
                sb.Append("</row>");
            }
        }
        sb.Append("</rows>");

        if (Response.ContentType == "application/xhtml+xml")
            Response.ContentType = "application/xhtml+xml;charset=utf-8";
        else
            Response.ContentType = "text/xml;charset=utf-8";

        ReturnResult(sb.ToString());
    }

    private void ReturnResult(string str)
    {
        Response.Write(str);//5+1+a+s+p+x
        Response.End();
    }
}
