updating fields via SharePoint Form and via List item event receiver - sharepoint-2010

I am confused and cannot seem to find the right answer to this problem:
When I use the ItemUpdating method in the event receiver to update the field title as show in the code below it works, but other fields which I have changed via the "Edit Form" do not change. they remain as they are.
public override void ItemUpdating(SPItemEventProperties properties)
{
SPListitem item = properties.ListItem;
item["Title"] = "title has been changed";
item.Update();
}
if I put the above code in ItemUpdated it does not work, but the changes done via UI (edit form) take effect.
can anyone help me solve this problem?
thank you
Editing
Let me put it simply like this:
can anyone tell me how can I update one or multiple fields via Event Receiver and the other field(s) via the Edit Form of a list?
I hope this is clearer.

It is always best to use ItemUpdated in the event receiver to update a field in the list.
and also you must note that updating the field in the event receiver using
item.Update();
might cause some problems when you start workflow. You can use the following
ways to update multiple field.
base.EventFiringEnabled = false;
try
{
item.SystemUpdate(false);
}
finally
{
base.EventFiringEnabled = true;
}
break;

When using the ItemUpdating event, check the AfterProperties property of the properties parameter. It is a dictionary containing all the new values that the item will have. It contains raw values, though, all of them as strings, before being processed into the actual values you'll see when you fetch the item properties.
When you execute your code in the ItemUpdating event, what happens is this:
User clicks save;
ItemUpdating is fired and executes up to your call to Update().
At this point, your call to Update() fires another ItemUpdating event to the item. In the context it runs, no fields have changed other than Title.
Control returns to the method you overrode. The changes that were to be persisted to the item don't apply anymore.
And that's why you lose the changes made by the user.
When you use ItemUpdated instead, the changes made by the user are persisted because they are saved before you do anything. Then you call that Update() inside an ItemUpdated event. That one is either throwing a stack overflow exception silently or doing something else completely crazy, because you've created an infinite recursion loop there - and that's why you don't see it changing the title.
If you want to add extra changes to an item being saved by the UI, modify the AfterProperties property of properties instead of changing them directly in the item.

Related

wxPropertyGrid::EditorValidate() always returns true

A user is editing a value in a property grid, then he clicks a button outside the property grid which executes code to read the property values. Sometimes the OLD value of the property is read, rather than the new value which was being edited. I have to tell the users that they must complete the editing, by hitting return or clicking on another property before clicking any buttons outside the grid. They forget, and report a bug.
I would like to make this foolproof. Perhaps by forcing the current edit to complete when the mouse leaves the property grid.
I know how to handle the mouse leaving event. I do not know how to force the property grid to accept any partial edits.
I have tried, as a hint to the user,
pg = new wxPropertyGrid( ...
....
if( ! pg->EditorValidate() )
{
SetStatusText("Please complete editing");
return;
}
but EditorValidate() always returns true
Found it!
wxPropertyGrid::CommitChangesFromEditor()
http://docs.wxwidgets.org/trunk/classwx_property_grid.html#a6e06d92a622237457fea00372df1eaae

How to assign an output to a label in a second form through the first form?

I wanna show the output of the calculations in a second form but I am writing the code in the first form. How can I do it?
Use the parent form name in front of the control that you are trying to work with. You may need to set the modifier to Friend.
Say that the label exists in form2 and you have code in form1 that is needing to change it. So you would do it like this: form2.label.text = "the string value here"
However, keep in mind that if the control was created or is owned by a different thread then the one that is trying to edit the control, you will receive a runtime exception.
To resolve that you will need to create a delegate for the calling sub or function.

How to save Id for dijit.form.ComboBox

I'm writing my first dijit control for EPiServer. In my template I am using the dijit.form.ComboBox.
I have attached an event handler to the "onChange" event like so:
postCreate: function () {
// call base implementation
this.inherited(arguments);
// Init textarea and bind event
this.inputWidget.set("intermediateChanges", this.intermediateChanges);
this.inputWidget.set("store", this.store);
this.connect(this.inputWidget, "onChange", this._onInputWidgetChanged);
},
Then in my event handler I have:
_onInputWidgetChanged: function (e) {
alert(e.id);
this._updateValue(value);
},
My issue is that as with a typical dropdown list, I want to store the Value rather than the Text. The options in my combobox look like so:
Value | Text
1 | "Test"
2 | "A different test"
The problem is that the value passed into the _onInputWidgetChanged handler is always the text value of the combobox i.e. "Test" or "A different test"
How can I get access to the Value instead? As I said, this is the first time I have ever worked with dojo and dijit so I may be missing something fundamental here.
Thanks in advance
Al
The thing about ComboBox is that its value is not required to be an entry in the drop-down menu (and thus, not guaranteed to be one either). Think of it as a textbox with autosuggest - users can use the menu to expedite the process, but the value of the textbox is freeform and is reported as whatever the user types into it.
If you want users to be required to choose an entry from the menu, you should be using FilteringSelect instead, which will report the associated store item's ID (or associated option tag's value) as its value. As opposed to the free-form nature of ComboBox, FilteringSelect can be thought of as a menu with type-ahead functionality.

How can I force entry to complete in a NSTextField from a NSButton

