using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Web.UI;
using Telerik.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Reflection;
using System.Threading;


public class ViewManager
{
    public static string RenderView(string path)
    {
        return RenderView(path, null);
    }

    public static string RenderView(string path, object data)
    {
        Page pageHolder = new Page();
        UserControl viewControl = (UserControl)pageHolder.LoadControl(path);

        if (data != null)
        {
            Type viewControlType = viewControl.GetType();
            FieldInfo field = viewControlType.GetField("Data");

            if (field != null)
            {
                field.SetValue(viewControl, data);
            }
            else
            {
                throw new Exception("View file: " + path + " does not have a public Data property");
            }
        }

        pageHolder.Controls.Add(viewControl);

        StringWriter output = new StringWriter();
        HttpContext.Current.Server.Execute(pageHolder, output, false);

        return output.ToString();
    }
}


[ScriptService]
public class ToolTipWebService : System.Web.Services.WebService
{

    [WebMethod]
    public string GetToolTipData(object context)
    {
        IDictionary<string, object> contextDictionary = (IDictionary<string, object>)context;
        string elementID = ((string)contextDictionary["Value"]);

        if (elementID == string.Empty)
        {
            throw new Exception("No Value argument is provided to the webservice!");
        }

        DataTable information = new DataTable();

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
        try
        {
            conn.Open();

            SqlDataAdapter adapter = new SqlDataAdapter();

            try
            {
                adapter.SelectCommand = new SqlCommand("SELECT * FROM [Customers] WHERE CustomerID=@id", conn);
                adapter.SelectCommand.Parameters.AddWithValue("@id", elementID);
                adapter.Fill(information);
            }
            finally
            {
                if (!Object.Equals(adapter.SelectCommand, null)) adapter.SelectCommand.Dispose();
            }
        }
        finally
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
        }

        DataRow row = information.Rows[0];

        return ViewManager.RenderView("~/CustomerInfo.ascx", information);
    }


}


