HierarchicalDataBoundControl.PerformDataBinding not being called on postback - hierarchical-data

I am binding to a SiteMapDataSource (hierarchical).
I am overriding PerformDataBinding to grab the data from the datasource.
Everything works great on page load. But when I perform a postback anywhere on the page, the PerformDataBinding method does not get called, and in effect, not rendering any menu items (PerformDataBinding wasn't called).

No clue why this is happening, but I have a fix for it. Amazingly, every example of a HierarchicalDataBoundControl I could find (even from msdn) was doing this. However, here is a workaround.
private bool dataBound = false;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (this.Page.IsPostBack)
{
this.DataBound += delegate { dataBound = true; };
this.Page.Load += delegate { if (!dataBound) DataBind(); };
}
}

Related

ActionScript3 How to increment score when working with classes

I would like to increment the value of a TextField by one every time an event listener in another class is activated. This is proving very difficult because the event listener can only be triggered once the Timer in the main class reaches completion.
I have tried using a getter method on a Boolean to ensure that the event listener in a different class has been triggered. This has proven unsucsessful however because of the Timer which is only triggered once on completion.
I am a begginer to AS3 and so any help would be greately appreciated. Thank You
THIS IS THE CODE FOR THE MAIN CLASS
public function t1Finish(e:TimerEvent):void {
ranNum2 = randomNum1(0, 11);
holes[ranNum2].sendUp();
setCounter();
}
public function setCounter():void {
if(holes[ranNum2].returnHasHit() == true) {
counter1++;
txt1instance.text = counter1.toString();
} else {
txt1instance.text = counter1.toString();
}
}
THIS IS THE CODE FOR THE OTHER CLASS
public function click1(e:MouseEvent):void {
this.gotoAndPlay(32);
hasHit = true;
}
public function returnHasHit():Boolean {
return hasHit;
}
You need to properly arrange your app hierarchy. Your case is not exactly problematic: you have main class that wants to capture some events occurring in certain subservient objects.
// That HOLE class.
// I assume it is an inner event handler, not a public
// interface, thus there's no need for it to be public.
private function click1(e:MouseEvent):void
{
// Normally you don't need to explicitly state "this".
gotoAndPlay(32);
// Produce a custom event.
dispatchEvent(new Event(Event.OPEN));
}
Then, you need to catch that event in the main class.
// The MAIN class.
// You need to subscribe for that custom event somewhere.
private function subscribeAll():void
{
for each (var aHole:MovieClip in holes)
{
aHole.addEventListener(Event.OPEN, onOpen);
}
}
// The custom event handler.
private function onOpen(e:Event):void
{
// If you want to know which hole dispatched this event.
var aHole:MovieClip = e.target as MovieClip;
// Here you might want to put any additional criteria
// to check if you want to accept this event.
// This handler is invoked ONLY if that "click1" handler was invoked
// on any of the holes, so there's no need to check "wasHit" interface.
counter1++;
txt1instance.text = counter1.toString();
}

UWP Custom ListView to scroll down

