User Control and DataSourceID Property

Last post 07-10-2007 3:43 AM by sujitm. 3 replies.

Sort Posts:

  • User Control and DataSourceID Property

    07-06-2007, 3:21 AM

    I created a user control (.ascx) that contains a paging-user-control, a repeater and some other controls.
    I want to use the property DataSourceID. How should I get the data from the datasource and when?
    What is the recommended way?

    I did it this way:
    - Create a method to get the data by reflection, like:
     

    private void PerformSelect()
    {
    	if (!String.IsNullOrEmpty(this.DataSourceID))
    	{
    		Control control = Page.FindControl(this.DataSourceID);
    		if (control == null)
    			control = Parent.FindControl(this.DataSourceID);
    
    		MethodInfo method = control.GetType().GetMethod("Select");
    		IEnumerable data = (IEnumerable)method.Invoke(control, new object[0]);
    
    		this.DataSource = data;
    		this.DataBind();
    	}
    }
     - Call this method
    In PageLoad when no postback
    Changing the current page (paging-user-control)
    Changing the current tab (tab-user-control)
    Switching Visibility to true

    I think that way is not the optimum, is there any better way?
    Do you have any suggestions or links?

    Thanks for your help, Andreas

  • Re: User Control and DataSourceID Property

    07-06-2007, 1:29 PM
    Answer
    • Contributor
      2,539 point Contributor
    • mgodoy_desenv
    • Member since 02-22-2007, 7:12 PM
    • São Paulo - Brazil
    • Posts 453

    Hi, a way to do that is custom your code to:

    private void PerformSelect()
    {
        if (!String.IsNullOrEmpty(this.DataSourceID))
        {
            Control control = Page.FindControl(this.DataSourceID);
            if (control == null)
            {
                control = NamingContainer.FindControl(this.DataSourceID);
            }

            if(control is ObjectDataSource)
            {
                this.DataSource = ((ObjectDataSource)control).Select();
                this.DataBind();
            }
            else
            {
                throw new NotSupportedException();
            }
        }
    }

  • Re: User Control and DataSourceID Property

    07-09-2007, 9:42 AM

    Thanks for your answer. My question was not meant to discuss advantages and disadvantages of casts / reflection.
    I do not know how the information flow is intended to work. Let's say there is a datasource with control paramters.
    Will the datasource inform my control to refresh its data? How does this happen? IDataSource? DataSourceView?

    Who initiates to select data from the source when one control parameter changes:
    - the objectdatasource does the select and informs its bound controls that new data available
    - the databound controls waiting for any signal from the datasource to request new data?

    How is this done by MS?

    Andreas

  • Re: User Control and DataSourceID Property

    07-10-2007, 3:43 AM
    Answer
    • Contributor
      3,121 point Contributor
    • sujitm
    • Member since 05-23-2007, 9:01 AM
    • Pune
    • Posts 512

    Hi,

    It has to be done by overriding the DataBind() method in the UserControl class. The page calls DataBind() methods recursively on all the data bound controls contained in it. The user just has to call Page.DataBind().

    In the overridden method DataBind() call PerformSelect() method which you have already written. This is how BaseDataBoundControl does it. For more information check source code of BaseDataBoundControl using Reflector utility.

    - Sujit

    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
Page 1 of 1 (4 items)