473,466 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

HOw to Raise Events in an ASCX User Control (Is it better in ASPNET 2.0 ?)

I know Visual Studio lacked support on easily writing code
to raise events from a ascx user control ( because you
have to hand write them).
(http://codebetter.com/blogs/brendan..../06/27795.aspx)
Question: Is it better and easier now in ASPNET 2.0 , meaning VS 2005?

Feb 7 '06 #1
1 9925
This is what you have to hand-write

Raise Events From ASP.NET ASCX User Controls

Drop a UserControl called MyControl.ascx into a page.

To add a member variable to the container class, you can simply add the
line (C#):

protected MyControl MyControl1;

STEPS IN RAISING EVENTS FROM ASP.NET ASCX USER CONTROLS.
================================================== ======

USERCONTROL.ASCX.CS
-------------------

1. Define a public member

public event System.EventHandler TabClicked;

2. Raise it whenever you want like this:

if(this.TabClicked != null)
this.TabClicked(this, new EventArgs());


PARENTPAGE.ASPX.CS
------------------

1. Drag and Drop the control.

2. Add in the Page_Load or Init the wire code
this.MyControl.TabClicked +=new
EventHandler(MyControl_TabClicked);

3. Write the Handler

private void MyControl_TabClicked(object sender, EventArgs e){ /*
etc... */ }

If you type "this.MyControl.TabClicked +=" in the IDE, an press TAB
twice

PASSING DATA
============
Now, say you wanted to pass some specific data to the container
control.

One way is to use a delegate. This comes in really handy, and is in
fact quite easy too.
For example, what if you wanted to pass some private value from the
encapsulated child class?

1. First create a custom class deriving from System.EventArgs with a
member variable
that contains your interesting private value:

public class TabClickEventArgs : EventArgs
{
private String myString;
public String MyString
{
get { return myString; }
set { myString = value; }
}
}

2. Then create a delegate for your event like so:

public delegate void TabClickEventHandler(object sender,
TabClickEventArgs e);
The rest is just like before, except that you define your custom event
hander,
instead of the basic System.EventHandler.

public event TabClickEventHandler TabClicked;

And raise it like so.

// Raise the tabclicked event.
if(this.TabClicked != null)
{
TabClickEventArgs e = new TabClickEventArgs();
e.SomeString = "test";
this.TabClicked(this, e);
}

Finally, when you wire the event up, your method signature for the
handler,
just needs to match the signature of your delegate:

private void MyControl_TabClicked(object sender, TabClickEventArgs
e) {}

-----

Feb 7 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michael Craig | last post by:
Hello, I have written an application using the calendar control in a regular aspx application, to prove I could get the functionality I wanted. Which is to display the current month and populate...
0
by: Greg Park | last post by:
I have many user controls loading into a web page using Page.LoadControl However, I'm unable to figure out how to raise an event when a button is click or a check box is checked. I can have...
3
by: runtoofar | last post by:
I have an ASCX user control with a drop down list inside an ASPX (C#) page. The user selects an item from the DDL, clicks a button, and the DDL.SelectedValue is supposed to be sent to another...
5
by: Paul Bromley | last post by:
For some time now I have been struggling trying to understand how to handle events originating in a User Control that I have designed when using this in an application. Basically I need to respond...
0
by: ElSchepo | last post by:
Hi, Just read http://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnaspp/html/customcontrolfromusercontrol.asp on how to put User Controls (ascx) into an assembly. I made a...
2
by: jfolland | last post by:
I am trying to add a basic user control to a web site; coming from visual studio 2003 this was extremely easy. I would just add the user control and then add the controls that I want on that...
0
by: erkbiz | last post by:
Hello, this is my first post, I hope this is the right forum to ask my question. I am dynamically loading a user control multiple times. I have a gridview and have inserted empty rows. Into these...
1
by: Sylvie | last post by:
I have an Aspx page (Name:"PG") (Contains "UC") and an Ascx Usercontrol (Name:"UC") (Contains "AspxGrid1") I wanna expose Aspxgrid1.rowchanged event to Aspx Page, when user vhanges or...
2
by: HammRadio | last post by:
I am exploring converting my web app (current Framework 1.1) to Framework 2.0. Everything went smoothly save for one item. To reduce the trips to the database, when loading user controls (like a...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.