c# – 将事件处理程序添加到转发器中的用户控件
发布时间:2020-12-15 22:18:25 所属栏目:百科 来源:网络整理
导读:我有一个asp.net Web应用程序.我创建了一个用户控件.用户控件具有父页面(.aspx文件)可以调用的事件处理程序.该.aspx页面使用转发器来生成多个用户控件.如果我将一个用户控件放在转发器之外并在Page_Load中添加事件处理程序,它将按照我想要的方式工作.如果我
我有一个asp.net Web应用程序.我创建了一个用户控件.用户控件具有父页面(.aspx文件)可以调用的事件处理程序.该.aspx页面使用转发器来生成多个用户控件.如果我将一个用户控件放在转发器之外并在Page_Load中添加事件处理程序,它将按照我想要的方式工作.如果我尝试在转发器中创建的控件,则不要调用我的事件.我会尽可能地删除下面的代码示例.
部分用户控制.ascx.cs文件: public event EventHandler UserControlUploadButtonClicked; private void OnUserControlUploadButtonClick() { if (UserControlUploadButtonClicked != null) { //Makes it to this line if control is created outside repeater //controls created in repeater are null so this line not executed UserControlUploadButtonClicked(this,EventArgs.Empty); } } protected void TheButton_Click(object sender,EventArgs e) { // .... do stuff then fire off the event OnUserControlUploadButtonClick(); } 重要的用户控件标记: <asp:ImageButton runat="server" ID="imbUploadFile" ImageUrl="~/images/status_red.png" ToolTip="Upload File" onclick="TheButton_Click" /> 这是.aspx.cs部分,如果我在Page_Load上调用它: protected void Page_Load(object sender,EventArgs e) { if (!IsPostBack) { LoadDDLs();Load drop down lists for filter for other UI stuff } This works! this.ucTest.UserControlUploadButtonClicked += new EventHandler(ManageUploader); } 这是我正在尝试绑定无效的转发器: protected void rptMonthlyFiles_ItemDataBound(Object Sender,RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DataRowView drv = e.Item.DataItem as DataRowView; //there are one of these for each month cut the rest out to make smaller code sample //Below gets DB info and sets up user control properly for UI based on business rules ((NRTWebApplication.UserControls.ucMonthlyFile2)e.Item.FindControl("ucOctMonthlyFile")).GetFileInfo(); // This is how I am adding the event handler for each user control and it does not work like if done in Page_Load ((NRTWebApplication.UserControls.ucMonthlyFile2)e.Item.FindControl("ucOctMonthlyFile")).UserControlUploadButtonClicked += new EventHandler(ManageUploader); } } 这是通过单击用户控件引发事件时父页面.aspx应该执行的操作的占位符: private void ManageUploader(object sender,EventArgs e) { // ... do something when event is fired this.labTest.Text = "After User Control Button Clicked"; } 坚持这一点任何和所有帮助非常感谢! 解决方法
看看这个(
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater_events(v=vs.110).aspx),了解Repeater的事件生命周期.基本上,您需要在创建转发器项目时连接Web用户控件的事件,而不是在绑定时绑定,这显然在创建后发生.以下代码应该适合您:
protected void rptMonthlyFiles_ItemCreated(Object Sender,RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DataRowView drv = e.Item.DataItem as DataRowView; //there are one of these for each month cut the rest out to make smaller code sample //Below gets DB info and sets up user control properly for UI based on business rules ((NRTWebApplication.UserControls.ucMonthlyFile2)e.Item.FindControl("ucOctMonthlyFile")).GetFileInfo(); // This is how I am adding the event handler for each user control and it does not work like if done in Page_Load ((NRTWebApplication.UserControls.ucMonthlyFile2)e.Item.FindControl("ucOctMonthlyFile")).UserControlUploadButtonClicked += new EventHandler(ManageUploader); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |