I have a TabView inside of my ContentPage and to keep things clean I usually put my tabs in a ContentView
My ContentView in this example has a few Entry controls. I want to subscribe to the TextChanged event for the control so I can display a character counter label on the page.
Now I'm not sure if this is correct or not but I keep these UI functions in the code behind as to my knowledge they have no place in the VM.
The issue is; in the ContentPage there is no way to get a reference to the control when its buried in a ContentView and if I try to move the logic to the ContentView there is no access to the usual OnAppearing & OnDisappearing methods. I'm using MVVMCross and at the moment I'm subscribing to the event in the OnBindingContextChanged method. However, I cant see anything that gets triggered when the page is closed / popped.
I was over thinking this as usual.
What I was wanting to achieve was to simply update a Label when the text in an Entry changed. I keep my UI specific code in the XAML and code behind but I was having issues in un/subscribing to the TextChanged events in the code behind.
I don't know why I don't think of this before; just wire the TextChanged event in the XAML instead of doing it in the code behind and that way the unsubscribe is already taken care of.
Related
I have a datalist and want to dynamically add buttons to it. I am using the OnItemCommand datalist event and setting the CommandName/ CommandArgument attributes of the button.
However I am having trouble with handling the button click - does not seem to fire.
It works when I declared a button on the aspx page, but not for buttons that are dynamically created.
I hope this makes sense, and any help would be great.
Thanks
You can only create dynamic controls on PreInit or Init if you want to handle associated events. Otherwise, on postback, they won't exist at the moment of event handling and because of that, your handler method won't be called.
Internet is full of resources about how to handle dynamic controls. Let me know if you need any reference.
I was going through an article on event bubbling in asp.net and came to know that although it is possible to subscribe to the click event of a user control's button from the containing page, "doing so would break some of the object oriented rules of encapsulation". A better idea is to publish an event in the user control to allow any interested parties to handle the event.
My question is that exactly how does a direct subscription to the button's click event from a containing page would break the object oriented rules of encapsulation?
Apologies if its a dumb question. :|
Thanks!
The Button is supposed to be encapsulated by the UserControl.
If the Page binds directly to events on the button, then the page is now dependent on the inner workings of the UserControl.
The Page should be consuming the UserControl, not the UserControl's button. If the author of the UserControl later wants to remove the button and use some fancy new method of firing its "Submit" event, your page could be broken because the button may no longer exist.
For that matter, if the owner of the UserControl decides in v1.1 to rename the button from btnSubmit to SubmissionButton, it could break your page, as well.
Better to consume the UserControl and let it be concerned with its own inner workings.
The idea is that the button of the control is an implementation detail of the UI of the control. If you republish the click event you could reimplement that button as an ImageButton, LinkButton, etc.
I think it's OK to attach an event handler at the page level to the button if the button is a permanent fixture of the UI. It saves a lot of event code, especially with a lot of buttons.
So i've got a custom user control. I have an event (SelectionChanged) and I'm wanting to have who ever uses my control to do the following to hook up the event:
drag the control to the page
in designer mode, click on the control
view the controls event handlers (from the properties window)
find the SelectionChanged event
double click and let visual studio create the code behind function and the wire-up on the aspx page.
How do i get this done? I've got the control setup so that the user can manually type in the event wire-up and code behind event by hand, but i want Visual Studio to do this.
Currently, when the a developer has dropped my control on the page, they can click on it and see the properties but no events are available (the lightning bolt isn't even there).
My events are public. Here they are:
public delegate void SelectionChangedDelegate(object sender, SelectionChangedEventArgs e);
public event SelectionChangedDelegate SelectionChangedEvent;
I don't understand how the lightning bolt isn't there. Is your control inheriting from System.Web.UI.HtmlControls.HtmlControl or System.Web.UI.Control or another derive control?
If you are inheriting from one of these controls, you should see the inherited events in the properties window.
The fact the lightning bolt isn't there leads me to believe that you aren't inheriting from one of the the control classes.
EDIT:
Well, you're not going to like this. UserControl doesn't behave the same as a WebControl. And as such, the VS Editor doesn't wireup the events.
If the Event Wireup is critcal for you (If this is going to be a sold library) I would recommend that you rewrite the control as a WebControl. This will require you to add all the controls programatically in the CreateChildControls override method.
You might also be able to wrap your UserControl inside of a WebControl and bubble up all the events that way.
Best of luck!
Been pulling my hair out and doing a bit of looking on the web to try and figure out an elegant solution to my issue.
I have a ProductImages.aspx page. It shows all of the images associated with that product in a dynamically created list. Events are wired up to each picture to allow you to update it.
This works fine.
However, I have an option at the end which lets me add a new image. This is a button which fires off a call to the AddImage method.
Now what is happening is that the original controls are being create and added to the page with events. Then the button event if fired which recreates all of the existing image controls and a new one. Add this point the new image control create after the OnInit does not have events attached due to the events being added AFTER the OnInit.
I can do a Response.Redirect to reload the page and fire the OnInit to wire up the events again but this seems very inelegant and destroys the point of using update Panels.
Any ideas?
I'm thinking you could always load the picture upload control in a div and have a Javascript link to toggle the display attribute of the div.
Or perhaps use CollapsiblePanels from the AjaxToolKit to hide and show the upload form.
I think either of those ways would be more elegant than doing a post back (even if it's in an UpdatePanel) just to retrieve the picture upload form.
Your questions makes it sound like you're saying that you can't put the controls in OnInit because it is only fired on the first load of the page. This is not the case - OnInit is fired each time the page is loaded (including postbacks), so you can re-create your controls there even when using an update panel.
One property that is different between the initial load and the postbacks is the Page.IsPostback property, which you can use to just perform actions on the first load of the page.
I have a ListView on a page that displays a list of widgets. When a user clicks on one of the items in the list, I want to display a ModalPopup that contains controls allowing the user to operate on the item they selected.
I could easily accomplish this by placing a Panel and a ModalPopupExtender in the ListView's ItemTemplate, but this mean one set of hidden controls for each and every widget, which would massively bloat the page size. (There are going to be some rather heavyweight controls in there.) Instead I want to reuse a single ModalPopup for each of the widgets in the list.
I've done some searching but I haven't found anything that applies directly to my situation before. From what I've been able to figure out, however, I have to do something like this:
Place a Panel and a ModalPopupExtender on the page inside an UpdatePanel.
Build a custom WidgetManipulator user control that has a WidgetID property. Put this in the Panel, along with a couple OK/Cancel buttons.
In Javascript on the page, attach a click handler to each widget in the ListView that triggers a postback on the UpdatePanel.
On the UpdatePanel_Load event on the server, display the ModalPopup and then set the WidgetID propety on the WidgetManipulator to the ID of the clicked widget.
On the OKButton_Click event or CancelButton_Click event on the server, hide the ModalPopup. If OKButton was clicked, call WidgetManipulator.SaveChanges() first.
The part I haven't figured out is: How the heck do I know what widget was clicked on, and how do I pass that back to the server when I refresh the UpdatePanel? Is this even the right approach at all?
If you can use jQuery instead you could do something along the lines of these two posts:
Modal Delete Confirmation Version
Two Using jQuery SimpleModal Plugin
Demo
Inserting Content Using
jQuery SimpleModal Plugin Demo
When I need to pass data from client to server in ASP.NET AJAX, I generally use an asp:HiddenField with runat="server". Both can see it freely, but beware potential postback asynchronicity.
Sounds like you need to notify the server the widget was clicked - You may use a Timer to postback; or I'd go with option 5.