So, I have a listview and I want it whenever an item is created to scroll to that item (bottom). Because I am using MVVM I found really nice explanation on how to make a new control that inherits from listview that scrolls down. The problem is that this answer (the third) is referring to WPF 6 years ago.
I am making a UWP app, so I copied the code and tried to format it to my needs. The following code doesn't give any error or exception but instead it loads the "ChatListView" as I call it perfectly and then does nothing. The comments are only a bit edited compared to the original code.
What can I do ? Thank you in advance!
public class ChatListView : ListView
{
//Define the AutoScroll property. If enabled, causes the ListBox to scroll to
//the last item whenever a new item is added.
public static readonly DependencyProperty AutoScrollProperty =
DependencyProperty.Register(
"AutoScroll",
typeof(Boolean),
typeof(ChatListView),
new PropertyMetadata(
true, //Default value.
new PropertyChangedCallback(AutoScroll_PropertyChanged)));
//Gets or sets whether or not the list should scroll to the last item
//when a new item is added.
public bool AutoScroll
{
get { return (bool)GetValue(AutoScrollProperty); }
set { SetValue(AutoScrollProperty, value); }
}
//Event handler for when the AutoScroll property is changed.
//This delegates the call to SubscribeToAutoScroll_ItemsCollectionChanged().
//d = The DependencyObject whose property was changed.</param>
//e = Change event args.</param>
private static void AutoScroll_PropertyChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
SubscribeToAutoScroll_ItemsCollectionChanged(
(ChatListView)d,
(bool)e.NewValue);
}
//Subscribes to the list items' collection changed event if AutoScroll is enabled.
//Otherwise, it unsubscribes from that event.
//For this to work, the underlying list must implement INotifyCollectionChanged.
//
//(This function was only creative for brevity)
//listBox = The list box containing the items collection.
//subscribe = Subscribe to the collection changed event?
private static void SubscribeToAutoScroll_ItemsCollectionChanged(
ChatListView listView, bool subscribe)
{
INotifyCollectionChanged notifyCollection =
listView as INotifyCollectionChanged;
if (notifyCollection != null)
{
if (subscribe)
{
//AutoScroll is turned on, subscribe to collection changed events.
notifyCollection.CollectionChanged +=
listView.AutoScroll_ItemsCollectionChanged;
}
else
{
//AutoScroll is turned off, unsubscribe from collection changed events.
notifyCollection.CollectionChanged -=
listView.AutoScroll_ItemsCollectionChanged;
}
}
}
//Event handler called only when the ItemCollection changes
//and if AutoScroll is enabled.
//sender = The ItemCollection.
//e = Change event args.
private void AutoScroll_ItemsCollectionChanged(
object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
int count = Items.Count;
ScrollIntoView(Items[count - 1]);
}
}
//Constructor a new ChatListView.
public ChatListView()
{
//Subscribe to the AutoScroll property's items collection
//changed handler by default if AutoScroll is enabled by default.
SubscribeToAutoScroll_ItemsCollectionChanged(
this, (bool)AutoScrollProperty.GetMetadata(typeof(ChatListView)).DefaultValue);
}
}
If you want to create a chat application you can use the ItemsStackPanel's ItemsUpdatingScrollMode particular property to KeepLastItemInView value to scroll to the latest item.
Usage:
<ListView>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel ItemsUpdatingScrollMode="KeepLastItemInView" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
Note: KeepLastItemInView enum member was introduced in the 14393 SDK.
Related link:
https://learn.microsoft.com/en-us/uwp/api/Windows.UI.Xaml.Controls.ItemsStackPanel#properties_
The accepted answer is pretty nice. However I there is one thing it won't do (at least if I simply copy and paste the above XAML): it won't do its intended scrolling if, say, the user was away from that page while new items were added, and then they navigated to the page.
For that I had to hook into
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (MyListView.Items.Count == 0)
return;
object lastItem = MyListView.Items[MyListView.Items.Count - 1];
MyListView.ScrollIntoView(lastItem);
}

Ignore multiple button taps after first one on iPhone webapp using jQuery Mobile?

Assume button A in an HTML5 webapp built with jQuery Mobile.
If someone taps button A, we call foo(). Foo() should get called once even if the user double taps button A.
We tried using event.preventDefault(), but that didn't stop the second tap from invoking foo(). event.stopImmediatePropagation() might work, but it also stops other methods further up the stack and may not lead to clean code maintenance.
Other suggestions? Maintaining a tracking variable seems like an awfully ugly solution and is undesirable.
You can set a flag and check if it's OK to run the foo() function or unbind the event for the time you don't want the user to be able to use it and then re-bind the event handler after a delay (just a couple options).
Here's what I would do. I would use a timeout to exclude the subsequent events:
$(document).delegate('#my-page-id', 'pageinit', function () {
//setup a flag to determine if it's OK to run the event handler
var okFlag = true;
//bind event handler to the element in question for the `click` event
$('#my-button-id').bind('click', function () {
//check to see if the flag is set to `true`, do nothing if it's not
if (okFlag) {
//set the flag to `false` so the event handler will be disabled until the timeout resolves
okFlag = false;
//set a timeout to set the flag back to `true` which enables the event handler once again
//you can change the delay for the timeout to whatever you may need, note that units are in milliseconds
setTimeout(function () {
okFlag = true;
}, 300);
//and now, finally, run your original event handler
foo();
}
});
});
I've created a sample here http://jsfiddle.net/kiliman/kH924/
If you're using <a data-role="button"> type buttons, there is no 'disabled' status, but you can add the appropriate class to give it the disabled look.
In your event handler, check to see if the button has the ui-disabled class, and if so, you can return right away. If it doesn't, add the ui-disabled class, then call foo()
If you want to re-enable the button, simply remove the class.
$(function() {
$('#page').bind('pageinit', function(e, data) {
// initialize page
$('#dofoo').click(function() {
var $btn = $(this),
isDisabled = $btn.hasClass('ui-disabled');
if (isDisabled) {
e.preventDefault();
return;
}
$btn.addClass('ui-disabled');
foo();
});
});
function foo() {
alert('I did foo');
}
});

GWT/MVP: detecting change events in a table with proper MVP pattern

