Click here to Skip to main content
15,867,686 members
Articles / Web Development / ASP.NET

How to Pass Value from Ajax Enabled Base User Control to Client User Control and to the Parent ASPX Page

Rate me:
Please Sign up or sign in to vote.
2.69/5 (6 votes)
13 Mar 2008CPOL 29.6K   21   1
How to pass value from Ajax Enabled Base User Control to Client User Control and to the Parent aspx page

Introduction

What this project is all about is building an Ajax enabled dialog type Base User Control. When the button is clicked, it pops up a GridView Popup User Control, from where the user selects some values and the selected values are sent back to the Client page and then to the Parent ASPX page. Data can be sent in the form of DataSet to the client page and from client page to the Parent page.

Background

Please read how to pass a value between user controls:

Using the Code

  1. Setup - create a Base user Control like this.

    Image 1

  2. Create a Client User control page drag and drop base control on to the client user control page. Client page can inherit the Base user control Client user control: Base user control
    C#
    //Base Control Codes    
    //Define the Delegate and Event
        public delegate void Ep_UserControlCommandEventHandler
    	(object sender, AB_UserControlCommandEventArgs e);
        public event AB_UserControlCommandEventHandler AjaxGridSelectedData;
    //User can set the DataSet from the Client User Control user can 
    //pass a data for the GridView of the Base control from Client User control;
    //Define the DataSet property on base control
        private DataSet data;
        public DataSet Parent_DataSet
        {
            set
            {
                data = value; ;
            }
        }
    
    //When user clicks on the show dialog button. This is base control event
    protected void Show_Click(object sender, EventArgs e)
        {
            GridView1.DataSource = data; 
            GridView1.DataBind();
            BPanel.Update();
            mdlPopup.Show();
        }
    
    //Define the virtual method this method needs to implement 
    //in the client page to access the return data set value
      protected virtual void OnAB_AjaxGridSelectedData(AB_UserControlCommandEventArgs e)
        {
            if (AjaxGridSelectedData != null) AjaxGridSelectedData(this, e);
        }
    
    //Pass the Selected values to the Client Page by Base Command event
    AB_UserControlCommandEventArgs(N_data));
  3. After selecting the values from the control, the user can press the add button to return the selected values as dataset.
    C#
    protected void btnadd_Click(object sender, EventArgs e)
    {
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("Id", typeof(string)));
    dt.Columns.Add(new DataColumn("Name", typeof(string)));
    dt.Columns.Add(new DataColumn("Is_Selected", typeof(Boolean)));
    ArrayList Id_List = (ArrayList)Session["CHECKED_ITEMS"];
    foreach (GridViewRow Ro in GridView1.Rows)
    {
    bool isChecked = ((CheckBox)Ro.FindControl("Is_Selected")).Checked;
    if (isChecked)
    {
    if (Id_List != null)
    {
    Id_List.Add(Ro.Cells[0].Text);
    }
    else
    {
    DataRow dra = dt.NewRow();
    dra["Id"] = Ro.Cells[0].Text;
    dra["Name"] = Ro.Cells[1].Text;
    dra["Is_Selected"] = true;
    dt.Rows.Add(dra);
    }
    }
    }
    if(Id_List!=null)
    {
    string Id_s ="'";
    for (int i = 0; i < Id_List.Count; i++)
    {
    Id_s += Id_List[i].ToString() +"','";
    }
    DataTable MyTable = data.Tables[0];
    DataRow[] test;
    string Slect = "Id in (" + Id_s + ")";
    Slect=Slect.Replace(",')", ")");
    test = MyTable.Select(Slect);
    foreach (DataRow rw in test)
    {
    DataRow dr = dt.NewRow();
    dr["Id"]=rw["Id"];
    dr["Name"]=rw["Name"];
    dr["Is_Selected"]=true;
    dt.Rows.Add(dr);
    }
    }
    DataSet N_data = new DataSet();
    N_data.Tables.Add(dt);
    OnEp_AjaxGridSelectedData(new AB_UserControlCommandEventArgs(N_data));
    mdlPopup.Hide();
    }
          
    //Client User control page implements the base control event
    protected void Page_Load(object sender, EventArgs e)
    {
    AjaxGridViewBaseControl1.Parent_DataSet = Main_GridData();
    AjaxGridViewBaseControl1.AjaxGridSelectedData += 
    	new AB_UserControlCommandEventHandler(OnAjaxGridSelectedData);
    }
    //Get value from Base
    private void OnAjaxGridSelectedData(object sender, AB_UserControlCommandEventArgs e)
    {
    //The selected values can be displayed in client control 
    //and even return back to parent page
    GridView1.DataSource = e.EpDataSet;
    GridView1.DataBind();
    }
  4. Drag and drop the client page on the parent page.
  5. This is an example of how the data is displayed in the POPup Grid

uc2.jpg

History

  • 13th March, 2008: Initial post

License

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


Written By
Chief Technology Officer Compuacademy.net
United States United States
More than 15 years of experience in design, architecture and development of various commercial objects oriented application.Other Specialties

Data Migration:
• MS Access database to SQL 2005/2008
• MS Access database to Oracle
• MS Access database to My SQL
• FoxPro to SQL

Application Migration:
• Converted MS Access application to .net web application (Asp.net)
• Excel Application to .net 3.5 web application
• FoxPro application to .net 3.5
Reporting development and support
• MS access reports
• Crystal reports
• SQL Reports(SSRS)
• DevExpress reports
• Cognos reports
Application development and support
• .net Application web /Win forms
• SharePoint
• MS Access
• Website
• Ecommerce
• WCF
• Web Services
3rd Party Control Support
• DevExpress
• .netForum
• Telerik
Version controls Support
• Team Foundation Server
• Source Safe
• CVS
• SVN

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey16-Feb-12 0:30
professionalManoj Kumar Choubey16-Feb-12 0:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.