How to expose WebCustomControl event

M

Mehdi

Hi,

I have been working on this for more than a day, with no joy and I was
wondering if someone could help me with this:

I am trying to create a WCC (WebCustomControl) that contains a LinkButton
(server side). I have compiled this WCC, added it to my VS toolbox and it
is working ok, except I need Click event of my WCC. Say if I drag and drop
my WCC from tool box onto a new WebForm and double click on it,
"WebCustomControl1_Click(object sender, System.EventArgs e)" will be
created, but it will not fire from the WebForm. Therefore whatever code i
put in there will not be executed.

I really appreceate if someone could tell me how to do this. Here is my
WebCustomControl code:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace WebControlLibrary1
{
/// <summary>
/// Summary description for WebCustomControl1.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:WebCustomControl1
runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : System.Web.UI.WebControls.LinkButton ,
INamingContainer

{

private System.Web.UI.WebControls.LinkButton _btn;


public WebCustomControl1()
{
this._btn = new LinkButton();
}


[Bindable(false),
Category("Datel")]
public string Text
{
set
{
_btn.Text = value;
}
get
{
return _btn.Text;
}
}

protected override void CreateChildControls()
{
_btn.ID = UniqueID;
_btn.Text = (Text =="" || Text == null) ? "LinkButton1": Text;
_btn.CssClass = CssClass;


Controls.Add(_btn);
}

protected override void Render(HtmlTextWriter writer)
{
writer.Write("<TABLE id=Table1 cellSpacing=1 cellPadding=1 width=300
border=0 bgColor=yellow>");
writer.Write(" <TR>");
writer.Write(" <TD>&nbsp;</TD>");
writer.Write(" <TD>");

this._btn.RenderControl(writer);

writer.Write(" </TD>");
writer.Write(" <TD>&nbsp;</TD>");
writer.Write(" </TR>");
writer.Write("</TABLE>");
}


}//
}//

//-----------------------------

Thanks for your time.

Regards


Mehdi
 
J

John Saunders

Mehdi said:
Hi,

I have been working on this for more than a day, with no joy and I was
wondering if someone could help me with this:

I am trying to create a WCC (WebCustomControl) that contains a LinkButton
(server side). I have compiled this WCC, added it to my VS toolbox and it
is working ok, except I need Click event of my WCC. Say if I drag and drop
my WCC from tool box onto a new WebForm and double click on it,
"WebCustomControl1_Click(object sender, System.EventArgs e)" will be
created, but it will not fire from the WebForm. Therefore whatever code i
put in there will not be executed.

I really appreceate if someone could tell me how to do this. Here is my
WebCustomControl code:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace WebControlLibrary1
{
/// <summary>
/// Summary description for WebCustomControl1.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:WebCustomControl1
runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : System.Web.UI.WebControls.LinkButton ,
INamingContainer

{

private System.Web.UI.WebControls.LinkButton _btn;

Mehdi,

I see from your code that your control both derives from LinkButton and also
_has_ a LinkButton. When you drop the control on a page and then
double-click it, a Click event handler is created which will handle Click
events raised by the base LinkButton. But your code only renders the
internal LinkButton, _btn. You have no code to handle events generated by
_btn. Instead, try the following:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace WebControlLibrary1
{
/// <summary>
/// Summary description for WebCustomControl1.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:WebCustomControl1
runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : System.Web.UI.WebControls.LinkButton ,
INamingContainer

{

public WebCustomControl1()
{
}


[Bindable(false),
Category("Datel")]
public string Text // May need to be public override
{
set
{
this.Text = value;
}
get
{
return this.Text;
}
}

protected override void Render(HtmlTextWriter writer)
{
writer.Write("<TABLE id=Table1 cellSpacing=1 cellPadding=1 width=300
border=0 bgColor=yellow>");
writer.Write(" <TR>");
writer.Write(" <TD>&nbsp;</TD>");
writer.Write(" <TD>");

base.Render(writer);

writer.Write(" </TD>");
writer.Write(" <TD>&nbsp;</TD>");
writer.Write(" </TR>");
writer.Write("</TABLE>");
}


}//
}//

//-----------------------------
 

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

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top