PDA

View Full Version : [CLOSED] Get javascript function's value



multimediait
Oct 16, 2014, 9:15 PM
Good evening.
Is there a way to get a javascript function's value from codebehind (C#), without DirectMethod or hidden fields?
Thanks in advance.

geoffrey.mcgill
Oct 17, 2014, 4:01 AM
You can pass a value in the Query String.

Hope this helps.

multimediait
Oct 21, 2014, 9:00 PM
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)?

geoffrey.mcgill
Oct 22, 2014, 2:13 AM
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.

multimediait
Oct 23, 2014, 7:50 AM
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(DesignerSerializat ionVisibility.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

Daniil
Oct 24, 2014, 4:44 AM
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.

multimediait
Oct 24, 2014, 10:20 AM
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.