473,769 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
+ 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 9940
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.ASC X.CS
-------------------

1. Define a public member

public event System.EventHan dler TabClicked;

2. Raise it whenever you want like this:

if(this.TabClic ked != 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(My Control_TabClic ked);

3. Write the Handler

private void MyControl_TabCl icked(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.EventArg s with a
member variable
that contains your interesting private value:

public class TabClickEventAr gs : 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 TabClickEventHa ndler(object sender,
TabClickEventAr gs e);
The rest is just like before, except that you define your custom event
hander,
instead of the basic System.EventHan dler.

public event TabClickEventHa ndler TabClicked;

And raise it like so.

// Raise the tabclicked event.
if(this.TabClic ked != null)
{
TabClickEventAr gs e = new TabClickEventAr gs();
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_TabCl icked(object sender, TabClickEventAr gs
e) {}

-----

Feb 7 '06 #2

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

Similar topics

0
2491
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 the date cells with events kept in a database. I'm using the DayRender event of the calendar control to query the database and insert new label controls into each cell, for each event. Here's the problem.. When using the same logic in a web...
0
1724
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 upto a hundred user controls on a single page and AutoPostBack isn't an option. I have no problem with raising events when an user control is dropped on the aspx page.
3
2211
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 function. However, the SelectedValue is always the first value in the DDL. I have tried several methods to work around this including the examples at: http://www.mastercsharp.com/article.aspx?ArticleID=67&&TopicID=2...
5
1400
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 to button clicks in my user control. I have struggled trying to understand Delegates, aqnd m still struggling. I then came across the following tutorial on MSDN and thought I had 'cracked it' ...
0
1762
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 solutions in which I created an user control. Then I used Publish website to make the assembly for my user control. So far so good. There is a file like
2
1652
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 user control, but I am not able to get a basic link button to click. This seems so ridiculously basic, but I am not able to get this to work. What am I missing?
0
1246
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 empty rows I am dynamically loading a .ascx user control (it too being a gridview) multiple times each time it loads it renders different data. I want to be able to access the data in the different gridviews is this possible? This snippet is...
1
2923
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 clicks on a row in usercontrol's grid, my page must catch this method and
2
3017
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 large datagrid or form), we store the grid or form in Session Object. On PostBack we grab the control from the session, add it to our main form. In VS 2003 (1.1), the page will fire any event called in the custom control, e.g, cmdSave_Click. ...
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9849
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8861
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7393
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6661
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3948
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.