adding event handler in user Control

G

Guest

My aspx has a user Control (a .ascx) that includes all the fields of an
address (add1, city, st, etc...). How can I add an event handler to a
comboBox inside the .ascx from within my aspx page? I just want to add this
event handler to that instance of the comboBox.

Thanks.
 
N

Nicholas Paldino [.NET/C# MVP]

VMI,

In order to do this, the control has to either expose the combo box
publically as a property (at which point, you can wire up an event), or it
would have to expose an event from the combobox, and aggregate the event
itself so that the page can connect to it.

Hope this helps.
 
S

smc750 via DotNetMonster.com

Every user control inherits from System.Web.UI.UserControl which exposes a
method called RaiseBubbleEvent. You do not necessarily have to make your
controls public. You can just call this method from your user control and
have the hosting page or control handle it through the protected method
OnBubbleEvent. Below is some example code where you can raise an event from a
custom dropdownlist control and the handle the event in the hosting page.

/// <summary>
/// Raises a custom event to the user of the control
/// if you want to sync this control with another.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ddCountries_SelectedIndexChanged(Object sender,EventArgs e)
{

CommandEventArgs ee = new CommandEventArgs("CountryChange", null);

RaiseBubbleEvent(sender, ee);


}

override protected bool OnBubbleEvent(object sender, System.EventArgs e)
{
bool isHandled = false;

if(e is CommandEventArgs)
{
CommandEventArgs ee = (CommandEventArgs)e;

switch(ee.CommandName)
{
case "CountryChange":
ddlStates.CountryCode = ddlCountries.SelectedCountry;
ddlStates.DataBind();
isHandled = true;
break;

}
}

return isHandled;
}



You can find lots of documentation on this in msdn help

smc750
http://www.certdev.com
 
G

Guest

I tried both version but I opted to use RaisedBubble and OnBubbleEvent to fix
the problem.
The only problem I'm having because of this (if you can call it a problem)
is that this aspx uses two instances of the same user control (the
residential address and the Postal address). How can I distinguish between
them? For example, how can I check in OnBubbleEvent to check the comboBox in
the Postal "version" of the user control (instead of the Residential User
Control)?

Thanks again.
 
S

smc750 via DotNetMonster.com

The code shows how you can change the commandEventArgs to send custom events
to the OnBubbleEvent.
Change the name to "residentialAddress" or "PostalAddress" and handle it
differently in the case statement.

smc750
http://www.certdev.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top