Handling User Control Events on Containing Page - asp.net

I want to have a user control containing a save button.
I want the click event attached to the save button to be handled by the containing page not the user control
Is this possible?
EDIT MORE SPECIFIC
Say I have a page called "Editor Page" and a user control called "Editor Buttons".
On the "Editor Buttons" user control I have a save button (which is a web control). I want the Click event of the save button to be handled by the "Editor Page" rather than the user control.
The reason for this is because I want to specify base pages that implement common behaviour.

This technique is commonly referred to as “event bubbling”.
Here is an exmple:
public partial class Test : System.Web.UI.UserControl
{
public event EventHandler buttonClick;
protected void Button1_Click(object sender, EventArgs e)
{
buttonClick(sender, e);
}
}
Then you can subscribe the event buttonClick at webpage to display the different information .
public partial class Test: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UserControlID.buttonClick+=new EventHandler(UserControlID_buttonClick);
}
protected void UserControlID_buttonClick(object sender, EventArgs e)
{
Response.Write("hello,I am jack.");
}
}

I will assume that you have the code for the UserControl. If that is the case, you can define a Save event in your user control and have it expose the Click event of the save button, either directly, or by internally setting up an event handler and raise the Save event when the button is clicked. That could look something like this in the UserControl:
public event EventHandler Save;
private void SaveButton_Click(object sender, EventArgs e)
{
OnSave(e);
}
protected void OnSave(EventArgs e)
{
// raise the Save event
EventHandler save = Save;
if (save != null)
{
save(this, e);
}
}

UserControl1.ascx
UserControl1.ascx.cs
public partial class UserControl1 : System.Web.UI.UserControl
{
public event EventHandler UserControl1Click;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn1_Click(object sender, EventArgs e)
{
UserControl1Click(sender, e);
}
}
UserControl2.ascx
UserControl1.ascx.cs
public partial class UserControl2 : System.Web.UI.UserControl
{
public event EventHandler UserControl2Click;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn2_Click(object sender, EventArgs e)
{
UserControl2Click(sender, e);
}
}
Then add one Asp.Net Text box(to display user control id) and above two user controls to Aspx page as shown below.
<title>Untitled Page</title>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txt1" runat="server"></asp:TextBox> <br />
<UC1:UC ID="uc1" runat="server" />
<UC1:UC ID="uc2" runat="server" />
</div>
</form>
Now add event handler for each user control to handle button click event of two controls as shown below.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
uc1.UserControl1Click += new EventHandler(uc1_UserControl1Click);
uc2.UserControl2Click += new EventHandler(uc2_UserControl2Click);
}
protected void uc1_UserControl1Click(object sender, EventArgs e)
{
txt1.Text = "User Control 1 Clicked";
}
protected void uc2_UserControl2Click(object sender, EventArgs e)
{
txt1.Text = "User Control 2 Clicked";
}
}
If we click User Control1, text box displays “User Control 1 Clicked” or if we click on User Control2, text box displays “User Control 2 Clicked”.

Related

How to handle or check User Control postback in Parent page?

I have a checkbox in my user control. How do I disable my parent control's event when user control's checkbox's CheckedChanged event was triggered?
You cannot disable events from it ASP.Net Life Cycle.
However, you can check inside each Event whether the postback is triggered by Parent or User Control.
If you want to check which control fire event inside Parent page -
public partial class Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
string id = Request.Form["__EVENTTARGET"];
if (!string.IsNullOrWhiteSpace(id) && id.Contains("WebUserControl11"))
{
}
}
}
}
If you want to check whether this event is fired by one of my control inside UserControl -
public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) // *** This IsPostBack is not same as Parent's IsPostBack ***
{
}
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (IsPostBack)
{
}
}
}

Refresh ascx from button click

How to refresh another user control declared in a ascx page from button click event of that page(ascx.cs)?
You can load it dynamically.
First from page load event
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostback)
{
LoadControl();
}
}
And then load it from a button again on which you want to reload it
protected void Button_Click(object sender, EventArgs e)
{
LoadControl();
}
private void LoadControl()
{
usercontrols_SimpleControl ucSimpleControl = LoadControl("~/UserControlName.ascx") as usercontrols_SimpleControl;
Placeholder1.Controls.Add(ucSimpleControl);
}

