ASP.NET Ajax Server Control Property Value Exchange

Last post 06-24-2009 10:45 PM by chetan.sarode. 1 replies.

Sort Posts:

  • ASP.NET Ajax Server Control Property Value Exchange

    06-24-2009, 6:52 AM

    I want to program a Server Control with Ajax Support. Now I have made the Control Class and the JS File as from Microsoft proposed. If I set a value to a Property in the C# Code and get it from the JS Class it works perfectly. But if I asign a value in the JS for a Property and make a postback, then the value is not saved back to the C# Property. What am I doing wrong? Is this possible? I would be very gratefull for any help! Thanks a lot in advance!

    JS Code:

    // Register the namespace for client control.
    Type.registerNamespace('Samples');

    // Define the control class and properties.
    Samples.SampleTextBox = function(element)
    {
        Samples.SampleTextBox.initializeBase(this, [element]);

        // Class properties
    }

    // Creates the prototype of the control
    Samples.SampleTextBox.prototype =
    {
        initialize: function()
        {
            Samples.SampleTextBox.callBaseMethod(this, 'initialize');
        },

        dispose: function()
        {
            Samples.SampleTextBox.callBaseMethod(this, 'dispose');
        },

        get_Expanded: function()
        {
            return this.Expanded;
        },

        set_Expanded: function(value)
        {
            this.Expanded = value;
            this.raisePropertyChanged('Expanded');
        },

        Alert: function()
        {
            alert(this.Expanded);
        }
    }

    Samples.SampleTextBox.descriptor =
    {
        properties:
            [
                {name: 'Expanded', type: String}
            ]
    }

    Samples.SampleTextBox.registerClass('Samples.SampleTextBox',Sys.UI.Control);
     
    if (typeof(Sys) !== 'undefined')
    {
        Sys.Application.notifyScriptLoaded();
    }


    C# Code:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Collections.Generic;

    namespace Samples
    {
        public class SampleTextBox : TextBox, IScriptControl
        {
            private ScriptManager ScriptManager;

            private string _Expanded = "Paul";
            public string Expanded
            {
                get
                {
                    return _Expanded;
                }

                set
                {
                    _Expanded = value;
                }
            }
            
            protected override void OnPreRender(EventArgs e)
            {
                if (!this.DesignMode)
                {
                    // Test for ScriptManager and register if it exists
                    this.ScriptManager = ScriptManager.GetCurrent(Page);

                    if (this.ScriptManager == null)
                        throw new HttpException("A ScriptManager control must exist on the current page.");

                    this.ScriptManager.RegisterScriptControl(this);
                }

                base.OnPreRender(e);
            }

            protected override void Render(HtmlTextWriter writer)
            {
                if (!this.DesignMode)
                    this.ScriptManager.RegisterScriptDescriptors(this);

                base.Render(writer);
            }

            protected virtual IEnumerable<ScriptReference> GetScriptReferences()
            {
                ScriptReference reference = new ScriptReference();

                reference.Path = ResolveClientUrl("/Scripts/SampleTextBox.js");

                return new ScriptReference[] { reference };
            }

            protected virtual IEnumerable<ScriptDescriptor> GetScriptDescriptors()
            {
                ScriptControlDescriptor descriptor = new ScriptControlDescriptor("Samples.SampleTextBox", this.ClientID);

                descriptor.AddProperty("Expanded", this.Expanded);

                return new ScriptDescriptor[] { descriptor };
            }

            IEnumerable<ScriptReference> IScriptControl.GetScriptReferences()
            {
                return GetScriptReferences();
            }

            IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors()
            {
                return GetScriptDescriptors();
            }
        }
    }

  • Re: ASP.NET Ajax Server Control Property Value Exchange

    06-24-2009, 10:45 PM
    Answer
Page 1 of 1 (2 items)