views:

1788

answers:

3

Hello,

I've created a user control that contains a button and a few other controls.

When declaring my user control in the html markup I'd like to do some sort of :

<asp:CustomControl onclick="CustomControl_Click" ID="cc1" runat="server">

Where CustomControl_Click is obviously the action I want to call when my control's button is clicked.

So far in my control I have:

public event EventHandler Click;

    protected void Button1_Click(object sender, EventArgs e)
    {
        Click.Invoke(sender, e);
    }

but how can I forward the Eventhandler of the parent page to assign it to the Click Eventhandler in my control?

Any help is really appreciated!

PS: maybe there's a way of getting the method from the hosting page using reflexion

A: 

Will Event Bubbling help?
Event Bubbling in ASP.NET
MSDN

ArsenMkrt
That's what I already have and it works. But the question is rather how do you go from declaring an eventhandler in your html markup to attaching to your bubbling event? (So I don't have to link it in the code behind of the hosting page)
teebot.be
+1  A: 

I'm using a custom button (actually html div with LinkButton embedded in it). Here is code of it:

    public delegate void ClickEventHandler(object sender, EventArgs e);
    public event ClickEventHandler Click =  delegate { };

    public string Text
    {
        get { return cmdLink.Text; }
        set { cmdLink.Text = value; }
    }

    public bool CausesValidation
    {
        get { return cmdLink.CausesValidation; }
        set { cmdLink.CausesValidation = value; }
    }

    public string OnClientClick
    {
        get { return cmdLink.OnClientClick; }
        set { cmdLink.OnClientClick = value; }
    }

    public string CssClass
    {
        get { return cmdLink.CssClass; }
        set { cmdLink.CssClass = value; }
    }

    protected void cmdLink_Click(object sender, EventArgs e)
    {
        Click(this, e);
    }

Here is usage in aspx page:

<Button_Control:ButtonControl ID="btnSave" runat="server" Text="Save" 
          OnClick="btnSaveClick" />

and this is in code-behind page of aspx page:

    protected void btnSaveClick(object sender, EventArgs e)
    {
        //do stuff here
    }
TheVillageIdiot
But Still how do you map "OnClick="btnSaveClick" to the ClickEventHandler Click ??
teebot.be
I guess it's a convention then?Thanks a lot for your answer!
teebot.be
You have to write OnClick="btnSaveClick" and write event handler method in your code behind page. It is called by control's Click event.
TheVillageIdiot
A: 

I found it!

Instead of using event bubbling I use reflexion in the click eventhandler of the user control so I have :

public string OnClick;

    protected void Button1_Click(object sender, EventArgs e)
    {
        MethodInfo methodInfo = this.Page.GetType().GetMethod(OnClick);
        methodInfo.Invoke(this.Page, new object[]{ sender, e });
    }

And it works with declaring the user control as :

<asp:CustomControl OnClick="CustomControl_Click" ID="cc1" runat="server">
teebot.be
It is reflection not reflexion and delegates and event handlers just do that for you automatically!
TheVillageIdiot