使自定义控件可单击

时间:2014-11-17 11:50:50

标签: c# asp.net onclick event-handling custom-controls

我希望能够点击我的compositeControl自定义控件上的任意位置,然后点击一下点击事件。这可能吗?

我有一个自定义控件,它是一个面板,里面有一些图像和标签。它应该是一个按钮,但我不知道如何使整个控件可点击。有什么想法吗?

namespace CustomButton
{
    [ToolboxData("<{0}:CustButton runat=server></{0}:CustButton>")]
    public class CustomButton : CompositeControl
    {
        Panel buttonPnl = new Panel(); //Declare and Init here in case you need it for chanmging background color at code compile and not run time


        //will put everything in a table in ordser to space it
        Table buttonTbl = new Table();
        TableRow imgRow = new TableRow();
        TableRow mainTextRow = new TableRow();
        TableRow subTextRow = new TableRow();
        TableCell iconImgCell = new TableCell();
        TableCell errorImgCell = new TableCell();
        TableCell mainTextCell = new TableCell();
        TableCell subTextCell = new TableCell();
        System.Web.UI.WebControls.Image iconImg = new System.Web.UI.WebControls.Image();
        System.Web.UI.WebControls.Image errorImg = new System.Web.UI.WebControls.Image();
        Label mainTextLbl = new Label();
        Label subTextLbl = new Label();

   protected override void CreateChildControls()
{
    /*
     * This builds the button. 
     * Note this is hit everytime the button atributes ar echnaged so this overrides everything. 
     * If you want control over a attribute, dont have it set here.
     */
    Controls.Clear();

    //init controls
    buttonPnl.ID = "buttonPnl";

    iconImg.ID = "logoImg";
    iconImg.Width = Unit.Percentage(50);//100% of cell width
.
.
.
.
build the custom control...

}

然后我公开了一些自定义属性....

如何将click事件添加到整个自定义控件?我是否在调用网页代码上执行此操作?

我已尝试添加动作属性,但不知道这是否是我必须做的。

[Category("Action")]
[Description("do some clicking")]
public event EventHandler Click
{
    add
    {
        Events.AddHandler(ObjEvent, value);
    }

    remove
    {
        Events.RemoveHandler(ObjEvent, value);
    }

}

0 个答案:

没有答案