ASP.NET Composite Control raise event to parent form - c#

Say I have a composite control in ASP.NET (C#) which includes a drop down list. I need to be able to bubble the event back to the parent form so that other code can be executed based on its SelectedItem.
How do I expose the OnSelectedItemChanged event to the application?
Do I need to create my own delegate and raise it when the internal drop down list item is changed?

I've created control which contains a button and I'm using same approach; create a delegate and raise events on button's click.
public delegate void IndexChangeEventHandler(object sender, EventArgs e);
public event IndexChangeEventHandler SelectedIndexChanged = delegate { };
//this is in your composite control, handling ddl's index change event
protected void DDL_SelectedIndexchanged(object sender, EventArgs e)
{
SelectedIndexChanged(this, e);
}

Correct... You would want to create your own event for SelectedItem and write an event handler for the dropdown list's SelectedItem and inside the method raise your event.

Related

How to handle different Events from different Controls in one method?

I have a ListView SelectionChanged event and a DatePicker SelectedDateChanged event, both named "Changes".
private void Changes(object sender, SelectionChangedEventArgs e)
{
//Process selected Items from ListView an Date from DatePicker
}
So both events are handled using the same method, which is exactly what I want. However, if I try the same thing with a TextBox SelectionChanged event, Visual Studio creates a new event handler (probably because of the different types of the e argument?). Is it possible to call my Changes method when I write text in the TextBox?
You can assign any of methods as any event handler, via lambda expression:
listView1.SelectionChanged += Changes;
datePicker1.SelectedDateChanged += Changes;
textBox1.SelectionChanged += (sender, e) => Changes(sender, null);
If you are using SelectionChangedEventArgs inside of method, you will need to create it manually with needed data inside which may be harder than creating multiple handlers.

Call a method in another class from a control within a control?

I have a custom control created, comprised of a bunch of text boxes, 1 button and radio buttons.
I then have a "parent" control which only has this 1 custom control placed on it, and in the code behind I have a reference to this control's Presenter.
The presenter handles the actual code for searching (when the one button is pressed). How do I set up the button click event on the child control to call the Search method from the presenter?
So I have the following:
CtlSearchDetails
ViewSearchScreen
PresenterSearchScreen
The button click event is on CtlSearchDetails and it needs to call the method on PresenterSearchScreen. I cannot figure out how to reveal this method to the instace of the control on ViewSearchScreen.
In your custom child control, you want to expose an event for the button click:
public event Action OnButtonClicked;
Then hook the button clicked event from the designer
private void btn_myButton_Clicked(object sender, EventArgs e)
{
if (OnButtonClicked != null)
OnButtonClicked();
}
Then in your parent container, you want to handle this event from the child control
this.myChildControl.OnButtonClicked += new Action(onChildButtonClicked);
private void onChildButtonClicked()
{
// Do your search here
}

C# combobox selection feeding another combobox

using mysql I feed combobox2 with selection of combobox1.It works OK. but the problem is the first select seems does not trigger the eventhandler. the second time I do it it will trigger.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.SelectedValueChanged += new EventHandler(comboBox1_selectedvaluechanged);
}
private void comboBox1_selectedvaluechanged(object sender, EventArgs e)
{
region = comboBox1.SelectedItem.ToString();
values_to_venue();
db.connection.Close();
}
It's because you're not creating your event handler until the SelectedIndex of the combobox changes. This is fired alongside SelectedValue changes. Create the event handler in the load method ensuring it is there when the first SelectedValue changes. If you put it in the Load make sure you clean it up with -= in the unload. Or, you can just create it in the constructor and then you won't need to remove it.
You do want to set the events in your constructor or load method; also, I think the event you want to use is ComboBox.SelectionChangeCommitted because if the user navigates the DropDown list using the up/down arrows on the keypad, SelectedIndexChanged will fire before the selection is committed - is that what you want??????

How to raise MouseClick event?

Hello
I have read that events can be raised the same way as methods. Well it works for my custom events (I create a delegate, the event and I am able to raise the event by calling it).
However I am not able to manually raise events like MouseClick and other, it keeps saying that it must appear on the left side of the += operator. What is the problem?
While I am certain you'll get other answers more informative than this one, basically you can't "raise" an event outside the class that contains it. MSDN has this to say about events
Events are a special kind of multicast
delegate that can only be invoked from
within the class or struct where they
are declared (the publisher class). If
other classes or structs subscribe to
the event, their event handler methods
will be called when the publisher
class raises the event.
If you wanted to literally raise the event for, say, a Windows Forms Control MouseClick, you'd have to create a subclass of that control and either invoke base.OnMouseClick() or override it.
If this is a button, you can programmatically click it using the PerformClick method.
Sadly, this only works on buttons and not other types of Controls... except MenuItem.
If you want to click button you should call:
button1.PerformClick();
If you want to call MouseClick please refer to this forum, there is solution in c# using windows api:
private void button1_Click(object sender, EventArgs e)
{
//Enter your code here
}
void Page_Load(object sender, EventArgs e){
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button1_Click(this, e);
}
Let's say you want to manually raise the event "click". This works for me:
public partial class CustomButton : UserControl
{
public new event EventHandler Click;
private void lblText_Click(object sender, EventArgs e)
{
Click(this, e);
}
}

How to raise list controls SelectedIndexChanged event in codebehind?

How to raise the SelectedIndexChanged event of an asp.net List control in a codebehind using C#?
If you're asking how to manually fire the event so that it can run whatever logic is attached: don't.
Your event handlers should be slim. If you need to perform the same operation from multiple places, then extract that functionality into its own method and have the event handler invoke that. For example:
private void CountryListBox_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateStates(ListBox1.SelectedItem.Text);
}
private void UpdateStates(string country)
{
StateListBox.DataSource = GetStates(country);
StateListBox.DataBind();
}
Now instead of trying to fire the SelectedIndexChanged event, you just invoke the method that this event handler refers to, i.e.
private void Page_Load(object sender, EventArgs e)
{
UpdateStates("USA");
}
Don't put complex logic in event handlers and try to raise those events from unexpected places. Instead, put the complex logic in its own method, so that you can perform the associated actions from elsewhere.
It is raised automatically.
Go in the Events section, lightening
bolt in properties window
alt text http://img704.imageshack.us/img704/6100/listbox.jpg
double click the place holder next to
event. This is what you will get.
protected void ListBox1_SelectedIndexChanged(object
sender, EventArgs e)
{
}
if you want to raise this event from another code block then, call
ListBox1_SelectedIndexChanged(sender,
e);
If what you want is more than just executing the code behaviour coded for the selected index (like listed in the previous answer), the short answer is there is no easy way. You can write a simple code that on prerender or render to explicitly define the control id variable in your rendered HTML and then use javascript to set the selected index. This will cause the postback that trigger the event. Alternatively you can register an ajax call back method and have the client calls that either when some event happened or by automatic timer.

Categories

Resources