473,224 Members | 1,604 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

Attaching Custom Events for WebUserControl

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
5 4497
<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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mike Levin | last post by:
Hello Group, I'm using VisualStudio.NET and put a DataGrid into a WebUserControl (an ascx file). For the DataGrid element, I have set AllowSorting="True" and OnSortCommand="Grid_Sort".
1
by: DichkoSoft | last post by:
Hi I create WebUserControl and very confused when use control For Example: ***************************** *wuc.ascx ***************************** <script id=clientEventHandlersJS...
2
by: Oren | last post by:
Hello everyone... I have a simple WebUserControl with Button and TextBox on it. Webform1.aspx contains this WebUserControl. How can I create my own C'tor for this WebUserControl - I mean when I...
2
by: Oren | last post by:
Hi everyone, I have WebUserControl on a Webform. How can I sent dynamically to the WebUserControl from a Function/Property on the Webform ? <uc:myuc id="myid1" CurrentPage=<%#GetText()%>...
3
by: George Jordanov Ivanov | last post by:
Folks, I am implementing a WebUserControl, which will have its own custom event StateChanged. Now, I want to add this event to the Events tab in the control properties, so that the users of my...
0
by: Terry Olsen | last post by:
In an effort to create a QueryBuilder for a web page, I've created a WebUserControl. It can be seen here: http://boycot.no-ip.com/images/querybuilder.jpg I load one control initially in the...
1
by: Dave A | last post by:
I have a problem that I have boiled down to a very simple example. I have a user control that displays a some data from a business object. On one screen I have a collection of these business...
0
by: Klaus Jensen | last post by:
Hi In a repeater-control, in the <SeparatorTemplatei have placed a webusercontrol, I have made. That works great- However, I want to know be able to only display this WebUserControl a certain...
2
by: =?UTF-8?B?16jXnteZ?= | last post by:
Hey, I'm loading a webusercontrol dynamically using The following code: Control newCtrl = LoadControl("MyCtrl.ascx"); newCtrl.ID = "MyCONTROL"; container.Control.Add(newCtrl); My...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.