Click here to Skip to main content
15,879,239 members
Articles / Web Development / IIS

How To Pass Value from User Web Control to Parent ASPX Page

Rate me:
Please Sign up or sign in to vote.
2.53/5 (6 votes)
23 Feb 2008CPOL 86.1K   35   7
How to pass values between user control and ASPX page without JavaScript

Introduction

I was working on a user control where I was trying to pass values from the pop user control to the parent page. It was very easy to use JavaScript and pass the value to the parent page, but it was not easy to find a way to pass value to the parent page without JavaScript. I did find a way to do this and want other developers to get some idea.

Background

Pass value between the user control and ASPX page without the help of JavaScript.

Using the Code

  1. Create a web project
  2. Add a user control to the project
  3. Next, see the code block
  4. Add these lines of code:
    C#
    private DataSet data;
    // declare a delegate
    public delegate void U_ListCommandEventHandler
    		(object sender, U_ListCommandEventArgs e);
    //Event
        public event U_ListCommandEventHandler UGridViewChanged;
    //selected index property
      public int SelectedIndex
        {
            get { return UGridView.SelectedIndex; }
            set
            {
                if (!Page.IsPostBack)
                {
                    GVData_Bind();
                }
                if (value < UGridView.Rows.Count)
                {
                    UGridView.SelectedIndex = value;
                    data = U_DataSet
    		(UGridView.Rows[UGridView.SelectedIndex].Cells[1].Text);
                    OnUGridViewChanged(new U_ListCommandEventArgs(data));
                }
            }
        }
    //Method to load data in to dataset
     private DataSet GVData_Bind()
        {
            SqlConnection Con = new SqlConnection(Cn);
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand("SELECT * FROM Person", Con);
            data = new DataSet();
            adapter.Fill(data, "U_Table");
            return data;
        }
     protected void Page_Load(object sender, EventArgs e)
        {
          // load data to the grid view
        if (!IsPostBack)
            {
            data= GVData_Bind();
            UGridView.DataSource = data;
            UGridView.DataBind();
        }
    }
    //Method to return DataSet base on id
      private DataSet U_DataSet(string id)
        {
            SqlConnection Con = new SqlConnection(Cn);
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand
    		("SELECT * FROM Person where Id='" + id + "'", Con);
            data = new DataSet();
            adapter.Fill(data, "U_Table");
            return data;
        }
    //method for selected index
     protected void UGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            data = U_DataSet(UGridView.Rows[UGridView.SelectedIndex].Cells[1].Text);
            OnUGridViewChanged(new U_ListCommandEventArgs(data));
    
        }
    //virtual method
      protected virtual void OnUGridViewChanged(U_ListCommandEventArgs e)
        {
            if (UGridViewChanged != null) UGridViewChanged(this, e);
        }
    // Add this class to the project
    public class U_ListCommandEventArgs
    {
        private DataSet _U_DataSet;
    
    	public U_ListCommandEventArgs(DataSet U_DataSet)
    	{
            _U_DataSet = U_DataSet;
    
    	}
        public DataSet U_DataSet { get { return _U_DataSet; } }
    
    }
    //also Add this
    public class U_ListCommandEventHandler
    {
    	public U_ListCommandEventHandler()
    	{
    		
    	}
    }
  5. Drag and drop the newly built user control on the ASPX page.
  6. Add or update the OnInit() method
    C#
    protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            GridViewUsercontrol1.UGridViewChanged+=
    		new GridViewUsercontrol.U_ListCommandEventHandler
    		(GridViewUsercontrol1_UGridViewChanged);        
        }
    //since we are getting dataset back from user control 
    //we can bind it to the gridview.
    private void GridViewUsercontrol1_UGridViewChanged
    		(object sender, U_ListCommandEventArgs e)
        {
            GridView1.DataSource = e.U_DataSet;
            GridView1.DataBind();
        }

History

  • 23rd February, 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 1 Pin
rjb227s7-May-09 13:20
rjb227s7-May-09 13:20 
Generalthank u tanveer Pin
Member 295678228-Aug-08 22:09
Member 295678228-Aug-08 22:09 
Generalneed clarification on this code Pin
Member 295678225-Aug-08 0:17
Member 295678225-Aug-08 0:17 
GeneralRe: need clarification on this code Pin
T.Ashraf25-Aug-08 3:05
T.Ashraf25-Aug-08 3:05 
GeneralGood looking Pin
Riad MS Afyouni23-Feb-08 22:55
Riad MS Afyouni23-Feb-08 22:55 
GeneralNice Pin
Ashrafur Rahaman23-Feb-08 18:51
Ashrafur Rahaman23-Feb-08 18:51 
GeneralAnother simple artilce Pin
Sheo Narayan11-Jul-08 4:32
Sheo Narayan11-Jul-08 4:32 

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.