user control button click event
-
we have a user control(it has one textbox and button),few text boxes and another button on a form.How to control the button click event? Thanks in advance Vani
I think there is nothing to say how to control the click event of a button on a form as you might already know that thing. And to control that event of the button on your user control, you simply define events in your control to respond to user actions on the Web form. You can get through the following steps: 1. Declare the event whithin your user control:
public event EventHandler Click;
2. Create a method to raise the event.
protected virtual void OnClick(EventArgs e)
{
if(Click != null)
{
Click(this, e);
}
}3. Raise the event from within your user control's code: (say, Button1 on your control)
private void Button1_Click(object sender, System.EventArgs e)
{
//You can do something here then call the OnClick method.
OnClick(e);
}4. To use the control Click event, you simply add the user control on a Web form, then write a handler that responds to the event:
private void UserControl1_Click(object sender, System.EventArgs e)
{
//Your code here to repond to the user Click event.
}For more information, see Events in ASP.NET Server Controls[^] in MSDN.