如何ASP.net自定义控件之间进行通信 [英] How to communicate between ASP.net custom controls

查看:112
本文介绍了如何ASP.net自定义控件之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的两个自定义控件。一集的标准进行搜索,而另一个显示一个列表。这两个控件需要保持单独的暂时。

I'm using two custom controls. One gathers the criteria for a search, and the other displays a list. These two controls need to remain seperate for the time being.

什么是从搜索控制传输数据,列表控件的最好方法?

What is the best method of transfering data from the search control, to the list control?

我想的ViewState,会议或两者的UpdatePanel内包装和使用自定义事件?

I'm thinking of ViewState, Session or wrapping both within a UpdatePanel and using custom events??

推荐答案

也许你可以创建一个代表,并通过searchvalues​​列表的事件?这样,您就可以轻松地添加另一个或多重显示控制的情况下,是有史以来成为必要。

Maybe you can create a delegate and an event to pass a list of searchvalues? This way you can easily add another or multiple display controls in case that ever becomes necessary.

请注意,这只是一些简单的示例code,应该优化/改进。

Note that this is just some quick sample code that should be optimized/improved.

public class SearchControl
{
    public delegate void SearchEventHandler(object sender, Dictionary<string, string> SearchValues);
    public event SearchEventHandler OnSearch;

    public SearchControl()
    {
        btnSearch.Click += new EventHandler(Search);
    }

    protected void Search(object sender, EventArgs e)
    {
        if (OnSearch != null)
        {
            Dictionary<string, string> searchValues = new Dictionary<string, string>();
            searchValues.Add("name", "John");
            searchValues.Add("age", "24");

            OnSearch(this, searchValues);
        }
    }
}

public class DisplayControl
{
    public void ShowResults(Dictionary<string, string> SearchValues)
    {
        // Some logic here...
    }
}

public class YourWebPage
{
    SearchControl searcher = new SearchControl();
    DisplayControl displayer = new DisplayControl();

    public YourWebPage()
    {
        searcher.OnSearch += new SearchControl.SearchEventHandler(searcher_OnSearch);
    }

    public void searcher_OnSearch(object sender, Dictionary<string, string> SearchValues)
    {
        displayer.ShowResults(SearchValues);
    }
}

这篇关于如何ASP.net自定义控件之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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