Attaching Custom Events for WebUserControl

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am wondering what the best method of attaching custom Events to custom
WebUserControls are. I cannot seem to find the proper terminology to expand
my research.
Basicallly I have a custom user control that has 2 or 3 events
selectionChanged
DropDownOpened

I would like the user to be able to attach Server Side events for both. I
have the client side implementation worked out. I just need to find a proper
way to to get everything registered so that the output produces something
like
<div selectionChanged="__dopostback('contorolname', '');"
dropdownOpened="__dopostback('controlname', 'dropDownOepned????');">
.....</div>

Any help or pointers would be greatly appreciated. Thanks in Advance
 
I am wondering what the best method of attaching custom Events to
custom WebUserControls are. I cannot seem to find the proper terminology
to expand my research.
Basicallly I have a custom user control that has 2 or 3 events
selectionChanged
DropDownOpened

I would like the user to be able to attach Server Side events for both. I
have the client side implementation worked out. I just need to find a
proper way to to get everything registered so that the output produces
something like
<div selectionChanged="__dopostback('contorolname', '');"
dropdownOpened="__dopostback('controlname', 'dropDownOepned????');">
....</div>

Any help or pointers would be greatly appreciated. Thanks in Advance

I hope this doesn't sound flip, but my recommendation is to go read
"Developing Microsoft® ASP.NET Server Controls and Components" from
Microsoft Press, by Nikhil Kothari and Vandana Datye
(http://www.microsoft.com/mspress/books/5728.asp).



John Saunders
 
if you want server side event for your ascx control then you need to define
a delegate and then define an event
e.g.

public testControl : System.Web.UI.UserControls
{
public delegate void SelectionDelegate(int selected);
public event SelectionDelegate SelectionChanged();

public void Page_Load()
{
.....
}

private void FireSelectionEvent(int selection)
{
if(SelectionChanged != null)
SelectionChanged(selection)
}
}


HTH

Ollie Riches
 
Ollie said:
if you want server side event for your ascx control then you need to
define
a delegate and then define an event
e.g.

public testControl : System.Web.UI.UserControls
{
public delegate void SelectionDelegate(int selected);
public event SelectionDelegate SelectionChanged();

public void Page_Load()
{
.....
}

private void FireSelectionEvent(int selection)
{
if(SelectionChanged != null)
SelectionChanged(selection)
}
}


There is a generally-accepted pattern for events in .NET:

1) Decide on a name for the event, let's say, "SelectionChanged"
2) Decide what information needs to be passed to listeners when the event
fires. Let's say, an integer "selection"
3) Define a derived class of EventArgs to hold this information. The class
should accept the information in its constructor, and should define a
read-only property to access the information: The name of the class should
be the name of the event followed by "EventArgs":

public class SelectionChangedEventArgs : EventArgs
{
private int _selection;
public SelectionChangedEventArgs(int selection) : base()
{
_selection = selection;
}

public int Selection
{
get {return _selection;}
}
}

4) Define a delegate type for the event. The name of the type should be the
name of the event followed by "EventHandler". It should return void and take
two parameters. The first should be called "sender" and should be of type
"object". The second should be called "e" and should be of your EventArgs
type:

public delegate void SelectionChangedEventHandler(object sender,
SelectionChangedEventArgs e);

5) Define the event:

public event SelectionChangedEventHandler SelectionChanged;

6) Define a method to fire the event. It should be protected virtual in
order to permit derived classes to handle the event efficiently, and to
allow them to raise it in a different manner. The method should be named
"On" followed by the name of the event. It should accept a single parameter
named "e", of your EventArgs type:

protected virtual void OnSelectionChanged(SelectionChangedEventArgs e)
{
if (SelectionChanged != null)
{
SelectionChanged(this, e);
}
}

7) For "extra credit", you can define "convenience" overloads of your "On"
method. They should _not_ be virtual, but should call the virtual version:

protected void OnSelectionChanged(int selection)
{
OnSelectionChanged(new SelectionChangedEventArgs(selection));
}

8) Fire the event as required:

private void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
OnSelectionChanged(ddl.SelectedIndex);
}


John Saunders
 
I guess I did not state my real question clearly enough.
Is one supposed to be responsible for creating the attribute and
postback refreence. I had already done what was reiterated above and while
the event was declared no attribute was created for the postback.
<div selectionChanged="__dopostback('contorolname', '');"
dropdownOpened="__dopostback('controlname', 'dropDownOepned????');">
.....</div>
 
oh yeah so there is :)

Ollie

John Saunders said:
There is a generally-accepted pattern for events in .NET:

1) Decide on a name for the event, let's say, "SelectionChanged"
2) Decide what information needs to be passed to listeners when the event
fires. Let's say, an integer "selection"
3) Define a derived class of EventArgs to hold this information. The class
should accept the information in its constructor, and should define a
read-only property to access the information: The name of the class should
be the name of the event followed by "EventArgs":

public class SelectionChangedEventArgs : EventArgs
{
private int _selection;
public SelectionChangedEventArgs(int selection) : base()
{
_selection = selection;
}

public int Selection
{
get {return _selection;}
}
}

4) Define a delegate type for the event. The name of the type should be
the name of the event followed by "EventHandler". It should return void
and take two parameters. The first should be called "sender" and should be
of type "object". The second should be called "e" and should be of your
EventArgs type:

public delegate void SelectionChangedEventHandler(object sender,
SelectionChangedEventArgs e);

5) Define the event:

public event SelectionChangedEventHandler SelectionChanged;

6) Define a method to fire the event. It should be protected virtual in
order to permit derived classes to handle the event efficiently, and to
allow them to raise it in a different manner. The method should be named
"On" followed by the name of the event. It should accept a single
parameter named "e", of your EventArgs type:

protected virtual void OnSelectionChanged(SelectionChangedEventArgs e)
{
if (SelectionChanged != null)
{
SelectionChanged(this, e);
}
}

7) For "extra credit", you can define "convenience" overloads of your "On"
method. They should _not_ be virtual, but should call the virtual version:

protected void OnSelectionChanged(int selection)
{
OnSelectionChanged(new SelectionChangedEventArgs(selection));
}

8) Fire the event as required:

private void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
OnSelectionChanged(ddl.SelectedIndex);
}


John Saunders
 

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

Back
Top