C# 从用户控件调用父页中的方法
我在C# 从用户控件调用父页中的方法,c#,asp.net,user-controls,webusercontrol,C#,Asp.net,User Controls,Webusercontrol,我在aspx页面中注册了一个用户控件 当用户控件中的按钮发生单击事件时,如何调用父页面的codebehind中的方法 谢谢。将页面强制转换为项目中的特定页面: ((MyPageName)this.Page).CustomMethod() 我建议您不要直接调用page方法,因为这样会将控件绑定到特定页面 而是公开一个事件,并让页面订阅它。它适用于任意数量的页面,当控件在单个页面上多次(甚至可能在列表上)时更容易使用,并且更符合asp.control设计。以下是使用Freddy Rios(来自we
aspx
页面中注册了一个用户控件
当用户控件中的按钮发生单击事件时,如何调用父页面的codebehind中的方法
谢谢。将页面强制转换为项目中的特定页面:
((MyPageName)this.Page).CustomMethod()
我建议您不要直接调用page方法,因为这样会将控件绑定到特定页面
而是公开一个事件,并让页面订阅它。它适用于任意数量的页面,当控件在单个页面上多次(甚至可能在列表上)时更容易使用,并且更符合asp.control设计。以下是使用Freddy Rios(来自web应用程序项目)建议的事件的经典示例。这假设您希望使用现有的委托而不是自己的委托,并且您没有通过事件args传递任何特定于父页面的内容 在用户控件的代码隐藏中(如果不使用代码隐藏或C#,则根据需要进行调整): 在页面本身中,您可以使用以下内容订阅事件:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// hook up event handler for exposed user control event
MyUserControl.UserControlButtonClicked += new
EventHandler(MyUserControl_UserControlButtonClicked);
}
private void MyUserControl_UserControlButtonClicked(object sender, EventArgs e)
{
// ... do something when event is fired
}
}
void btnSave_Click(object sender, EventArgs e)
{
//123 is test integer for CategoryID
_LoadEditCategory(123);
}
有一篇关于从用户控件到父页面的事件冒泡的有用文章,详细介绍了以下方面提供的答案:
- 您可以使用会话。当您单击用户控件中的按钮时,当您想调用父页面方法时,请在按钮的事件处理程序中单击“将值设置为”
Session["CallParent"]="someValue";
as按钮将发回页面。在父页面的页面加载事件上添加它
protected void Page_Load(object sender,EventArgs e)
{
if(Session["CallParent"]!=null)
{
// here call the Parent Page method
Method1();
// now make the session value null
Session["CallParent"]=null;
}
}
它的效率要高得多遵循良好和适当的方法 使用事件和委托概念使委托与父页中的方法具有相同的签名,然后在父页加载事件中将父方法分配给它,然后通过用户控件中的事件调用此委托 下面给出了代码示例和链接
//Parent Page aspx.cs part
public partial class getproductdetails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ucPrompt.checkIfExist += new uc_prompt.customHandler(MyParentMethod);
}
bool MyParentMethod(object sender)
{
//Do Work
}
}
//User control parts
public partial class uc_prompt : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public delegate bool customHandler(object sender);
public event customHandler checkIfExist;
protected void btnYes_Click(object sender, EventArgs e)
{
checkIfExist(sender);
}
}
阅读更多有关其工作原理的详细信息(参考):-
我想做这个场景:
- 调用
方法(父方法)LoadEditCategory
- 父方法(
)需要一个LoadEditCategory
参数(int
)CategoryID
- 子用户控件是位于同一父页文件夹中的
RightControlPanel
操作
(\u LoadEditCategory
)
public Action\u LoadEditCategory=null代码>
是int
参数(CategoryID
)
2-在按钮事件(btnSave
按钮名称)中使用此操作
,如下所示:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// hook up event handler for exposed user control event
MyUserControl.UserControlButtonClicked += new
EventHandler(MyUserControl_UserControlButtonClicked);
}
private void MyUserControl_UserControlButtonClicked(object sender, EventArgs e)
{
// ... do something when event is fired
}
}
void btnSave_Click(object sender, EventArgs e)
{
//123 is test integer for CategoryID
_LoadEditCategory(123);
}
父页面或父用户控件
3-添加父方法
private void LoadEditCategory(int CategoryID)
{
// CategoryID is 123 in my example
//Do some things with CategoryID
}
4-加载子用户控件时添加此代码(RightControlPanel
)
我喜欢Stephen M.Redd的答案,必须将其转换为VB。在这里分享。欢迎您的建议
在用户控件的代码隐藏中:
Public Class MyUserControl
Inherits System.Web.UI.UserControl
'expose the event publicly
Public Event UserControlButtonClicked As EventHandler
'a method to raise the publicly exposed event
Private Sub OnUserControlButtonClick()
RaiseEvent UserControlButtonClicked(Me, EventArgs.Empty)
End Sub
Protected Sub lbtnApplyChanges_Click(sender As Object, e As EventArgs) Handles lbtnApplyChanges.Click
'add code to run here, then extend this code by firing off this event
'so it will trigger the public event handler from the parent page,
'and the parent page can hanlde it
OnUserControlButtonClick()
End Sub
End Class
在父页面中订阅事件,因此当引发事件时,代码将在此处运行
Public Class SalesRecord
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'hook up event handler for exposed user control event to handle this
AddHandler MyUserControl.UserControlButtonClicked, AddressOf MyUserControl_UserControlButtonClicked
End Sub
''' <summary>
''' code to run when user clicks 'lbtnApplyChanges' on the user control
''' </summary>
Private Sub MyUserControl_UserControlButtonClicked()
'this code will now fire when lbtnApplyChanges_Click executes
End Sub
End Class
公共类SalesRecord
继承System.Web.UI.Page
受保护的子页加载(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Load
'为公开的用户控件事件连接事件处理程序以处理此事件
AddHandler MyUserControl.UserControlButtonClicked,MyUserControl\u UserControlButtonClicked的地址
端接头
'''
当用户单击用户控件上的“lbtnApplyChanges”时要运行的“”代码
'''
私有子MyUserControl\u UserControlButtonClicked()
'当lbtnApplyChanges\u单击执行时,此代码现在将触发
端接头
末级
我在这里发布了一些有用的东西:
或者,如果希望访问控件,也可以执行以下操作:
更新父页面
其中父级有一个名为“UpdatePanel1”的更新面板
控制
UpdatePanel onParent1 = (UpdatePanel)Parent.FindControl("UpdatePanel1");
onParent1.Update();
@亨克:当然,这违反了最佳实践,但如果提问者不理解这样简单的事情是如何运作的,那么他/她以后又如何理解更大的事情呢。总是最好先用最简单的方法,即使是错误的,也只是为了学习。我同意雷克斯,这就是提问者所问的问题,回答这个问题是很好的,而不是总是说问题是错的。斯蒂芬,一个有帮助且简洁的回答+1.我希望我能给你+2:)如果我想返回一些东西给usercontrol,并点击一个非无效的MyUserControl\u UserControlButtonClick?这是+1的完美方式…:)我认为这不是100%正确。我认为在Page_Load()中MyUserControl.UserControlButtonClicked+=newEventHandler(MyUserControl_UserControlButtonClicked);应替换为ControlName.UserControlButtonClicked+=新事件处理程序(MyUserControl_UserControlButtonClicked);-应该使用特定控件的名称,而不是类名。
UpdatePanel onParent1 = (UpdatePanel)Parent.FindControl("UpdatePanel1");
onParent1.Update();