I have a usercontrol that is inside another user control, what I would like to do is after a certain process runs to find the button control in the parent and run the OnClick event.
usercontrol 1
protected void btnReload_Click(object sender, EventArgs e)
{
//reloads data
}
usercontrol 2 (embeded in usercontrol 1)
protected void btnSave_Click(object sender, EventArgs e)
{
Button btnReload = (Button)Parent.FindControl("btnReload");
//here fire the btnReload onclick even
}
if this is not the best way I take recomendations cause I’m to the point of pulling my hair out over this. The reason I am doing this is because I have 8 pages that all require to create a new user so I would like to use 1 usercontrol for that but reload the page they are on with that new user created.
There are two ways you can do it :
1. On Server Side.
What you are trying to do is architecture wise wrong, child should not try to find controls in parents and neither should parent. So Child control can have a custom event, parent can register to it and you can fire that event on child control’s button click event.
Here’s an article to get you started : http://codebetter.com/brendantompkins/2004/10/06/easily-raise-events-from-asp-net-ascx-user-controls/
This has also been discussed here : How do you determine when a button is clicked in the child on the parent – ASP.NET
2. Client Side :
Using JQuery, you can catch child control’s button click event and from there you can fire parent’s button’s click. You can share parent’s button’s id using a hidden variable.