Flex: Is it possible to pass multiple strings between views? - apache-flex

I have a list of text files and when one is selected it is read and assigned to a string variable. this string is then pushed to a view : pushView(viewer, string), where the string is displayed in a textarea. I want to also pass the relative path of the file also. Is this possible to pass two strings?
protected function view1_viewActivateHandler(event:ViewNavigatorEvent):void
{
if(data!=null){
mainTextField.text = data.toString();
}else{
mainTextField.text = "";
}
}

Yes it is possible. Typically the way I do this in Flex is to add a data property on my view of type Object allowing me to pass any type of object into the view.
In your case I would change pushView(viewer, string) to something like pushView(viewer, data) where data is some object that stores all the data you need to pass to the view. data might look like:
var data:Object = { text: "my text to display",
relativePath: "the/relative/path" };

Related

QML Access a Component using a string to reference it´s ID

Here is the thing. I need to do the same processing for 16 Custom Input Boxes. This is a drag. So I thought I could add their ids to some sort of list and then iterate through a list, calling on a fucntion that does the processing.
Problem is I don't know how to access the component (to get the inputted text and set some variables inside it) when the id is in a string variable. Is this possible?
I'm leaving an answer here, in case it is of any help to anyone.
As it turns out you can just pass the input element as a parameter to a function. So lets say that you have
TextInput{
id: input1
...
}
TextInput{
id: input2
...
}
You could do this:
var list = [input1, input2]
for (var i = 0; i < list.length; i++) doStuffFunction(list[i])
And inside the function...
function doStuffFunction (imp){
var thetext = imp.text
//... Do more stuff
}

How to use the widget fullpath to access widget's properties?

I know that using widget.getFullPath() method, we can retrieve the full path of a widget as a string in the format "pagename.child1name.child2name....childNname.widgetname".
I am trying to find a way for using this string to access the original widget.
I was successful using this:
app.pages["pagename"].children["child1name"].children["child2name"]....children["childNname"].children["widgetname"].text = "some text";
but I don't know how to change this in a more general function, independent by the depth of the path.
Any idea would be very much appreciated. Thank you.
The parent root descendents should have everything on the page, so try:
widget.parent.root.descendants.ANYTHING.value;
you might alternately need to do this if you want ANYTHING to be a variable
widget.parent.root.descendants[ANYTHING].value;
My intention is to access the widget's property from another page. The root of a widget is its page, and it is the string before the first dot in the full path.
I eventually wrote a function which builds the object after splitting the full path string in an array:
function getWidgetPath(pathString) {
var pathArray = pathString.split('.');
var widgetPath = app.pages[pathArray[0]];
for( i=1; i<pathArray.length; i++ ) {
widgetPath = widgetPath.children[pathArray[i]];
}
return widgetPath;
}
The function can be used to build the target object. I.e., considering that the target widget is a label and I want to change its text property, I use:
var targetPath = 'parentpage.panel1.panel2.panel3.label1';
var targetWidget = getWidgetPath(targetPath);
targetWidget.text = 'some text';
I have the feeling that it's not very Javascriptian, though.

Adding additional attributes to CheckBoxList on DataBind

I have a checkbox list that is bound to values from a database, as follows
chkTopLanguages.DataSource = dsSiteLanguages;
chkTopLanguages.DataTextField = "Language";
chkTopLanguages.DataValueField = "LanguageID";
chkTopLanguages.DataBind();
However, I need to also add another value (AltLanguage) to a custom attribute so that I can access this value in some instances. How can I add an additional value attribute to the checkbox items on databind?
You're probably not going to like this, but when I faced this issue before the only way I could see doing it in pure .NET was to do the following:
// Create this field on your data source objects
public LanguageField {
get {
return LanguageID + "_" + AltLanguage;
}
}
chkTopLanguages.DataSource = dsSiteLanguages;
chkTopLanguages.DataTextField = "Language";
chkTopLanguages.DataValueField = "LanguageField";
chkTopLanguages.DataBind();
Then, when you get your values, split the value by "_" and then you can grab both values.

Change Single URL query string value

