Adding additional attributes to CheckBoxList on DataBind - asp.net

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.

Related

xpages session scope AND document data binding

I have a value that I get from a picklist. I set this value as a sessionScope variable.
I then want to use this value, do a lookup, and set the value of an input field - which is working.
However, I am doing the lookup code in the fields data binding section using SSJS, and as such am not too sure how to save this value (normally my data binding would just be document1.FIELDNAME)
I've tried setting the value as part of my code, but the change is not saved in the backend doc.
I've also tried doing the lookup code in the fields "Default value" property, but this always just returns nothing.
Does anyone know how I can display on the xpage the value from my lookup AND also save this value to the backend document?
I fear I am missing something simple and maybe getting tunnel vision!
The code I am using for my data binding value is below.
Thanks
try{
var key1 = sessionScope.PLProspectiveAssured;
var dbName:NotesDatabase = session.getDatabase(database.getServer(),"CIR2001.nsf");
if (key1==""){
returnVal = "Not found";
}else {
var vwOrgs:NotesView = dbName.getView("OrgDocID");
var doc:NotesDocument = vwOrgs.getDocumentByKey(key1);
returnVal = doc.getItemValueString("OrgCountry");
}
// set our field
var doc:NotesDocument = document1.getDocument();
doc.replaceItemValue("ProspectiveAssured", returnVal);
return returnVal;
}catch(e){
openLogBean.addError(e,this);
}
Use your datasource and set the value using .setValue(field, value). In your case:
// set our field
document1.setValue("ProspectiveAssured", returnVal);
Make sure that you save your datasource somewhere (else).

ASP Multiselect listbox separator

I have encountered a problem and I didn't manage to find any soultions yet. Let me simplify things a bit.
I have 2 forms, the first contains an ASP ListBox with multi select mode enabled. I submit the form and in the other form I use just for testing purposes this snippet of code:
protected void Page_Load(object sender, EventArgs e)
{
foreach (string formKey in Request.Form.AllKeys)
{
if (formKey != null)
{
if (formKey.Equals("ctl00$MainContent$ListBox1"))
Label1.Text = Request.Form[formKey];
}
}
}
The problems is that the values that come from the listbox (the values that i selected in the previous form) are separated by "," for ex. "test1,test2,test3". How can i change this separator to "$" for example? I need to change it because the actual values may contain "," and i don't manualy feed them to the listbox.
I can't use any other mode of transfering this values between the form because the entire application uses this model. The values that i get are then sent to a workflow where there will be manipulated and in the workflow i need to know where each listbox item starts and ends so it must be an unique separator.
Any help is apreciated! Thank you very much
Thank you MatteKarla but unfortunately this does not solve my problem. Yes, this is a good way of transfering the values from one form to another.
However i must use the method I described above with Request form keys because the listbox is one of many others "parameters" that are generated at runtime and have their values sent to a workflow method that takes this values. And i can't afford to change that in my application.
My problem is that coma (",") separator is used by default with a multiselect listbox.
I thought that there maybe is a method to change that separator from coma to another char because the coma can also be included in the value itself and this will create confusion.
As i said if i select three values test1, test2 and test3, the result with my method will be a string looking like "test1,test2,test3". However a "test1$test2$test3" would be much better.
But I'm affraid that changing this default separator is not possbile. I must think at a method to overcome this problem like replacing before feeding the listbox all the intended coma from the values with some other char not to create confusion. But this is not a great way of doing it.
On your first page/form (First.aspx.cs) create a public property with the listbox:
public ListBox PostedListBox { get { return ListBox1; } }
Set the postback-url for the button to Second.aspx
Second page in the aspx-file after the #Page-directive add:
<%# PreviousPageType VirtualPath="~/First.aspx" %>
Then in Form_Load on Second.aspx.cs you can extract the values:
if (PreviousPage != null)
{
ListBox postedListbox = PreviousPage.PostedListBox;
foreach (var index in postedListbox.GetSelectedIndices())
{
var itemText = postedListbox.Items[index].Text;
}
}
Or you could just try to locate the control by using:
if (PreviousPage != null)
{
var control = PreviousPage.FindControl("ListBox1") as ListBox;
}
Third Edit:
You could use GetValues:
Request.Form.GetValues("ctl00$MainContent$ListBox1");
returns a string array containing each of the selected items.

How to update a table row with save button using .ajax

I have a table which has one row and only one cell will be editable. I have accomplished this with the following code.
$("td#effEndDate").click(function() {
if (!$(this).hasClass("edit")) {
var value = jQuery.trim($(this).html());
$(this).html("<input id=\"txtEdit\" type=\"text\" value=\"" + value + "\" />");
$(this).addClass("edit");
$("#txtEdit").focus();
}
});
Now this is the part where i'm stuck.
After the field is updated a save button must be clicked to call the proper .ajax method to update the database. But how can I compare the previous value to the current value on a button press? Since i'm not using the onblur property where I could have saved the old value and passed it to the update function.
There are two possibilities.
Pass the variable around in between functions
Make the variable global
if you want the variable global do not use the "var" keyword
Change:
var value = jQuery.trim($(this).html());
To this:
value = jQuery.trim($(this).html());
Edit
If the click function is getting hit more then once before a page refresh and assuming you want to keep a copy of the original table rows you can try this. Save a copy of the original table in a variable then you can query the original table for the html using the ID number. Here is a quick mock
first store the table in a variable upon the page loading. This will save an original copy of the table
//get a copy of the table
var GetCopyofOriginalTable = function() {
var TableToBeCopied = $('the appropriate selector');
CopyOfTable = JQuery.extend(true, {}, TableToBeCopied); //Notice no var, its global
}
//Now store the variale
GetCopyofOriginalTable();
var FindTableRowByID = function(trID) {
return $('table:has(tr#' + trID));
}
Now you can loop through the new table test its value agaisnt the old table. This methed make use alot of memory depending on how big the table is.
I would store the original value somewhere in a variable, and compare the two in the submission function before sending the .ajax call.

Converting a string into a CheckBox

I have a string which is ultimately the id of a CheckBox.
What I need to be able to do is to access the CheckBox's properties from the string
var myCheckBox:Object;
var myString:String;
myString = "checkbox_1"
myCheckBox = Object(myString); ?!?!
... and then I'd need to get to myCheckBox.selected, and myCheckBox.label etc
easier answer:
if(this.hasOwnProperty(myString) && this[myString] is CheckBox) {
myCheckBox = this[myString] as CheckBox
}
It's a bit of overcoding (since the as keyword will return a null if it's not a checkbox and you could better handle it that way with potentially less code), but that should do ya.
Best of luck.
If you know what DisplayObjectContainer (e.g. Sprite, MovieClip) the CheckBox is inside you can use getChildByName.
Unfortunately if you are using Flex containers (like Group) there is no function getElementByName(). There is getElementAt so you could write a loop that iterates over all of a Groups elements until it encounters one that matches the name you have.

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