简体   繁体   English

如何从用户控制页面访问按钮

[英]How to access Buttons from User Control Page

I have a User Control ContactUC , with a button PageButton . 我有一个带有按钮PageButton的用户控件ContactUC

public Button PageButton
{
    get { return this.ProjectManagerOperation; }
    set { this.ProjectManagerOperation = value; }
}

I want to use this button in another page called AdminPage 我想在另一个名为AdminPage页面中使用此按钮

I have put this code into the AdminPage.aspx.cs 我已将此代码放入AdminPage.aspx.cs

<%@ Register Src="~/ContactUC.ascx" TagPrefix="uc1" TagName="ContactUC" %>  

to access the User Control ContactUC . 访问用户控件ContactUC

Then in the AdminPage , I do : 然后在AdminPage ,我执行以下操作:

ContactUC c = new ContactUC();

Now I want to access the button action when the user clicked it. 现在,我想在用户单击按钮时访问按钮动作。

Instead of accessing button, why not move whatever button does to some public void method : 除了访问按钮之外,为什么不将按钮执行的操作移到某些public void方法上:

public void SomeButtonClick()
{
//Do magic
}

and then you can access it like that 然后您可以像这样访问它

ContactUC c = new ContactUC();
c.SomeButtonClick();

or in some cases 或在某些情况下

ContactUC.SomeButtonClick(); ContactUC.SomeButtonClick();

might work as well 可能也可以

Unless there's some reason you want to access actual UI element and not the action it does (even if there's almost never reason to do that). 除非出于某种原因,否则您想要访问实际的UI元素而不是它要执行的操作(即使几乎没有理由这样做)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM