Eventhandler for UserControl with button onClick event
17,299
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
}
Author by
teebot
Updated on June 04, 2022Comments
-
teebot almost 2 years
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