[CLOSED] Get javascript function's value

  1. #1

    [CLOSED] Get javascript function's value

    Good evening.
    Is there a way to get a javascript function's value from codebehind (C#), without DirectMethod or hidden fields?
    Thanks in advance.
    Last edited by Daniil; Oct 24, 2014 at 10:29 AM. Reason: [CLOSED]
  2. #2
    You can pass a value in the Query String.

    Hope this helps.
    Geoffrey McGill
    Founder
  3. #3
    Hi Geoffrey, thanks for your quick response.
    Within a custom control, I can use State.Get and State.Set to retrieve (and set) a variable's value (client side)?
  4. #4
    Can you mockup a simple sample of what you want to accomplish. Maybe there is a way, although I don't 100% understand your requirements at this moment.
    Geoffrey McGill
    Founder
  5. #5
    Sure, this is a small example.
    I would, get variable's value "value", in C#.
    I know that I would use DirectEvent Change but, I prefer set when I need it.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Xml.Serialization;
    
    using Ext.Net;
    using Container = Ext.Net.Container;
    
    using Newtonsoft.Json;
    
    [assembly: WebResource("ExtNetTestEnv.UI.AnyControl.AnyControl.js", "text/javascript")]
    
    namespace ExtNetTestEnv.UI
    {
    	public enum ControlKind
    	{
    		TextField,
    		DataField
    	}
    
    	public class AnyControl : Container
    	{
    		public ControlKind Kind
    		{
    			get { return State.Get("kind", ControlKind.TextField); }
    			set { State.Set("kind", value); }
    		}
    
    		public string Value
    		{
    			get
    			{
    				Call("getValue");
    				return State.Get("value", "");
    			}
    			set { State.Set("value", value); }
    		}
    
    		[Browsable(false)]
    		[EditorBrowsable(EditorBrowsableState.Never)]
    		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    		[XmlIgnore]
    		[JsonIgnore]
    		public override ConfigOptionsCollection ConfigOptions
    		{
    			get
    			{
    				ConfigOptionsCollection list = base.ConfigOptions;
    				list.Add("kind", new ConfigOption("kind", new SerializationOptions("kind", JsonMode.Value),
    					ControlKind.TextField, Kind));
    				list.Add("value", new ConfigOption("value", new SerializationOptions("value", JsonMode.Value),
    					"", Value));
    				return list;
    			}
    		}
    
    		[Category("0. About")]
    		[Description("")]
    		public override string InstanceOf
    		{
    			get { return "App.AnyControl"; }
    		}
    
    		[Category("0. About")]
    		[Description("")]
    		public override string XType
    		{
    			get { return "anycontrol"; }
    		}
    
    		protected override List<ResourceItem> Resources
    		{
    			get
    			{
    				List<ResourceItem> baseList = base.Resources;
    				baseList.Capacity += 1;
    				baseList.Add(new ClientScriptItem(typeof(AnyControl),
    					"ExtNetTestEnv.UI.AnyControl.AnyControl.js", ""));
    				return baseList;
    			}
    		}
    
    		protected override void OnInit(EventArgs e)
    		{
    			base.OnInit(e);
    			DirectEvents.AfterRender.Event += (caller, args) =>
    			{
    				if (Kind == ControlKind.TextField)
    				{
    					new TextField
    					{
    						ID = "text",
    						FieldLabel = "Text"
    					}.Render(this);
    				}
    				else
    				{
    					new DateField
    					{
    						ID = "date",
    						FieldLabel = "Text"
    					}.Render(this);
    				}
    			};
    		}
    	}
    }
    Ext.define('App.AnyControl', {
    	extend: 'Ext.container.Container',
    	alias: 'widget.anycontrol',
    
    	kind: 'TextField',
    	value: '',
    
    	getValue: function () {
    		var item = this;
    		if (item.kind == 'TextField') {
    			var txt = item.down('textfield');
    			if (txt) {
    				item.value = txt.getValue();
    			}
    		} else {
    			var dt = item.down('datefield');
    			if (dt) {
    				item.value = dt.getValue();
    			}
    		}
    		return item.value;
    	}
    });
    I hope I was clear
    Last edited by multimediait; Oct 23, 2014 at 5:59 PM.
  6. #6
    You cannot get a client side value on server without submitting it somehow to server. So, the answer to this question is "No, you cannot".
    Within a custom control, I can use State.Get and State.Set to retrieve (and set) a variable's value (client side)?
    You can render a hidden field to contain an AnyControl's value on client. Then it will be submitted to server. Then you should implement a LoadPostData method for the AnyControl server class to extract a respective value from POST. You can look at any LoadPostData implementation inside Ext.NET as an example.

    There is one more thing I would like to say about. This method:
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        DirectEvents.AfterRender.Event += (caller, args) =>
        {
            if (Kind == ControlKind.TextField)
            {
                new TextField
                {
                    ID = "text",
                    FieldLabel = "Text"
                }.Render(this);
            }
            else
            {
                new DateField
                {
                    ID = "date",
                    FieldLabel = "Text"
                }.Render(this);
            }
        };
    }
    Any DirectEvent is an AJAX request. In your case, an AnyControl is rendered on client, then it initiates an AfterRender DirectEvent and a TextField or a DateField is rendered on the fly. Is that really required? That looks quite suboptimal.

    I would use:
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        if (Kind == ControlKind.TextField)
        {
            this.Items.Add(new TextField
            {
                ID = "text",
                FieldLabel = "Text"
            });
        }
        else
        {
            this.Items.Add(new DateField
            {
                ID = "date",
                FieldLabel = "Text"
            });
        }
    }
    One more thing is ID = "text" and ID = "date". I think that stops you to reuse your custom control, because each instance will have the same IDs, but they must be unique.
  7. #7
    I understand, thanks Daniil, I will try with hidden field and override LoadPostData.
    A control that dynamically creates inner controls based on the type of a specified property's value

    In example:
    typeof(string) = TextField
    typeof(int) = TextField (with regex)
    typeof(DateTime) = DateField
    typeof(bool) = RadioGroup
    etc...

    ID "text" and "date", were static because was an small example, thanks anyway for your advice :)
    You can mark with CLOSED.

Similar Threads

  1. [CLOSED] Javascript function migrating
    By ViDom in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: May 07, 2013, 10:06 AM
  2. Calling JavaScript function
    By UserClarion in forum 1.x Help
    Replies: 2
    Last Post: Dec 30, 2011, 10:23 AM
  3. Replies: 1
    Last Post: May 14, 2011, 3:52 PM
  4. [CLOSED] Call Javascript Function
    By speedstepmem4 in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Jul 21, 2010, 3:58 AM
  5. [CLOSED] The javascript function in not fire?
    By Etisbew in forum 1.x Legacy Premium Help
    Replies: 7
    Last Post: Sep 09, 2009, 10:12 AM

Posting Permissions