帮助c#事件侦听和用户控制 [英] Help with c# event listening and usercontrols

查看:105
本文介绍了帮助c#事件侦听和用户控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,所以我有一个页面,它有一个列表。在listview的项目模板里面是一个用户控件。此用户控件正在尝试触发事件,以便托管页面可以收听。我的问题是事件没有被触发,因为处理程序是空的。 (即EditDateRateSelected是我的处理程序,调试时为空)

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

但显然我需要在listviewcontrols中收听事件。

But obviously I need listen for events inside the listviewcontrols.

任何建议将不胜感激。我已经尝试将EnableViewState设置为true为我的列表视图,但没有改变。有没有其他地方我应该连接控制处理程序?

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 :)

推荐答案

确定我尝试在源代码中将事件连接到我的用户控件,如下所示:

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; 

事实证明,在我的托管页面中 - 我的事件处理程序没有定义可访问性(所以是私有的) - 所以它需要被保护,嘿presto是快乐的!

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!

问题解决!

这篇关于帮助c#事件侦听和用户控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