We're using gwt-presenter, but not really a question specific to that...
I've got a table with users in it. As I build the table in the view (from the data provided by the presenter), I need to add two action buttons ("Edit", and "Delete") at the end of the row.
What's the best way to assign click handlers to these buttons so the presenter knows which was clicked? Previous to this, we could pass a private field from the view to the presenter and attach a discrete click handler to that button. However, this method is rather rigid and doesn't work in this scenario very well.
Thanks in advance.
How about having the view allowing the subscription for edit/delete click events, registering internally the individual row click events, and then delegating the event handling to the ones registered by the view?
I mean something like the following pesudo code:
View:
addRowEditClickHandler(ClickHandler handler) {
this.rowEditClickHandler = handler;
}
addRowDeleteClickHandler(ClickHandler handler) {
this.rowDeleteClickHandler = handler;
}
//... somewhere when setting up of the grid...
rowEditButton.addClickHandler = new ClickHandler() {
onClick(args) {
this.rowEditClickHandler.onClick(args)
}
rowDeleteButton.addClickHandler = new ClickHandler() {
onClick(args) {
this.rowDeleteClickHandler.onClick(args)
}
Presenter:
View view = new View();
view.addRowEditClickHandler( new ClickHandler() {
onClick(args) {
doSomething();
}
});
view.addRowDeleteClickHandler( new ClickHandler() {
onClick(args) {
doSomething();
}
});

How to close a ViewPart in Eclipse?

I have a view in Eclipse (implemented by a class which extends org.eclipse.ui.part.ViewPart) which I need to close. I mean completely close, not just hide. I want a new ViewPart instance to be created when the user (or my code) asks to open the view again.
The only method I found was IWorkbenchPage.hideView which hides the view, but does not completely dispose of it. Invoking dispose on the view has no affect, either.
BTW, my view is defined as allowMultiple="false" but I tried with true and that didn't make any difference.
Any help will be appreciated.
I found the problem eventually. If the view is open on more than one perspective, hiding it on one perspective will not close it. It is possible to iterate over all the open perspective and look for the view. Hiding it on all perspectives will close it.
I think the IWorkbenchPage.hideView() method you mentioned is the only one available to programmaticaly close a view. I also think this method name should be closeView() beacause it really close the view.
I have been using this method for a while (with allowMultiple=true views) and after debugging it seems my view.dispose() method is invoked each time I invoke hideView().
Next time I open this view again (I mean from my code and not from user interface), a new one is created by Eclipse and the createPartControl() method is invoked again.
Moreover, the call hierarchy view told me than the hideView() should call the dispose method() ....
hideView() >> releaseView() >> partRemoved() >> disposePart() >> dispose() >> doDisposePart() >> doDisposePart() >> dispose()
Hope this can help ....
One last question, how did you checked that your view was not correctly disposed ??
The org.eclipse.ui.internal.ViewFactory has a method called releaseView that I think closes the view completely (though I'm not certain). It takes an IViewReference.
You can access the ViewFactory by calling Perspective.getViewFactory, and you can access the Perspective, you then pass it an IViewReference to release the view.
IWorkbenchPage page =
Workbench.getInstance().getActiveWorkbenchWindow().getActivePage()
Perspective perspective = page.getPerspective();
String viewId = "myViewId"; //defined by you
//get the reference for your viewId
IViewReference ref = page.findViewReference(viewId);
//release the view
perspective.getViewFactory.releaseView(ref);
I overridden dispose method from IWorkbenchPart and that worked.
I had something like this in my overridden dispose method:
public void dispose() {
super.dispose();
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
if (page != null) {
IViewReference[] viewReferences = page.getViewReferences();
for (IViewReference ivr : viewReferences) {
if (ivr.getId().equalsIgnoreCase("your view id")
|| ivr.getId().equalsIgnoreCase("more view id if you want to close more than one at a time")) {
page.hideView(ivr);
}
}
}
}
In order to dispose ViewPart on closing Perspective we used the next code:
IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (workbenchWindow != null) {
workbenchWindow.addPerspectiveListener(new PerspectiveAdapter() {
#Override
public void perspectiveActivated(IWorkbenchPage page,
IPerspectiveDescriptor perspectiveDescriptor) {
super.perspectiveActivated(page, perspectiveDescriptor);
}
#Override
public void perspectiveDeactivated(IWorkbenchPage page,
IPerspectiveDescriptor perspective) {
super.perspectiveDeactivated(page, perspective);
page.closePerspective(perspective, false, true);
}
});
}
In result of page.closePerspective(perspective, false, true);, ViewPart that was opened within perspective, will be disposed.
To close views, opened in different perspective, I overridden perspectiveDeactivated() of org.eclipse.ui.PerspectiveAdapter.
public void perspectiveDeactivated(IWorkbenchPage page,
IPerspectiveDescriptor perspective) {
super.perspectiveDeactivated(page, perspective);
boolean myPerspective = MyPerspective.PERSPECTIVE_ID.equals(perspective.getId());
if(!myPerspective) {
//close Error Log view if it is opened in any perspective except My perspective.
IViewPart errorView = page.findView("org.eclipse.pde.runtime.LogView");
if(errorView != null) {
page.hideView(errorView);
}
}
}
My requirement was to close "Error Log" view. Above code can be modified to close any view.