gpt4 book ai didi

c# - 将事件处理程序添加到中继器中的用户控件

转载 作者:行者123 更新时间:2023-11-30 12:43:08 29 4
gpt4 key购买 nike

我有一个 asp.net 网络应用程序。我创建了一个用户控件。用户控件有一个父页面(.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"  />

如果我在 Page_Load 上调用它,这里是 .aspx.cs 部分:

    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);
}

这就是我在绑定(bind)不起作用的中继器时所尝试的:

    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) 以了解转发器的事件生命周期。基本上,您需要在创建转发器项目时连接 Web 用户控件的事件,而不是在绑定(bind)时,这显然发生在创建之后。以下代码应该适合您:

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);
}
}

关于c# - 将事件处理程序添加到中继器中的用户控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31976944/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com