i have a aspx page on it i'm dynamically adding the web user controls in a placeholder. and know i have cancel button on the one user control and on click of that Cancel button i want to load other user control inside the aspx page (placeholder).
how can i do that, if i create an event handler in usercontrol then that become null.
thanks
A user control should never, ever have any kind of dependency to the page where it lives. Add an event to the control and fire it from the Cancel button event handler.
public event EventHandler CancelClicked;
protected Cancel_Click(object sender, EventArgs e)
{
if(CancelClicked != null)
{
CancelClicked(this, e);
}
}
From your page, subscribe to the controls's CancelClicked event and do whatever operation should be done on that other user control.
I believe the best way to achieve this would to create an event in the user control that is fired when you need it to communicate, then in the page you can subscribe to this event.
Your User Control
Put this in your control as an event variable.
public event EventHandler CancelRequested;
Then in the cancel button click event :
CancelRequested(this, new CommandEventArgs("CancelClicked", SomeVariable));
Your Page (or parent)
Then subscribe to the event in the parent page like this (when you dynamically add it in):
ctrlYourControl.CancelRequested += new EventHandler(ctrlYourControl_CancelRequested);
Declare the event handler :
void ctrlYourControl_CancelRequested(object sender, EventArgs e)
{
// display other user control
}
Add this event to your user controls:
public event EventHandler CancelClicked;
Now, this method on your parent aspx page.
public void OnCancelClicked (object sender, EventArgs data)
{
// do your stuffs like loading other controls and
// whatever when event fired
}
When you are loading your user control dynamically, add this:
var someControl = LoadControl(#"~\SomeControl.ascx") as SomeControl;
someControl.CancelClicked += new EventHandler(OnCancelClicked);
And on your user controls:
public void btnCancel_Click(object sender, EventArgs e)
{
if(CancelClicked != null)
{
CancelClicked(this, e);
}
}
This should do !
Related
I have a winforms app in vs2010 and a panel whose click event I would like to fire programatically. How do I do this? Button has a PerformClick but I cannot find the same in Panel.
Your panel's Click event is going to be attached to an event handler, right?
Then just call that event handler from the button's click event handler:
public void Panel1_Click(object sender, EventArgs e)
{
//Do whatever you need to do
}
public void Button1_Click(object sender, EventArgs e)
{
//Do anything you need to do first
Panel1_Click(Panel1, EventArgs.Empty);
}
The effect will be the same as clicking on the panel.
I have a user control named myControl. I have rendered another user control named dialog inside myControl as <uc:dialog x:Name="dialog"> .
I have a button named as myButton inside the dialog user control. I need to get the lostfocus event of myButton from the parent level .ie,myControl code behind.How can I get that? Which is the best way to do that?
var myButton= dialog.FindName("myButton") as Button;
if (myButton!= null)
{
myButton.LostFocus += myButton;
}
I tried like this. But it doesn't work.Why?
You could define such an Event in your UserControl.
Then just register for this event.
public event EventHandler ButtonLostFocusEvent;
private void Button_LostFocus(object sender, RoutedEventArgs e)
{
EventHandler testEvent = this.ButtonLostFocusEvent;
// Check for no subscribers
if (testEvent == null)
return;
testEvent(sender, e);
}
I have a Main page that contains a listBox.
When a user selects a profile form the list box, this opens up a child window called pWindow.
This window as the option to delete the current profile via a hyperlink button that opens up a another confirmation window called dprofile.
My question being is it possible that once a user has confirmed to delete the current profile they are in, and confirmed it in the button click on dProfile, how can I update the listBox in the first Main page so that the list no longer contains the deleted profile (which it is not doing at present.
In the dProfile window I have created an event -
public event EventHandler SubmitClicked;
Where in the OK button click I have-
private void OKButton_Click(object sender, RoutedEventArgs e)
{
if (SubmitClicked != null)
{
SubmitClicked(this, new EventArgs());
}
}
So on the Main page I have added-
private void deleteProfile_SubmitClicked(object sender, EventArgs e)
{
WebService.Service1SoapClient client = new WebService.Service1SoapClient();
listBox1.Items.Clear();
client.profileListCompleted += new EventHandler<profileListCompletedEventArgs>(client_profileListCompleted);
client.profileListAsync(ID);
}
I thought this may have updated the listBox as it was confirmed in the dProfile form however when the form closes, the listBox stays the same and I have to manually refresh the webpage to see the update. How can I do this?
If I understood it correctly then you have three pages. Main, pWindow and dProfile. Earlier you were trying to close pWindwow from dProfile and that was working properly. Now you want to refresh the listBox1 on Main Page.
To achieve that you may follow a similar strategy. You are probably opening pWindow from Main page with something on the following line
pWindow pWin = new pWindow();
pWin.Show();
Now you may define a new event in pWindow class.
public event EventHandler pWindowRefeshListBox;
Then in your event handler for deleteProfile_SubmitClicked you may raise the event to refresh listbox1, something on the following line:
private void deleteProfile_SubmitClicked(object sender, EventArgs e)
{
if(pWindowRefreshListBox != null)
pWindowRefreshListBox(this, new EventArgs());
this.Close();
}
Then in your main page register the event against pWin object, which you defined earlier.
pWin.pWindowRefreshListBox += new new EventHandler(pWindow_pWindowRefreshListBox);
Then define the event in Main page.
private void pWindow_pWindowRefreshListBox(object sender, EventArgs e)
{
listBox1.Items.Clear();
}
This should refresh the listbox. I haven't test the code or the syntax. So you may check it
before implementing.
EDIT
you may define the event in dProfile as static
public static event EventHandler SubmitClicked;
Then you will be able to register it in Main and pWindow against Class Name
dProfile.SubmitClicked += new ..............
Then implement it accordingly, in pWindow, close the window and in main refresh listbox
EDIT:
You may create instance of deleteProfile on the main page register the following in your main
deleteProfile.SubmitClicked += new EventHandler(deleteProfile _SubmitClicked)
this should work
I have a problem with MouseEvents on my WinForm C# application. I want to get ALL mouse clicks on my application.
How to determine which control has been clicked ?(I'm beginner C#)
Try this:
private void Control_Clicks(object sender, EventArgs e)
{
Control control = (Control)sender; // Sender gives you which control is clicked.
MessageBox.Show(control.Name.ToString());
}
This, this or this may help.
Hope it helps.
private void Form1_Load(object sender, EventArgs e)
{
SetupClickEvents(this);
}
/// <summary>
/// This will loop through each control within the container and add a click handler to it
/// </summary>
/// <param name="container">The container whose children to handle clicks for</param>
private void SetupClickEvents(Control container)
{
foreach(Control control in container.Controls)
{
control.Click += HandleClicks;
}
}
private void HandleClicks(object sender, EventArgs e)
{
Control control = (Control)sender;
MessageBox.Show(string.Format("{0} was clicked!", control.Name));
}
If you're doing Windows Forms, you have several options :
Hook mouse event, and after figure out if the clicked component actually makes part of your application
You can declare a base class MyComponent : Control. That component overrides MousClick event and raise a special event notifying about a fact. Every control in your app derive from that control, so every control will notify about click happened on it. It's enough to subcribe
to thier events and process them as requested.
Just a couple of ideas...
You'd have to wire them all up to the same event handler. This can be done in the properties window for the controls in question. You could also write your own function to traverse the control tree and tie the function to each of their event handlers.
You can recursively traverse the Form.Controls collection with a foreach loop.
void attachGlobalHandler(Control aToParse, EventHandler aGlobalHandler)
{
foreach(Control lControl in aToParse.Controls)
{
attachGlobalHandler(lControl, aGlobalHandler);
lControl.Click += aGlobalHandler;
}
}
And then you call that on your form, with the name of the function you want to call:
attachGlobalHandler( Form1, myClickHandler );
And that should tie it to EVERY clickable control on the form. The sender argument of the handler should then always refer to the control that fired the event. That being said, I'd probably just attach individual event handlers, unless you need to treat multiple controls as a group.
WARNING: The code above is untested.
For the second question asked "How to determine which control has been clicked?" each control has events which may be handled in code.
The easiest way to know when a control has been clicked is to attached to the clicked event of a control which is done from the properties for the control. You may have to click the lightning bolt icon to see the events. Double-clicking beside the even will create an empty handler.
For example if you had a simple form with a single button attaching click events to the form and to the button will tell you when there is a click anywhere. In most cases the button click would be the most useful to handle.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
It's really simple!
On your click event in your Win-Form, You add
// Here is a modified version of your code:
private void Form1_Click(object sender, EventArgs e)
{
var control = Form1.ActiveControl;
}
I have a Custom Cntrol that has a button inside it. Now I want to access the button click from my Application Page.
Take a look at this Post:
How to Access a Button present inside a Custom Control, from the implementing page?
You have 2 ways:
You can access the button's click event through the user control object. For eg.
MyUC.button1.click += //etc
You can create your custom event of that specific button click in the user control. For example, in your usercontrol, you have:
public delegate void OnButtonClick(object sender, EventArgs e);
public event OnButtonClick Button1Click;
button1_click(object sender, EventArgs e)
{
if(Button1Click != null)
Button1Click(this, e);
}
Then you watch for that event on your user control:
MyUC.Button1Click += //etc.