473,387 Members | 1,561 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,387 software developers and data experts.

Event handling with usercontrol containing other controls

Hi,

Another question from a C# newbie:
I have a usercontrol class that includes a customized control.
Now, if I create an instance of the class, and click on it, a click
event is raised for the customized control within the class. This is
good.
What I also want is to raise a click event for the class itself.

For instance,
If I have class A (user control), with Custombutton B in it,
I create an instance of A.
then at runtime if I click on B, My Custombutton code can see the
click and act accordingly (which is good), however, my A class wont
report back that the A usercontrol has been clicked.
How can I do this?

I am guessing it is something like, within the class, declare
delegates etc, and if the Custombutton B is clicked, raise an event.

If this is the case, an example would be nice

(what I am actually trying to do here is have a usercontrol within a
form that I can click and drag around the screen. However, that
usercontrol has other controls within it. I should be able to click
and drag from anywhere within the usercontrol, even if clicking on
other controls)
Thanks very much.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #1
1 3000
Hi jralford,

You should wrap the Click event in your UserControl something like this:

namespace WebFormTest1
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for WebUserControl1.
/// </summary>
public class WebUserControl1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Button Button1;

public event EventHandler Click;

private void Page_Load(object sender, System.EventArgs e)
{
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
if (null != Click)
{
Click(sender, e);
}
}
}
}

And given this HTML:

....
<form id="Form1" method="post" runat="server">
<P>
<uc1:WebUserControl1 id="WebUserControl11"
runat="server"></uc1:WebUserControl1>
</P>
</form>
....

Here's the WebForm snippet where it is used:

....
public class WebForm1 : System.Web.UI.Page
{
protected WebUserControl1 WebUserControl11;

private void Page_Load(object sender, System.EventArgs e)
{
WebUserControl11.Click += new EventHandler(WebUserControl1_Click);
}

private void WebUserControl1_Click(object sender, EventArgs e)
{
Response.Write("WebUserControl has been Clicked!");
}
....

Joe
--
http://www.csharp-station.com

"jralford" <jr******@hotmail-dot-com.no-spam.invalid> wrote in message
news:42********@127.0.0.1...
Hi,

Another question from a C# newbie:
I have a usercontrol class that includes a customized control.
Now, if I create an instance of the class, and click on it, a click
event is raised for the customized control within the class. This is
good.
What I also want is to raise a click event for the class itself.

For instance,
If I have class A (user control), with Custombutton B in it,
I create an instance of A.
then at runtime if I click on B, My Custombutton code can see the
click and act accordingly (which is good), however, my A class wont
report back that the A usercontrol has been clicked.
How can I do this?

I am guessing it is something like, within the class, declare
delegates etc, and if the Custombutton B is clicked, raise an event.

If this is the case, an example would be nice

(what I am actually trying to do here is have a usercontrol within a
form that I can click and drag around the screen. However, that
usercontrol has other controls within it. I should be able to click
and drag from anywhere within the usercontrol, even if clicking on
other controls)
Thanks very much.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 16 '05 #2

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

Similar topics

0
by: Colin Ramsay | last post by:
Hi all, I don't normally post swathes of code like this but I am truly banging my head off my desk here... I've dynamically created a datagrid within a usercontrol. There are two columns...
1
by: Lin | last post by:
Hi All, I have a UserControl and in the UserControl I have a few controls (textbox buttons ect.) and I want to know how can I fire the "onchange" event (of the usercontrol) when the textbox in...
2
by: Just Me | last post by:
A control on a UserControl has it's mouseup event raised. I want the usercontrol to pass it on. That is , I want the form containing the UserControl to receive a MouseUp event from the...
2
by: Phill. W | last post by:
How do I propagate an Event that I've just handled? I have a UserControl containing, among other things, a TextBox. The UserControl handles KeyDown/KeyPress Events for both itself and the...
9
by: Marcelo Cabrera | last post by:
Hi, I have a user control that in turn creates a bunch of webcontrols dynamically and handles the events these webcontrols raise. It used to work fine on ASP .Net 1.1 but when compiled on 2.0 it...
5
by: Martin | last post by:
I have created a subclass of a textbox. In this subclass I have a property called 'Caption'. If this property contains a value then I want to create a Label object and add it to the controls...
3
by: Phillip N Rounds | last post by:
In diagnosing a problem, I noted that a button_click event gets run only after the page Page_Load event of the post back. (VS 2003, ASPNET 1.1, C#) Can this be correct? I'm trying to set...
0
by: Sjaakie | last post by:
Hi all, I have a Usercontrol containing a pre-formatted DataGrid and some code which by default enables paging (with links 1..20). I now want to add some extra paging information to the...
12
by: Marc | last post by:
Hi, How can i increase the mousehover event timer? AddHandler NewBtn.MouseHover, AddressOf Shownotes Private Sub Shownotes(ByVal sender As System.Object, ByVal e As System.EventArgs)...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.