usercontrol and checkbox event


G

Guest

OK, I am trying to fire an event from a usercontrol that tells the page when
a checkbox was clicked.

Here is the control and when I try and wire up the page to catch the event,
my event is not shown. What am I doind wrong?

public delegate void CheckBoxEventHandler(object sender, System.EventArgs e);

public class WebUserControl1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.CheckBox CheckBox1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Response.Write("WebUserControl1 :: Page_Load <BR>");
}

#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.CheckBox1.CheckedChanged += new
System.EventHandler(this.CheckBox1_CheckedChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

public event CheckBoxEventHandler CheckedMe;

protected void OnCheckedMe(object sender, System.EventArgs e)
{
if(CheckedMe != null)
{
CheckedMe(this,e);
}
}


private void CheckBox1_CheckedChanged(object sender, System.EventArgs e)
{
Response.Write("WebUserControl1 :: Begin CheckBox1_Click <BR>");
OnCheckedMe(this,e);
Response.Write("WebUserControl1 :: End CheckBox1_Click <BR>");
}
 
Ad

Advertisements

G

Guest

If you are using ASP.NET 1.1 The page that uses the control has to
1- Register the control, e.g.
<%@ Register TagPrefix="cc" tagName="c1" src="WebUserControl1.ascx" %>

2- declare it in the codebehind as protected, e.g.
protected WebUserControl1 CheckBox1;

Then you will be able to write:
CheckBox1.CheckedMe+=new CheckBoxEventHandler(CheckBox1_CheckedMe);
 
G

Guest

Phillip thank you for your input.

I think I found the problem and confusion on my part.
I have 1.
<%@ Register TagPrefix="uc1" TagName="WebUserControl1"
Src="Control/WebUserControl1.ascx" %>

<uc1:webusercontrol1 id="WebUserControl11"
runat="server"></uc1:webusercontrol1>

But for your 2nd point I have
protected UserControl WebUserControl11;

I think this is where the problem may be, point 2 that is. By using this I
never see the event CheckedMe. This is a UserControl, so am I declaring it
incorrect on my page?
 
G

Guest

Yes, when you used the base type "UserControl", intellisense could not
display for you the methods in your derived control, which inherited the base
type.
 
G

Guest

Ok, I am not usnderstanding something or I have been looking at it too long.
I have dragged the control on my webform and
I declare the control....
protected UserControl ThisControl;

I try and create an event, though intellesense does not see it. OK, I can
live with that but I would think it would be easier to see the event I am
looking for...
this.ThisControl.CheckedMe += new CheckBoxEventHandler(ThisControl_CheckMe);

Compile error--
does not contain a definition for 'CheckedMe'??

So I can clearly see that there is a definition for the CheckedMe in my
control (public event CheckBoxEventHandler CheckedMe;)

So what am I doing wrong?
 
Ad

Advertisements

G

Guest

These are basic concepts in Object Oriented Programming.

An object of type System.Web.UI.UserControl (the base type) does not define
an event named "CheckedMe". Any object even if it were derived from
UserControl but cast as the base type cannot consume that event and hence the
compiler error.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top