I just noticed a problem when my user-interface is in a certain state. I have a table view with two columns both of which the user can enter data that is to be used later. There's also a button which acts upon the contents of the table view.
The problem is if the user has entered new data in the column but has not yet exited the field by using the tab key or return key (i.e. the cursor is still in the field and in editing mode) and the button is pressed the old value is used not the current value sitting in the field.
What is the best way to handle this this? I want to use whatever the user has entered thus far.
Basically, The button code needs to tell the text field to finish completion or exit the editing mode. But I can't seem to find a method that will do that.
Use bindings. In Interface Builder, select the table column and in the Inspector go to Table Column Bindings and set the Value content binding appropriately and ensure the "Continuously Updates Values" option is checked. Then changes to the table cell content will propagate immediately.
Found the answer, At least for me.
Find out if a row is selected and if so deselect it. This causes the current entry to be completed.
- (void) completeTableEntry
{
// If a column is selected ensure it is completed
NSInteger sr = [keyValueTable selectedRow];
if (sr != -1) {
[keyValueTable deselectRow:sr];
}
}
How about:
[theTargetWindowWhateverThatIs endEditingFor:nil];
theTargetWindowWhateverThatIs may be, for example, self.window if you are inside a NSWindowController.

Reading 'Checked' Property Of Dynamic CheckBox on Page Postback

Apologies in advance for the long-winded post, but I'm having some trouble with a .NET page I'm building.
END QUESTION: How can I check the 'Checked' property of a dynamically-created checkbox during the Page_Load subroutine?
DETAILS: Basically, I have a VB.NET page that creates some table rows dynamically, based on a number selected by the user. So, for example, if the user selects "3" from a dropdown list, the page posts back and creates three table rows. Each row contains a couple of textboxes, a dropdown list, and a checkbox (which are all .NET form controls rather than plain HTML controls, I should point out).
Typically, the user would enter a few details into the form controls, and click the 'Submit' button, after which the page iterates through each row, and inserts the data into a SQL Server table.
But if the user ticks the checkbox for that row, this signifies that the page is to ignore that row, and NOT insert that row of data into the database when the user clicks 'Submit'.
This works well, but there is a problem. If the user clicks 'Submit' and some of the values entered into the form controls are invalid (so, for example, the user didn't enter their name) then the page won't submit the data, and instead, shows an error to the user informing them of the values they need to change. But if the user creates three rows, for example, but decides to "ignore" the third row (by ticking the checkbox) then when the page posts back, finds some invalid entries, and re-shows the form to the user to allow them to correct any errors, I'd rather the page didn't render the third row altogether. After all, they chose to create three rows originally, but then decided that they only needed two. So it makes sense that the third row is not recreated.
To start with, I simply used code similar to the following within my Page_Load subroutine:
If objCheckbox.Checked = False
' Render the row, and recreate the dynamic controls '
Else
' Dont render the row or any of the controls '
End If
But what seemed to happen was that objCheckbox.Checked was always returning False, even when the checkbox was ticked. Once the page had loaded, the table rows had rendered again, and the tick was present in the checkbox, so it's not like the value was lost on postback. But at the point I check whether the checkbox is ticked, it always returns False, rendering a table row that the user doesn't need.
Does anyone know how to get round this problem? I've read lots of articles about the .NET ViewState, and the page lifecycle, but I've yet to find a solution that works. I simply need to be able to check if a checkbox is ticked before re-creating some dynamic controls.
I tried this alternative code, which utilises the ViewState, but to no avail:
If objIgnoreCheckbox.ViewState("Checked") = False Then
' Render the row, and recreate the dynamic controls '
Else
' Dont render the row or any of the controls '
End If
When doing this, I get the following error:
'System.Web.UI.Control.Protected Overridable ReadOnly Property ViewState() As System.Web.UI.StateBag' is not accessible in this context because it is 'Protected'.
So I tried to create a custom class, that inherited from Checkbox, and tried to override the ViewState property to make it public, so that it can be read from:
Public Class CheckboxControl
' Inherits all Checkbox properties and methods '
Inherits Checkbox
' Create the public ViewState property '
Public Overrides ReadOnly Property ViewState As StateBag
Get
Dim objChecked As Object = ViewState("Checked")
If Not (IsNothing(objChecked)) Then
Return objChecked
End If
End Get
End Property
End Class
But then I basically found out that you can't override a protected member with a public one. I've had very little experience with creating new classes etc, so I'm stumped.
So if anyone can tell me how to go about checking the darned checkbox, I'd be eternally grateful! I've spent a full working day trying to solve the problem, but with no luck!
Thanks in advance!
For static controls, the view state of controls is restored in Page_Init, which happens before Page_Load, so they contain the correct values in Page_Load. Dynamic controls are created in Page_Load, so their viewstate is incorrect in Page_Load and will be restored after Page_Load, but before calling event handlers. MSDN says:
After the Page_Load event has run, but before control event-handling methods are called, the remaining view state information is loaded into the dynamically created controls.
This is why Checked returns false, and why changing the visibility of CheckBox.ViewState will not solve your problem.
Solution (untested, but I think it should work): Create all controls (even those that you don't want to display) in Page_Load. Attach an event handler on CheckedChanged to your CheckBoxes (in Page_Load). After Page_Load has finished, ASP.NET will restore the view state of the newly created controls (that's why it is important to create the same controls in Page_Load, so that ASP.NET can correctly map the view state to the control), and ASP.NET will call your event handler for those CheckBoxes that have been changed. There, you can remove the rows you don't want to display.
This is how you add the event handler
AddHandler objCheckbox.CheckedChanged, AddressOf MyCheckedChangedFunction
This function would look something like this:
Function MyCheckedChangedFunction(sender As Object, e As EventArgs)
Dim objCheckbox = DirectCast(sender, CheckBox)
... access objCheckbox.Changed and do something useful here ...
End Function