By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,665 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,665 IT Pros & Developers. It's quick & easy.

Attaching Custom Events for WebUserControl

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
<jo*****@driver.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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


Nov 19 '05 #2

P: n/a
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
<jo*****@driver.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

Nov 19 '05 #3

P: n/a
"Ollie" <ol**********@hotmail.com> wrote in message
news:Ok*************@TK2MSFTNGP12.phx.gbl...
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

<jo*****@driver.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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


Nov 19 '05 #4

P: n/a
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>
"John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
news:OP**************@TK2MSFTNGP15.phx.gbl...
"Ollie" <ol**********@hotmail.com> wrote in message
news:Ok*************@TK2MSFTNGP12.phx.gbl...
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

<jo*****@driver.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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



Nov 19 '05 #5

P: n/a
oh yeah so there is :)

Ollie

"John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
news:OP**************@TK2MSFTNGP15.phx.gbl...
"Ollie" <ol**********@hotmail.com> wrote in message
news:Ok*************@TK2MSFTNGP12.phx.gbl...
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

<jo*****@driver.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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



Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.