locked
Setting common properties for an ObjectDataSource RRS feed

  • Question

  • User-177998100 posted

    Hi there,

    In my ASP.NET application I am using a lot of ObjectDataSource controls. I want to set the following three properties globally for all ObjectDataSource controls:

                EnableCaching="true"
                CacheDuration="120" 
                CacheExpirationPolicy="Sliding"

    • EnableCaching="true"
    • CacheDuration="120"
    • CacheExpirationPolicy="Sliding"

    Is it possible to do that? I don't want to keep writing the same code for all my ObjectDataSources.

    I also want the option of overriding the global settings for one or two controls if needed. Is that possible too? What's the best way to do this?

    Thanks!

    Sunday, November 7, 2010 4:51 AM

Answers

  • User-231977777 posted

    hi

    i don't if there is a direct way but you can run the following  code one time in page load :

        if (!IsPostBack)
            {
                foreach (Control c in form1.Controls)
                {
                    if (c is ObjectDataSource)
                    {
                        ObjectDataSource objds = (ObjectDataSource)c;
                        objds.CacheDuration = 20;
                        objds.EnableCaching = true;
                        objds.CacheExpirationPolicy = DataSourceCacheExpiry.Sliding;
                    }
                }
            }

    hope this helps


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 7, 2010 7:34 AM
  • User1564875471 posted

    Is it possible to do that? I don't want to keep writing the same code for all my ObjectDataSources.

    I also want the option of overriding the global settings for one or two controls if needed. Is that possible too? What's the best way to do this?

    I think the cleanest way is to create a custom ObjectDataSource control that inherits from the ObjectDataSource and set some of default properties like this:

    namespace CustomControls
    {
        /// <summary>
        /// Summary description for CustomObjectDataSource
        /// </summary>
        public class CustomObjectDataSource : ObjectDataSource
        {
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                this.CacheDuration = 20;
                this.EnableCaching = true;
                this.CacheExpirationPolicy = DataSourceCacheExpiry.Sliding;
                //other default properties can be set here.
            }
        }
    }


    And then you can register it oin web.config file:

    <controls>
            <add assembly="App_Code"   tagPrefix="CustomControls" namespace="CustomControls" />

    And finally use it :

        <CustomControls:CustomObjectDataSource ID="CustomObjectDataSource1"  runat="server" SelectMethod="..." TypeName="...">
        </CustomControls:CustomObjectDataSource>

    Since the custom control inherits from the standard ObjectDataSource control, you can override it's properties from one of the pages like changing the cache duration.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 7, 2010 12:36 PM

All replies

  • User-231977777 posted

    hi

    i don't if there is a direct way but you can run the following  code one time in page load :

        if (!IsPostBack)
            {
                foreach (Control c in form1.Controls)
                {
                    if (c is ObjectDataSource)
                    {
                        ObjectDataSource objds = (ObjectDataSource)c;
                        objds.CacheDuration = 20;
                        objds.EnableCaching = true;
                        objds.CacheExpirationPolicy = DataSourceCacheExpiry.Sliding;
                    }
                }
            }

    hope this helps


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 7, 2010 7:34 AM
  • User-177998100 posted

    Thanks, what I've done is create this method on my form:

        private void ConfigureDataCache()
        {
            foreach (Control c in frmTradeInfo.Controls)
            {
                if (c is ObjectDataSource)
                {
                    ObjectDataSource objds = (ObjectDataSource)c;
                    objds.CacheDuration = 120;
                    objds.EnableCaching = true;
                    objds.CacheExpirationPolicy = DataSourceCacheExpiry.Sliding;
                }
            }        
        }


    Is there a way to call this from Page_Load on multiple forms? Perhaps put it in App_Code and call it from there and pass in the form? I'm not an experienced programmer so I'm not really sure if it's possible.

    Sunday, November 7, 2010 9:50 AM
  • User-231977777 posted

     


    Is there a way to call this from Page_Load on multiple forms? Perhaps put it in App_Code and call it from there and pass in the form? I'm not an experienced programmer so I'm not really sure if it's possible.

     

    hi

    create your class file :

    using System.Web.UI.HtmlControls;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    /// <summary>
    /// Summary description for Class1
    /// </summary>
    public class Class1
    {
    	public Class1()
    	{
    		//
    		// TODO: Add constructor logic here
    		//
    	}
        public  void ConfigureDataCache(HtmlForm frmTradeInfo)
        {
            foreach (Control c in frmTradeInfo.Controls)
            {
                if (c is ObjectDataSource)
                {
                    ObjectDataSource objds = (ObjectDataSource)c;
                    objds.CacheDuration = 120;
                    objds.EnableCaching = true;
                    objds.CacheExpirationPolicy = DataSourceCacheExpiry.Sliding;
                }
            }
        }
    }


    after that you can call it like this :

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Class1 c = new Class1();
                c.ConfigureDataCache(this.form1);
            }
        }


    hope this helps

    Ahmed

    Sunday, November 7, 2010 10:12 AM
  • User1564875471 posted

    Is it possible to do that? I don't want to keep writing the same code for all my ObjectDataSources.

    I also want the option of overriding the global settings for one or two controls if needed. Is that possible too? What's the best way to do this?

    I think the cleanest way is to create a custom ObjectDataSource control that inherits from the ObjectDataSource and set some of default properties like this:

    namespace CustomControls
    {
        /// <summary>
        /// Summary description for CustomObjectDataSource
        /// </summary>
        public class CustomObjectDataSource : ObjectDataSource
        {
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                this.CacheDuration = 20;
                this.EnableCaching = true;
                this.CacheExpirationPolicy = DataSourceCacheExpiry.Sliding;
                //other default properties can be set here.
            }
        }
    }


    And then you can register it oin web.config file:

    <controls>
            <add assembly="App_Code"   tagPrefix="CustomControls" namespace="CustomControls" />

    And finally use it :

        <CustomControls:CustomObjectDataSource ID="CustomObjectDataSource1"  runat="server" SelectMethod="..." TypeName="...">
        </CustomControls:CustomObjectDataSource>

    Since the custom control inherits from the standard ObjectDataSource control, you can override it's properties from one of the pages like changing the cache duration.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 7, 2010 12:36 PM
  • User-177998100 posted

    Very nice Anas, thanks very much. Just what I wanted.

    Monday, November 8, 2010 3:21 AM