Sending data from usercontrol to main page on postback

I want to create a usercontrol for retrieving and filtering a dataset. The problem I'm having is that the controls to be populated are not in the usercontrol, but on the main page on which the usercontrol exists. The reason for this is that the controls that are to be populated are different on each page.
If I could get some help solving the following simplified problem, I will probably be on my way to solve the bigger problem:
<body>
<form id="form1" runat="server">
<div>
<uc1:uc1 ID="uc1" runat="server" />
<asp:Label ID="lbl" runat="server"></asp:Label>
</div>
</form>
</body>
What I want to do is set the label on the main page by clicking a button on the usercontrol. Problem I'm having is that the usercontrol postback happens after the main page.
Thanks!
I managed to solve it by calling Parent.Page. I also implemented an interface so that I can call different pages' methods from the user control. Not sure if it's a good solution, but it will do:
interface IFilter
{
void SetLabel(string text);
}
public partial class uc : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Click(object sender, EventArgs e)
{
((IFilter)Parent.Page).SetLabel("changed");
}
}
public partial class _Default : System.Web.UI.Page , IFilter
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
}
public void SetLabel(string text)
{
lbl.Text = text;
}
}
create a custom event in user control
create a property in usercontrol
fire the event when the button is clicked & set the property value
access the property in the main page in user control's eventhandler

A confusion about webcontrol scope in ASP.NET

A custom control inside my Masterpage, Does that can access the main page?
Please help!
This is my code.
namespace TVSSystem
{
public partial class ControlTVS1 : System.Web.UI.UserControl
{
Page abc;
protected void Page_Load(object sender, EventArgs e)
{
abc = this.Page; //Control: I suposse that I can access all controls of my page
}
protected void Image1_Click(object sender, ImageClickEventArgs e)
{
ContentPlaceHolder cph = (ContentPlaceHolder)abc.FindControl("ContentPlacerHolder1");
TextBox txt = (TextBox)cph.FindControl("TextBox1");
Button btn = (Button)cph.FindControl("Button3");
txt.Visible = true;
btn.Visible = true;
}
}
}
Solved.
http://forums.asp.net/t/1000865.aspx/1
You could use the Page property to access the page containing this user control (no matter if you placed it inside a masterpage):
public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
var page = this.Page;
...
}
}

Pass MasterPage ImageButton event to content Page

I have ImageButton in a MasterPage. I want the OnClick event to fire and be captured by the .ASPX page hosted inside the MasterPage?
MasterPage:
<asp:ImageButton ID="btnClear" OnClick="Clear_Click"
ImageUrl="images/Back_Icon_06.png" runat="server" AlternateText="Clear"
width="38" height="39"/>
The masterpage is actually a child of the page (in fact, it's a UserControl). We don't want the page to have to be aware of the intimate details of its child controls (thats why we delegate those aspects to those controls in the first place), so the correct approach would be to handle the click event on the master page and from there fire another event on the masterpage which the page handles:
Master:
public event EventHandler SomethingHappened;
protected void Button_Click(object sender, EventArgs e)
{
OnSomethingHappened(EventArgs.Empty);
}
protected void OnSomethingHappened(EventArgs e)
{
if(this.SomethingHappened != null)
{
this.SomethingHappened(this, e);
}
}
Page:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
//allows us to change master pages
if(this.Master is MyMaster)
{
((MyMaster)this.Master).SomethingHappened += new EventHandler(HandleSomethingHappened);
}
}
private void HandleSomethingHappened(object sender, EventArgs e)
{
//deal with it
}
I would recommend specifying a strongly typed master page in your content page and exposing the event on the master page side
Here's a good MSDN reference
This is similar to what Rex M specified but just simplifies accessing the Master Page a little bit.
// Master Page Code
public event EventHandler ClearClick
{
add
{
this.btnClear.Click += value;
}
remove
{
this.btnClear.Click -= value;
}
}
// Content Page markup
<%# Page masterPageFile="~/MasterPage.master"%>
<%# MasterType virtualPath="~/MasterPage.master"%>
// Content Page Code
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Master.ClearClick += new EventHandler(OnClearClick);
}
private void OnClearClick(object sender, EventArgs e)
{
// Handle click here
}

Resources