I am having an issue with my usercontrol being loaded too late in the "post back timeline", because they are being loaded in as a result of a custom event.
As a result the button click events on this usercontrol don't fire on the first click (the entire post-back occurs only the event-handlers for the click don't get raised). On the second click (and hence second post-back), however, the event-handlers work fine.
How can I invoke a second post-back automatically right after one has just finished? So my usercontrol gets loaded correctly.
Default Page
public interface IEventProvider
{
void TriggerEvent(String path);
}
public partial class Default : System.Web.UI.Page, IEventProvider
{
private string LastLoadedControl
{
get
{
return Session[Paths.CURRENTCTRL] as string;
}
set
{
Session[Paths.CURRENTCTRL] = value;
}
}
private void LoadUserControl()
{
string controlPath = LastLoadedControl;
ContentPlaceholder.Controls.Clear();
if (string.IsNullOrEmpty(controlPath))
controlPath = Utils.Paths.USERCTRL_BASE + "Main.ascx";
Control uc = Page.LoadControl(controlPath);
ContentPlaceholder.Controls.Add(uc);
}
protected void Page_Load(object sender, EventArgs e)
{
LoadUserControl();
}
public void TriggerEvent(String path)
{
if (path.Equals("logout"))
{
Session.Clear();
Session.Abandon();
LastLoadedControl = null;
}
else LastLoadedControl = Paths.USERCTRL_BASE + path + ".ascx";
LoadUserControl();
}
}
Usercontrol code
protected void profileBtn_Click(object sender, EventArgs e)
{
Utils.Events.triggerRedirectPage(this.Page, "Login");
}
events code
public static void triggerRedirectPage(Page p, String path)
{
IEventProvider eventProvider = p as IEventProvider;
if (eventProvider != null)
eventProvider.TriggerEvent(path);
}
You can add a button (or another control) with AllowPostBack=true then trigger click event on this button.
Related
I have a cs file for Drag drop Element. In the Method after the drop is completed (OnManipulationCompleted) I would like to initiate/trigger the 3 button clicks from another WPF page which has 3 buttons within the OnManipulationCompleted method.
namespace KinectDemos
{
public class DragDropElementController : IKinectManipulatableController
{
private ManipulatableModel _inputModel;
private KinectRegion _kinectRegion;
private DragDropElement _dragDropElement;
private bool _disposedValue;
public DragDropElementController(IInputModel inputModel, KinectRegion kinectRegion)
{
_inputModel = inputModel as ManipulatableModel;
_kinectRegion = kinectRegion;
_dragDropElement = _inputModel.Element as DragDropElement;
_inputModel.ManipulationStarted += OnManipulationStarted;
_inputModel.ManipulationUpdated += OnManipulationUpdated;
_inputModel.ManipulationCompleted += OnManipulationCompleted;
}
private void OnManipulationCompleted(object sender,
KinectManipulationCompletedEventArgs kinectManipulationCompletedEventArgs)
{**HERE I WOULD LIKE TO INITIATE THE BUTTON CLICKS**
}
The Other Wpf page which has these buttons having function for three buttons .After each button drop it would navigate to another page.
Public partial class Beauty : Usercontrol
{
Public void Tip_Click (object sender, RoutedEventArgs e)
{
Afterdrop page1 = new Afterdrop
this.content = page1;
}
Public void Tricks_Click (object sender, RoutedEventArgs e)
{
Afterdrop2 page2 = new Afterdrop2
this.content = page2;
}
Public void Invent_Click (object sender, RoutedEventArgs e)
{
Afterdrop3 page3 = new Afterdrop3
this.content = page3;
}
}
How will I do that ?Please help
You need to have an instance of the page you want to trigger events on. If that page exists then you can get that instance based on your application architecture otherwise there is no way to call non-static members without instantiating an object.
Once you get that page instance call the respective event handlers as methods. As an example if that instance is named targetPage then
targetPage.Tip_Click(null, new EventArgs());
I have an issue with a custom event i have created. I have made a Usercontrol that looks the following:
public partial class UCListView : UserControl {
public UCListView() {
InitializeComponent();
}
public event EventHandler SubmitClick;
public event EventHandler MouseButtonUpEvent;
private void SubmitButton_OnClick(object sender, RoutedEventArgs e) {
if (SubmitClick != null)
SubmitClick(this, e);
}
private void MouseButtonUp(object sender, RoutedEventArgs e) {
if (MouseButtonUpEvent != null) {
MouseButtonUpEvent(this, e);
}
}
}
Here is the MouseButtonUp event i have.
The following is where i listen to the event:
public partial class RoundsteelWindow : WindowControls {
private UCListView uc;
public RoundsteelWindow() {
InitializeComponent();
uc = new UCListView();
uc.SubmitClick += new EventHandler(ButtonPressed);
uc.MouseButtonUpEvent += new EventHandler(MousePressed);
stkTest.Children.Add(uc);
base.Test<RoundSteel>(uc, "Roundsteel");
}
}
Here is the WindowControls, where the MousePressed method can be seen. This is the same as the code snippet beneath this code. Really don't see the issue:
public abstract class WindowControls : Window {
public IMaterialWith14Elements _ReturnObject { get; set; }
public double amount { get; set; }
private UCListView _uc;
public void Test<T>(UCListView uc, string type) where T: IMaterialWith14Elements, new() {
_uc = uc;
List<T> test = MaterialLogic.GetList(type) as List<T>;
foreach (T material in test) {
uc.listView.Items.Add(material.Name);
}
}
private string str;
public void MousePressed(object sender, EventArgs eventArgs) {
var item = (sender as ListView).SelectedItem;
if (item != null) {
_ReturnObject = _uc.listView.SelectedItems as FlatSteel ;
str = item.ToString();
_uc.amountText.IsEnabled = true;
}
}
public void ButtonPressed(object sender, EventArgs e) {
if (!string.IsNullOrEmpty(_uc.amountText.Text)) {
amount = _uc.amountText.Text.customParseToDouble();
this.Close();
}
else {
MessageBox.Show("Indtast venligst en værdi.");
}
}
}
Now the problem is the following: With the following code it is working, but this class is not using the windowcontrols. It is called by another class which handles all of the buttons.
private void flatsteelListView_PreviewMouseLeftButtonUp(object sender, RoutedEventArgs e) {
var item = (sender as ListView).SelectedItem;
if (item != null) {
_returnObject = flatsteelListView.SelectedItems as FlatSteel;
str = item.ToString();
amountTextbox.IsEnabled = true;
FindObject(str);
}
}
The first picture shows the working window. This is where there is not used a usercontrol. Actually this is a previous issue i have worked with and got help with here on stackoverflow Help for thisissue.
The second picture is showing the next window using the usercontrol that has been created. The button event works and closes the window. Here comes then the issue, when the listview item is pressed. It is doing the same thing as on the first picture(where it works), but it is giving me a null reference, which doesn't make any sense to me. I have also checked the object sender to see if there was a difference between the sender of these two different windows.
I simply can't figure out why this is not working.
greetings darophi
Your sender is an object of UCListView class which is inherited from UserControl and you are trying to use it like ListView. So as result of operation (sender as ListView) you get null because sender is not an instance of ListView class and not inherits it.
I have a radio list in my master page and they fire an event when I select one of them.
Now this event isn't not controlled by my master page instead it is controlled by my content page.
My question is, is it possible to pass int/Strings from the master page method to content page method somehow.
P.S i want to pass the int i to content page method in this case
This is how i tide them up.
Master page Code to handle event
public event EventHandler Master_Save;
...
public void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
int i=RadioButtonList1.SelectedIndex;
if(Master_Save!=null)
{ Master_Save(this, EventArgs.Empty); }
}
and my content page code to handle the event
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
(this.Page.Master as Pages_MasterPage).Master_Save += new EventHandler(ContentPage_Save);
}
private void ContentPage_Save(object sender, EventArgs e)
{
//Code that changes a query
}
Sure you can, just define a custom event args class and parametrize your event with it:
public class MasterSaveEventArgs : EventArgs
{
public int Index { get; private set; }
public MasterSaveEventArgs(int index)
{
this.Index = index;
}
}
And then just use it:
public event EventHandler<MasterSaveEventArgs> Master_Save;
...
{ Master_Save(this, new MasterSaveEventArgs(i)); }
...
(this.Page.Master as Pages_MasterPage).Master_Save += ContentPage_Save;
// notice the shortened syntax here
...
private void ContentPage_Save(object sender, MasterSaveEventArgs e)
I've followed this question and tried to build my solution. The problem is that 'UserControlButtonClicked' appears to be null! So 'UserControlButtonClicked(this, EventArgs.Empty)' inside the if, doesn't run, and the method 'addStepContent' in the parent page is never called.
UserControl 'StepsBar'
public sealed partial class StepsBar : UserControl
{
public event EventHandler UserControlAddStepContent;
[...]
public StepsBar()
{
this.InitializeComponent();
Image step_1 = new Image();
ButtonInfo step_1Info = new ButtonInfo();
step_1Info.Add((int)stepNumber.one, (int)stepStatus.normal);
step_1.Tag = step_1Info;
step_1.Source = setBackground((int)stepStatus.normal);
step_1.Tapped += stepTapped;
[...]
}
public void stepTapped(Object sender, RoutedEventArgs e)
{
[...]
if (step != null)
{
[...]
firePageEvent();
}
}
public void firePageEvent()
{
if (UserControlAddStepContent != null)
{
UserControlAddStepContent(this, EventArgs.Empty);
}
}
Parent Page
public Violation()
{
this.InitializeComponent();
StepsBar stepsBar = new StepsBar();
stepsBar.UserControlAddStepContent += new EventHandler(addStepContent);
}
private void addStepContent(object sender, EventArgs e)
{
CheckBox check_1 = new CheckBox();
check_1.Content = "Check me!";
bodyStackPanel.Children.Add(check_1);
}
This assumes that you want to use an existing delegate rather than make your own and you aren't passing anything specific to the parent page by event args.
In the user control's code-behind (adapt as necessary if not using code-behind or C#):
public partial class MyUserControl : System.Web.UI.UserControl
{
public event EventHandler UserControlButtonClicked;
private void OnUserControlButtonClick()
{
if (UserControlButtonClicked != null)
{
UserControlButtonClicked(this, EventArgs.Empty);
}
}
protected void TheButton_Click(object sender, EventArgs e)
{
// .... do stuff then fire off the event
OnUserControlButtonClick();
}
// .... other code for the user control beyond this point
}
In the page itself you subscribe to the event with something like this:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// hook up event handler for exposed user control event
MyUserControl.UserControlButtonClicked += new
EventHandler(MyUserControl_UserControlButtonClicked);
}
private void MyUserControl_UserControlButtonClicked(object sender, EventArgs e)
{
// ... do something when event is fired
}
}
Solved. The problem was this, on the parent page.
StepsBar stepsBar = new StepsBar();
stepsBar.UserControlAddStepContent += new EventHandler(addStepContent);
The istance of StepsBar was not added to the page. D'OH!
So here's what I've done:
stepsBar.UserControlAddStepContent += new EventHandler(addStepContent);
and on the xaml of the parent page:
<local:StepsBar x:Name="stepsBar"/>
I've created a user control that contains a button and a few other controls.
When declaring my user control in the html markup I'd like to do some sort of :
<asp:CustomControl onclick="CustomControl_Click" ID="cc1" runat="server">
Where CustomControl_Click is obviously the action I want to call when my control's button is clicked.
So far in my control I have:
public event EventHandler Click;
protected void Button1_Click(object sender, EventArgs e)
{
Click.Invoke(sender, e);
}
but how can I forward the Eventhandler of the parent page to assign it to the Click Eventhandler in my control?
Any help is really appreciated!
PS: maybe there's a way of getting the method from the hosting page using reflexion
I'm using a custom button (actually html div with LinkButton embedded in it). Here is code of it:
public delegate void ClickEventHandler(object sender, EventArgs e);
public event ClickEventHandler Click = delegate { };
public string Text
{
get { return cmdLink.Text; }
set { cmdLink.Text = value; }
}
public bool CausesValidation
{
get { return cmdLink.CausesValidation; }
set { cmdLink.CausesValidation = value; }
}
public string OnClientClick
{
get { return cmdLink.OnClientClick; }
set { cmdLink.OnClientClick = value; }
}
public string CssClass
{
get { return cmdLink.CssClass; }
set { cmdLink.CssClass = value; }
}
protected void cmdLink_Click(object sender, EventArgs e)
{
Click(this, e);
}
Here is usage in aspx page:
<Button_Control:ButtonControl ID="btnSave" runat="server" Text="Save"
OnClick="btnSaveClick" />
and this is in code-behind page of aspx page:
protected void btnSaveClick(object sender, EventArgs e)
{
//do stuff here
}
I found it!
Instead of using event bubbling I use reflexion in the click eventhandler of the user control so I have :
public string OnClick;
protected void Button1_Click(object sender, EventArgs e)
{
MethodInfo methodInfo = this.Page.GetType().GetMethod(OnClick);
methodInfo.Invoke(this.Page, new object[]{ sender, e });
}
And it works with declaring the user control as :
<asp:CustomControl OnClick="CustomControl_Click" ID="cc1" runat="server">