Click here to Skip to main content
15,898,035 members
Articles / Web Development / XHTML

ClientCallback custom control for web applications

Rate me:
Please Sign up or sign in to vote.
4.91/5 (51 votes)
19 Dec 2008CPOL3 min read 100K   1K   103  
A client callback custom control and how to use it in WebForms.
using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class TreeViewCallbackDisplayGrid : System.Web.UI.Page
{
    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsCallback)
            return;

        if (!Page.IsPostBack)
        {
            PopulateTreeView();
            PopulateDummyGridView();
        }
    }  
    #endregion   

    #region TreeView Populate & Attach CallBack handler to it's nodes

    /// <summary>
    /// TreeView populated
    /// </summary>
    private void PopulateTreeView()
    {
        // One can load the tree here
        // Currently static nodes attached in the Designer itself.
        
        // Attach the callback handler
        AttachCallBackHandlerToNodes();
    }

    /// <summary>
    /// For attaching handler to the Nodes of the tree - Recursively
    /// </summary>
    private void AttachCallBackHandlerToNodes()
    {
        foreach (TreeNode tn in tvCallBack.Nodes)
        {
            AttachCallBack(tn);
        }
    }

    /// <summary>
    /// Handler's attached to the Nodes of the tree - Recursively
    /// </summary>
    /// <param name="tNode">tree view nodes</param>
    private void AttachCallBack(TreeNode tNode)
    {
        //Client Side callback event attached
        tNode.NavigateUrl = "javascript:OnNodeClick('" + tNode.Value + "');";
        if (tNode.ChildNodes.Count > 0)
        {
            foreach (TreeNode cNode in tNode.ChildNodes)
            {
                AttachCallBack(cNode);
            }
        }
    } 

    #endregion

    #region CallBack Event Handler
    // Bubbled event for Callback control placed
    // One can handle the operations required during the callback out here.
    protected void CallBackControl_Perform(object sender, EventArgs e)
    {
        string argParameter = ((MyCustomControls.MyCustomCallBackControl)sender).ArgumentParameter;
        DataTable dt = RetrieveDataTable(argParameter);
        gvTest.DataSource = dt;
        gvTest.DataBind();

        //Setting of the response output for callback
        using (System.IO.StringWriter sw = new System.IO.StringWriter())
        {
            gvTest.RenderControl(new HtmlTextWriter(sw));
            ((MyCustomControls.MyCustomCallBackControl)sender).RenderedOutput = sw.ToString();
        }
    } 
    #endregion

    #region GridView Operations & GridView Data Retrieval

    /// <summary>
    /// Dummy Grid View populated. One can populate the Root node related Data. 
    /// </summary>
    protected void PopulateDummyGridView()
    {
        DataTable dt = new DataTable("Table1");

        DataColumn dc1 = new DataColumn("ID");
        dt.Columns.Add(dc1);

        dt.Rows.Add("1");
        dt.Rows.Add("2");

        gvTest.DataSource = dt;
        gvTest.DataBind();
    }

    /// <summary>
    /// Retrieval of Test Data
    /// </summary>
    /// <param name="param">Parameter in order to retrieve the data</param>
    /// <returns></returns>
    private DataTable RetrieveDataTable(string param)
    {
        DataTable dt = new DataTable("Table1");

        DataColumn dc1 = new DataColumn("ID");
        DataColumn dc2 = new DataColumn("Caption");
        DataColumn dc3 = new DataColumn("Description");
        DataColumn dc4 = new DataColumn("Value");
        dt.Columns.Add(dc1);
        dt.Columns.Add(dc2);
        dt.Columns.Add(dc3);
        dt.Columns.Add(dc4);

        dt.Rows.Add(param, "Name" + param, "Who is responsible" + param, "NodeValue" + param);
        dt.Rows.Add(param, "Education" + param, "Detail about educational background" + param, "NodeValue" + param);
        dt.Rows.Add(param, "Experience" + param, "How much of professional experience" + param, "NodeValue" + param);
        dt.Rows.Add(param, "Address" + param, "Where do you stay?" + param, "NodeValue" + param);

        return dt;
    } 
    #endregion
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Intuit India
India India


A software professional for more than 17+ years building solutions for Web and Desktop applications.

Currently working at Intuit India.

Website: Learn By Insight
Github: Sandeep Mewara
LinkedIn: Sandeep Mewara

Strongly believe in learning and sharing knowledge.



Comments and Discussions