帮助 C# 事件监听和用户控件

发布于 2024-09-04 04:46:16 字数 1680 浏览 6 评论 0原文

好的,我有一个页面,上面有一个列表视图。列表视图的项目模板内部是一个用户控件。该用户控件正在尝试触发一个事件,以便托管页面可以侦听它。我的问题是,由于处理程序为空,因此未触发该事件。 (即 EditDateRateSelected 是我的处理程序,调试时它为空)

   protected void lnkEditDate_Click(object sender, EventArgs e)
    {
            if (EditDateRateSelected != null)
            EditDateRateSelected(Convert.ToDateTime(((LinkButton)frmViewRatesDate.Row.FindControl("lnkEditDate")).Text)); 
    }

在我的 listvew 的项目数据绑定上是我添加事件处理程序的位置

protected void PropertyAccommodationRates1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        UserControls_RatesEditDate RatesViewDate1 = (UserControls_RatesEditDate)e.Item.FindControl("RatesViewDate1");
        RatesViewDate1.EditDateRateSelected += new UserControls_RatesEditDate.EditDateRateEventHandler(RatesEditDate1_EditDateRateSelected);

        RatesViewDate1.PropertyID = (int)Master.PropertyId;
        if (!String.IsNullOrEmpty(Accommodations1.SelectedValue))
        {
            RatesViewDate1.AccommodationTypeID = Convert.ToInt32(Accommodations1.SelectedValue);
        }
        else
        {
            RatesViewDate1.AccommodationTypeID = 0;
        }

        RatesViewDate1.Rate = (PropertyCMSRate)((ListViewDataItem)e.Item).DataItem;
    }
}

如果控件位于页面内部并且在页面加载时我有以下内容,那么我的事件代码一切正常line:

RatesEditDate1.EditDateRateSelected += new UserControls_RatesEditDate.EditDateRateEventHandler(RatesEditDate1_EditDateRateSelected);

但显然我需要监听列表视图控件内的事件。

任何建议将不胜感激。我尝试将列表视图的 EnableViewState 设置为 true,但这并没有产生任何影响。我应该在其他地方连接控制处理程序吗?

注意 - 如果我的术语有误并且我将代表称为处理程序等,请道歉:)

OK so I have a page which has a listview on it. Inside the item template of the listview is a usercontrol. This usercontrol is trying to trigger an event so that the hosting page can listen to it. My problem is that the event is not being triggered as the handler is null. (ie. EditDateRateSelected is my handler and its null when debugging)

   protected void lnkEditDate_Click(object sender, EventArgs e)
    {
            if (EditDateRateSelected != null)
            EditDateRateSelected(Convert.ToDateTime(((LinkButton)frmViewRatesDate.Row.FindControl("lnkEditDate")).Text)); 
    }

On the item data bound of my listvew is where I'm adding my event handlers

protected void PropertyAccommodationRates1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        UserControls_RatesEditDate RatesViewDate1 = (UserControls_RatesEditDate)e.Item.FindControl("RatesViewDate1");
        RatesViewDate1.EditDateRateSelected += new UserControls_RatesEditDate.EditDateRateEventHandler(RatesEditDate1_EditDateRateSelected);

        RatesViewDate1.PropertyID = (int)Master.PropertyId;
        if (!String.IsNullOrEmpty(Accommodations1.SelectedValue))
        {
            RatesViewDate1.AccommodationTypeID = Convert.ToInt32(Accommodations1.SelectedValue);
        }
        else
        {
            RatesViewDate1.AccommodationTypeID = 0;
        }

        RatesViewDate1.Rate = (PropertyCMSRate)((ListViewDataItem)e.Item).DataItem;
    }
}

My event code all works fine if the control is inside the page and on page load I have the line:

RatesEditDate1.EditDateRateSelected += new UserControls_RatesEditDate.EditDateRateEventHandler(RatesEditDate1_EditDateRateSelected);

But obviously I need listen for events inside the listviewcontrols.

Any advice would be greatly appreciated. I have tried setting EnableViewState to true for my listview but that hasn't made a difference. Is there somewhere else I'm supposed to be wiring up the control handler?

Note - apologies if I've got my terminology wrong and I'm referring to delegates as handlers and such :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风无影 2024-09-11 04:46:16

好的,我尝试将事件连接到源中的用户控件,如下所示:

<uc1:RatesEditDate ID="RatesViewDate1" runat="server" OnEditDateRateSelected="RatesEditDate1_EditDateRateSelected" /> 

然后发现它抱怨由于其保护级别而无法访问它。但在我的用户控件内 - 我已将我的委托和事件公开???

public delegate void EditDateRateEventHandler(DateTime theDateTime); 
public event EditDateRateEventHandler EditDateRateSelected; 

事实证明,在我的托管页面中 - 我的事件处理程序没有在其上定义可访问性(因此是私有的) - 因此需要对其进行保护,嘿,普雷斯托很高兴!

问题解决了!

OK I tried wiring the event to my user control in the source like so:

<uc1:RatesEditDate ID="RatesViewDate1" runat="server" OnEditDateRateSelected="RatesEditDate1_EditDateRateSelected" /> 

and then found it complaining about it being inaccessible due to its protection level. Inside my usercontrol though - I've made my delegate and event public ???

public delegate void EditDateRateEventHandler(DateTime theDateTime); 
public event EditDateRateEventHandler EditDateRateSelected; 

Turns out that within my hosting page - my event handler had no accessiblity defined on it (so was private) - so it needed to be made protected and hey presto was happy!

Problem solved!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文