I hope my subject is clear. anyway, i created a button and a dropdownlist in ucButtons.ascx web control. now, i defined it to master page as common controls and should be visible to any content pages. now, i wanted to modify the events of a button and the
dropdownlists in every content page.
i already created them but i have no idea how to call and modify its events from content page.
The idea of it, is for example if i press the "Save" button, it will execute the codes that are defined in the content page.
hello Syukna, thanks for your reply. i tried this but i can't call .ButtonPH in my page load. is there anything else should i do like delegation? if so, how do i do that? thanks in advance. jim.
In the above example, when the custom user contrrol button is clicked "Label1.Text" property is changed in "MyCustomButtonClick" event, which is created in the user control. Above example is tested and working perfectly from my end, Hope this helps you in
anyway.
Please "Mark as Answer" if this post answered your question.
hello 2pac, thanks for your codes and ideas. its pretty clear to me. i have a question, what if i defined the custombuttoncontrol in my master page which is Site.Master <uc:CustomButtonControl runat="server" ID="ucCustomButtonControl"/>, can i can call those
controls in my content page (default.aspx) and define an EventHandler inside default.aspx?
hello 2pac, thanks for your codes and ideas. its pretty clear to me. i have a question, what if i defined the custombuttoncontrol in my master page which is Site.Master <uc:CustomButtonControl runat="server" ID="ucCustomButtonControl"/>, can i can call those
controls in my content page (default.aspx) and define an EventHandler inside default.aspx?
//Site1.Master.cs
public partial class Site1 : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
//expose user control to other content pages
public CustomButtonControl MyButtonControl
{
get { return ucCustomButtonControl; }
}
}
//ContentPage.aspx.cs
public partial class ContentPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var page = Page.Master;
if (page != null)
{
var customButton = Master.MyButtonControl;
customButton.MyCustomButtonClick += MyCustomButtonClick;
}
}
protected void MyCustomButtonClick(object sender, EventArgs e)
{
Label1.Text = "Text Changed";
}
}
In the content page we have a label "Label1", now we are going to use custom user control's click event to change the label text, by using, @ MasterType directive, such as the one in the example, you can reference members on the master page.
<%@ MasterType virtualPath="~/Site1.Master"%>
In Master page you need to expose the control:
public CustomButtonControl MyButtonControl
By doing the above, we can get reference of the user control from master page and use its click event to change the label
Hope this helps
Please "Mark as Answer" if this post answered your question.
Hello 2pac, you are one of the Genius that i knew, plus you're also very generous. This is EXACTLY what i'm looking for. i successfully applied it and it works perfectly!!! thanks a lot to you.
I have one more question, is there any way to set <%@ MasterType virtualPath="~/Site1.Master"%> programmatically? or inside code behind? the fact is that, i have 2 master pages different structures and i want to change the virtualpath if i wanna use the
code you shared.
or can i define 2 different MasterType VirtualPath at the same time?
<%@ MasterType virtualPath="~/Site1.Master"%>
<%@ MasterType virtualPath="~/Preview.Master"%>
i tried that format with 2 VirtualPath but it gives me error.
jim_rosacena...
Member
31 Points
89 Posts
How to access user control (ucButtons.ascx) events defined in master page from content page
Jul 29, 2012 01:39 PM|LINK
Dear Guys and Experts,
I hope my subject is clear. anyway, i created a button and a dropdownlist in ucButtons.ascx web control. now, i defined it to master page as common controls and should be visible to any content pages. now, i wanted to modify the events of a button and the dropdownlists in every content page.
i already created them but i have no idea how to call and modify its events from content page.
The idea of it, is for example if i press the "Save" button, it will execute the codes that are defined in the content page.
Thank you very much in advance. Jim.
alvingeorge
Participant
925 Points
203 Posts
Re: How to access user control (ucButtons.ascx) events defined in master page from content page
Jul 29, 2012 02:32 PM|LINK
pleae check this
http://www.codeproject.com/Articles/15169/Creating-a-Common-Toolbar-in-ASP-NET-2-0-using-Mas
syukna
Participant
779 Points
197 Posts
Re: How to access user control (ucButtons.ascx) events defined in master page from content page
Jul 29, 2012 04:54 PM|LINK
You'll want to expose the button in the user control.
public Button ButtonPH { get { return this.Button1; } }then on the page load event or wherever in your content page you'll want to set the event.
protected void Page_Load(object sender, EventArgs e) { ucButtons.ButtonPH.Click += this.MyButtonEvent; } protected void MyButtonEvent(object sender, EventArgs e) { Response.Write("hi"); }jim_rosacena...
Member
31 Points
89 Posts
Re: How to access user control (ucButtons.ascx) events defined in master page from content page
Jul 30, 2012 04:54 AM|LINK
hello Syukna, thanks for your reply. i tried this but i can't call .ButtonPH in my page load. is there anything else should i do like delegation? if so, how do i do that? thanks in advance. jim.
jim_rosacena...
Member
31 Points
89 Posts
Re: How to access user control (ucButtons.ascx) events defined in master page from content page
Jul 31, 2012 04:34 AM|LINK
any more idea guys? thanks in advance. :)
jim_rosacena...
Member
31 Points
89 Posts
Re: How to access user control (ucButtons.ascx) events defined in master page from content page
Jul 31, 2012 04:36 AM|LINK
...
2pac
Participant
1586 Points
269 Posts
Re: How to access user control (ucButtons.ascx) events defined in master page from content page
Jul 31, 2012 06:21 AM|LINK
Hi Jim,
Please refer to en example below, which shows how to create a custom event on user control
//CustomButtonControl.ascx.cs code behind protected override void OnInit(EventArgs e) { Button1.Click += CustonButtonClickEvent; base.OnInit(e); } public event EventHandler MyCustomButtonClick; protected void CustonButtonClickEvent(Object sender, EventArgs e) { MyCustomButtonClick(sender, e); }Here is how we use it in the other content page
//default.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="TestApplication._default" %> <%@ Register Src="~/CustomButtonControl.ascx" TagPrefix="uc" TagName="CustomButtonControl" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form runat="server" ID="some"> <uc:CustomButtonControl runat="server" ID="ucCustomButtonControl"/> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </form> </body> </html>//default.aspx.cs protected void Page_Load(object sender, EventArgs e) { ucCustomButtonControl.MyCustomButtonClick += new EventHandler(MyCustomButtonClick); } protected void MyCustomButtonClick(object sender, EventArgs e) { Label1.Text = "Text Changed"; }In the above example, when the custom user contrrol button is clicked "Label1.Text" property is changed in "MyCustomButtonClick" event, which is created in the user control. Above example is tested and working perfectly from my end, Hope this helps you in anyway.
Regards,
Jayesh
jim_rosacena...
Member
31 Points
89 Posts
Re: How to access user control (ucButtons.ascx) events defined in master page from content page
Jul 31, 2012 07:40 AM|LINK
hello 2pac, thanks for your codes and ideas. its pretty clear to me. i have a question, what if i defined the custombuttoncontrol in my master page which is Site.Master <uc:CustomButtonControl runat="server" ID="ucCustomButtonControl"/>, can i can call those controls in my content page (default.aspx) and define an EventHandler inside default.aspx?
2pac
Participant
1586 Points
269 Posts
Re: How to access user control (ucButtons.ascx) events defined in master page from content page
Jul 31, 2012 08:04 AM|LINK
Yes you can, Please follow the example below:
//Site1.Master <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="TestApplication.Site1" %> <%@ Register Src="~/CustomButtonControl.ascx" TagPrefix="uc" TagName="CustomButtonControl" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <asp:ContentPlaceHolder ID="head" runat="server"> </asp:ContentPlaceHolder> </head> <body> <form id="form1" runat="server"> <div> <uc:CustomButtonControl runat="server" ID="ucCustomButtonControl" /> <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> </div> </form> </body> </html>Code behind
//Site1.Master.cs public partial class Site1 : System.Web.UI.MasterPage { protected void Page_Load(object sender, EventArgs e) { } //expose user control to other content pages public CustomButtonControl MyButtonControl { get { return ucCustomButtonControl; } } }In content Page
//ContentPage.aspx <%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="ContentPage.aspx.cs" Inherits="TestApplication.ContentPage" %> <%@ MasterType virtualPath="~/Site1.Master"%> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </asp:Content>//ContentPage.aspx.cs public partial class ContentPage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { var page = Page.Master; if (page != null) { var customButton = Master.MyButtonControl; customButton.MyCustomButtonClick += MyCustomButtonClick; } } protected void MyCustomButtonClick(object sender, EventArgs e) { Label1.Text = "Text Changed"; } }In the content page we have a label "Label1", now we are going to use custom user control's click event to change the label text, by using, @ MasterType directive, such as the one in the example, you can reference members on the master page.
In Master page you need to expose the control:
By doing the above, we can get reference of the user control from master page and use its click event to change the label
Hope this helps
Regards,
Jayesh
jim_rosacena...
Member
31 Points
89 Posts
Re: How to access user control (ucButtons.ascx) events defined in master page from content page
Jul 31, 2012 08:44 AM|LINK
Hello 2pac, you are one of the Genius that i knew, plus you're also very generous. This is EXACTLY what i'm looking for. i successfully applied it and it works perfectly!!! thanks a lot to you.
I have one more question, is there any way to set <%@ MasterType virtualPath="~/Site1.Master"%> programmatically? or inside code behind? the fact is that, i have 2 master pages different structures and i want to change the virtualpath if i wanna use the code you shared.
or can i define 2 different MasterType VirtualPath at the same time?
<%@ MasterType virtualPath="~/Site1.Master"%>
<%@ MasterType virtualPath="~/Preview.Master"%>
i tried that format with 2 VirtualPath but it gives me error.