Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fire event in Custom Control

Status
Not open for further replies.

kalkumar

Programmer
Jul 7, 2006
40
US
Hi,
I am designing the custom control which contains a Panel. Panel header row contains some text, Imagebutton. That text and text for image button is passed through the Properties. For the Image button I need to add the click event when the user clicks on Image button I need to transfer to another page.
How to provide the Click event to Image button and how to fire the click event in custom control?
I can write like this:
public event EventHandler Click
TableCell c=new TableCell();
ImageButton i=new ImageButton();
i.text=//get text from one property.
i.Click += new EventHandler(this.ib_Click);
c.Controls.add(i);

Protected virtual void ib_click(Object sender, EventArgs e)
{
if (Click != null)
{
Click(this, e); //Click is the EventHandler
}
}

This event is not firing.
How to handle the event.
 
this should work. when are you attaching the delegate to the event?

when adding controls to a page you must add them in the OnInit(). Do custom controls contain a similiar event?

this event must be added everytime, does your control check of postback?

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi,
Can you explain me a bit clear about these two lines:
when adding controls to a page you must add them in the OnInit(). Do custom controls contain a similiar event?

this event must be added everytime, does your control check of postback?
 
jmeckley said:
when adding controls to a page you must add them in the OnInit(). Do custom controls contain a similiar event?
if you dynamically add a control to a ascx or aspx it must be added in the [tt]protected override void OnInit(EventArgs e) { }[/tt]. Is there something similiar when designing a custom control?

jmeckley said:
this event must be added everytime, does your control check of postback?
Bad:
Code:
If(!this.Page.IsPostBack)
{
   TableCell c=new TableCell();
   ImageButton i=new ImageButton();
   i.text=//get text from one property.
   i.Click += new EventHandler(this.ib_Click);
   c.Controls.add(i);
}
Good:
Code:
TableCell c=new TableCell();
ImageButton i=new ImageButton();
i.text=//get text from one property.
i.Click += new EventHandler(this.ib_Click);
c.Controls.add(i);

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi,
I am not adding the event on aspx page. Iam adding this event on Custom control page i.e .cs page.

Thanks in adavnce
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top