There's more than question and article about the same exact question but I have a couple more related questions and was hoping to get some answers.
关于同样的问题不仅仅是问题和文章,而是我还有一些相关的问题,希望得到一些答案。
-
I've heard of two approaches to find the button and add the handler or use an interface (Check both approaches from here) .. Which one do you suggest ?
我听说有两种方法可以找到按钮并添加处理程序或使用界面(从这里检查两种方法)。你建议哪一种?
-
If you could please illustrate the 'Interface' option with some code and where to class the interface file cause it's not readable in the page when I try to inherit it!
如果你能用一些代码来说明'Interface'选项,那么在接口文件的类中,当我尝试继承它时,它在页面中是不可读的!
2 个解决方案
#1
7
Second aproach is IMO better. The first choice couples a page to the specific master page, and it is not nice.
第二个方法是IMO更好。第一个选择是将页面耦合到特定的母版页,这并不好。
All files are placed in the same folder.
所有文件都放在同一个文件夹中。
IPageInterface.cs:
namespace CallFromMasterPage
{
public interface IPageInterface
{
void DoSomeAction();
}
}
Default.aspx.cs:
namespace CallFromMasterPage
{
public partial class Default : System.Web.UI.Page, IPageInterface
{
public void DoSomeAction()
{
throw new NotImplementedException();
}
}
}
Site.Master.cs:
namespace CallFromMasterPage
{
public partial class SiteMaster : System.Web.UI.MasterPage
{
protected void Button1_Click(object sender, EventArgs e)
{
IPageInterface pageInterface = Page as IPageInterface;
if (pageInterface != null)
{
pageInterface.DoSomeAction();
}
}
}
}
There are other approaches. E.g. you can publish an event via event broker.
还有其他方法。例如。您可以通过活动经纪人发布活动。
#2
0
See From My Openion it will be best if you make use of event handlers...and even with the custom delegate..
请参阅From My Openion,最好使用事件处理程序......甚至使用自定义委托。
like this
public delegate ReturnType MasterPageButtonHandler(CustomEventArgs ObjPriargs);
public event MasterPageButtonHandler MasterPagebuttonClick;
.
.
.
.
Button.click+=new EventHandler(Button1_Click);
.
.
.
protected void Button1_Click(Object sender,EventArgs e)
{
if(MasterPagebuttonClick!=null)
{
CustomEventArgs ObjPriargs=new CustomEventArgs();
ObjPriargs.Property1=SomeValu1;
ObjPriargs.Property2=SomeValu2;
MasterPagebuttonClick.Invoke(ObjPriargs);
}
}
.
.
.
public class CustomEventArgs
{
Public DataType Property1{get;set;}
Public DataType Property2{get;set;}
Public DataType Property3{get;set;}
}
.
.
.
// Now in your aspx Page
MyMaster m=Page.Master as MyMaster;
m.MasterPagebuttonClick+=new MasterPageButtonHandler(MasterPageHandler_Click);
.
.
.
protected void MasterPageHandler_Click(CustomEventArgs ObjPriargs)
{
//You code/////
}
going through this manner give some flexibility like in case if in future you want to pass some data tour content page when clicked..its easily possible.
通过这种方式提供一些灵活性,如果在将来你想要在点击时传递一些数据旅游内容页面的情况下很容易。
#1
7
Second aproach is IMO better. The first choice couples a page to the specific master page, and it is not nice.
第二个方法是IMO更好。第一个选择是将页面耦合到特定的母版页,这并不好。
All files are placed in the same folder.
所有文件都放在同一个文件夹中。
IPageInterface.cs:
namespace CallFromMasterPage
{
public interface IPageInterface
{
void DoSomeAction();
}
}
Default.aspx.cs:
namespace CallFromMasterPage
{
public partial class Default : System.Web.UI.Page, IPageInterface
{
public void DoSomeAction()
{
throw new NotImplementedException();
}
}
}
Site.Master.cs:
namespace CallFromMasterPage
{
public partial class SiteMaster : System.Web.UI.MasterPage
{
protected void Button1_Click(object sender, EventArgs e)
{
IPageInterface pageInterface = Page as IPageInterface;
if (pageInterface != null)
{
pageInterface.DoSomeAction();
}
}
}
}
There are other approaches. E.g. you can publish an event via event broker.
还有其他方法。例如。您可以通过活动经纪人发布活动。
#2
0
See From My Openion it will be best if you make use of event handlers...and even with the custom delegate..
请参阅From My Openion,最好使用事件处理程序......甚至使用自定义委托。
like this
public delegate ReturnType MasterPageButtonHandler(CustomEventArgs ObjPriargs);
public event MasterPageButtonHandler MasterPagebuttonClick;
.
.
.
.
Button.click+=new EventHandler(Button1_Click);
.
.
.
protected void Button1_Click(Object sender,EventArgs e)
{
if(MasterPagebuttonClick!=null)
{
CustomEventArgs ObjPriargs=new CustomEventArgs();
ObjPriargs.Property1=SomeValu1;
ObjPriargs.Property2=SomeValu2;
MasterPagebuttonClick.Invoke(ObjPriargs);
}
}
.
.
.
public class CustomEventArgs
{
Public DataType Property1{get;set;}
Public DataType Property2{get;set;}
Public DataType Property3{get;set;}
}
.
.
.
// Now in your aspx Page
MyMaster m=Page.Master as MyMaster;
m.MasterPagebuttonClick+=new MasterPageButtonHandler(MasterPageHandler_Click);
.
.
.
protected void MasterPageHandler_Click(CustomEventArgs ObjPriargs)
{
//You code/////
}
going through this manner give some flexibility like in case if in future you want to pass some data tour content page when clicked..its easily possible.
通过这种方式提供一些灵活性,如果在将来你想要在点击时传递一些数据旅游内容页面的情况下很容易。