I have an ASP.NET page which takes a number of parameters in the query string:
search.aspx?q=123&source=WebSearch
This would display the first page of search results. Now within the rendering of that page, I want to display a set of links that allow the user to jump to different pages within the search results. I can do this simply by append &page=1 or &page=2 etc.
Where it gets complicated is that I want to preserve the input query string from the original page for every parameter except the one that I'm trying to change. There may be other parameters in the url used by other components and the value I'm trying to replace may or may not already be defined:
search.aspx?q=123&source=WebSearch&page=1&Theme=Blue
In this case to generate a link to the next page of results, I want to change page=1 to page=2 while leaving the rest of the query string unchanged.
Is there a builtin way to do this, or do I need to do all of the string parsing/recombining manually?
You can't modify the QueryString directly as it is readonly. You will need to get the values, modify them, then put them back together. Try this:
var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
nameValues.Set("page", "2");
string url = Request.Url.AbsolutePath;
string updatedQueryString = "?" + nameValues.ToString();
Response.Redirect(url + updatedQueryString);
The ParseQueryString method returns a NameValueCollection (actually it really returns a HttpValueCollection which encodes the results, as I mention in an answer to another question). You can then use the Set method to update a value. You can also use the Add method to add a new one, or Remove to remove a value. Finally, calling ToString() on the name NameValueCollection returns the name value pairs in a name1=value1&name2=value2 querystring ready format. Once you have that append it to the URL and redirect.
Alternately, you can add a new key, or modify an existing one, using the indexer:
nameValues["temp"] = "hello!"; // add "temp" if it didn't exist
nameValues["temp"] = "hello, world!"; // overwrite "temp"
nameValues.Remove("temp"); // can't remove via indexer
You may need to add a using System.Collections.Specialized; to make use of the NameValueCollection class.
You can do this without all the overhead of redirection (which is not inconsiderable). My personal preference is to work with a NameValueCollection which a querystring really is, but using reflection:
// reflect to readonly property
PropertyInfo isReadOnly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
// make collection editable
isReadOnly.SetValue(this.Request.QueryString, false, null);
// remove
this.Request.QueryString.Remove("foo");
// modify
this.Request.QueryString.Set("bar", "123");
// make collection readonly again
isReadOnly.SetValue(this.Request.QueryString, true, null);
Using this QueryStringBuilder helper class, you can grab the current QueryString and call the Add method to change an existing key/value pair...
//before: "?id=123&page=1&sessionId=ABC"
string newQueryString = QueryString.Current.Add("page", "2");
//after: "?id=123&page=2&sessionId=ABC"
Use the URIBuilder Specifically the link textQuery property
I believe that does what you need.
This is pretty arbitrary, in .NET Core at least. And it all boils down to asp-all-route-data
Consider the following trivial example (taken from the "paginator" view model I use in virtually every project):
public class SomeViewModel
{
public Dictionary<string, string> NextPageLink(IQueryCollection query)
{
/*
* NOTE: how you derive the "2" is fully up to you
*/
return ParseQueryCollection(query, "page", "2");
}
Dictionary<string, string> ParseQueryCollection(IQueryCollection query, string replacementKey, string replacementValue)
{
var dict = new Dictionary<string, string>()
{
{ replacementKey, replacementValue }
};
foreach (var q in query)
{
if (!string.Equals(q.Key, replacementKey, StringComparison.OrdinalIgnoreCase))
{
dict.Add(q.Key, q.Value);
}
}
return dict;
}
}
Then to use in your view, simply pass the method the current request query collection from Context.Request:
<a asp-all-route-data="#Model.NextPageLink(Context.Request.Query)">Next</a>

Accessing the object/row being edited in Dynamic Data

I'm modifying the "Edit.aspx" default page template used by ASP.NET Dynamic Data and adding some additional controls. I know that I can find the type of object being edited by looking at DetailsDataSource.GetTable().EntityType, but how can I see the actual object itself? Also, can I change the properties of the object and tell the data context to submit those changes?
Maybe you have found a solution already, however I'd like to share my expresience on this.
It turned out to be a great pita, but I've managed to obtain the editing row. I had to extract the DetailsDataSource WhereParameters and then create a query in runtime.
The code below works for tables with a single primary key. If you have compound keys, I guess, it will require modifications:
Parameter param = null;
foreach(object item in (DetailsDataSource.WhereParameters[0] as DynamicQueryStringParameter).GetWhereParameters(DetailsDataSource)) {
param = (Parameter)item;
break;
}
IQueryable query = DetailsDataSource.GetTable().GetQuery();
ParameterExpression lambdaArgument = Expression.Parameter(query.ElementType, "");
object paramValue = Convert.ChangeType(param.DefaultValue, param.Type);
Expression compareExpr = Expression.Equal(
Expression.Property(lambdaArgument, param.Name),
Expression.Constant(paramValue)
);
Expression lambda = Expression.Lambda(compareExpr, lambdaArgument);
Expression filteredQuery = Expression.Call(typeof(Queryable), "Where", new Type[] { query.ElementType }, query.Expression, lambda);
var WANTED = query.Provider.CreateQuery(filteredQuery).Cast<object>().FirstOrDefault<object>();
If it's a DD object you may be able to use FieldTemplateUserControl.FindFieldTemplate(controlId). Then if you need to you can cast it as an ITextControl to manipulate data.
Otherwise, try using this extension method to find the child control:
public static T FindControl<T>(this Control startingControl, string id) where T : Control
{
T found = startingControl.FindControl(id) as T;
if (found == null)
{
found = FindChildControl<T>(startingControl, id);
}
return found;
}
I found another solution, the other ones did not work.
In my case, I've copied Edit.aspx in /CustomPages/Devices/
Where Devices is the name of the table for which I want this custom behaviour.
Add this in Edit.aspx -> Page_Init()
DetailsDataSource.Selected += entityDataSource_Selected;
Add this in Edit.aspx :
protected void entityDataSource_Selected(object sender, EntityDataSourceSelectedEventArgs e)
{
Device device = e.Results.Cast<Device>().First();
// you have the object/row being edited !
}
Just change Device to your own table name.

Resources