实现观察者模式来执行类中的方法

发布于 2024-12-11 04:48:56 字数 1572 浏览 1 评论 0原文

我正在使用 c# [ASP.NET 2.0 - VS 2005],我想实现观察者模式以在 DropDown 索引更改时触发方法(驻留在类中)。有三个 DropDown 和一个标签控件,当 DropDown 索引发生变化时,它应该实时显示新生成的方案代码。

public sealed class GetSchemeCode:INotifyPropertyChanged
{

    private string _distCode;
    private string _blockCode;
    private string _schmType;


    public string DistCode
    {
        get { return _distCode; }
        set { _distCode = value; }
    }
    public string BlockCode
    {
        get { return _blockCode; }
        set { _blockCode = value; }
    }
    public string SchemeType
    {
        get { return _schmType; }
        set { _schmType = value; }
    }


    public GetSchemeCode()
    {
        //
        // TODO: Add constructor logic here
        //
    }


    protected string GetNewSchemeCode()
    {
        SqlCommand cmdSchmCode = new SqlCommand("GenerateSchemeCode", dbConnection.cn);
        try
        {
            cmdSchmCode.CommandType = System.Data.CommandType.StoredProcedure;
            //Add Parameters
            cmdSchmCode.Parameters.AddWithValue("@districtCode", DistCode.ToString());
            cmdSchmCode.Parameters.AddWithValue("@blockCode", BlockCode.ToString());
            cmdSchmCode.Parameters.AddWithValue("@schemeType", SchemeType.ToString());
            dbConnection.OpenConnection("Scheme");
            return cmdSchmCode.ExecuteScalar();
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            cmdSchmCode.Dispose();
            dbConnection.CloseConnection();
        }
    }

}

I am using c# [ASP.NET 2.0 - VS 2005] and I want to implement Observer Pattern to fire a method (residing in a class) as and when DropDown index changes. There are three DropDowns and a Label Control which should display newly generated scheme code in real-time, as and when DropDown index changes.

public sealed class GetSchemeCode:INotifyPropertyChanged
{

    private string _distCode;
    private string _blockCode;
    private string _schmType;


    public string DistCode
    {
        get { return _distCode; }
        set { _distCode = value; }
    }
    public string BlockCode
    {
        get { return _blockCode; }
        set { _blockCode = value; }
    }
    public string SchemeType
    {
        get { return _schmType; }
        set { _schmType = value; }
    }


    public GetSchemeCode()
    {
        //
        // TODO: Add constructor logic here
        //
    }


    protected string GetNewSchemeCode()
    {
        SqlCommand cmdSchmCode = new SqlCommand("GenerateSchemeCode", dbConnection.cn);
        try
        {
            cmdSchmCode.CommandType = System.Data.CommandType.StoredProcedure;
            //Add Parameters
            cmdSchmCode.Parameters.AddWithValue("@districtCode", DistCode.ToString());
            cmdSchmCode.Parameters.AddWithValue("@blockCode", BlockCode.ToString());
            cmdSchmCode.Parameters.AddWithValue("@schemeType", SchemeType.ToString());
            dbConnection.OpenConnection("Scheme");
            return cmdSchmCode.ExecuteScalar();
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            cmdSchmCode.Dispose();
            dbConnection.CloseConnection();
        }
    }

}

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

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

发布评论

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

评论(2

北渚 2024-12-18 04:48:56

执行类似下面的代码的操作来连接下拉列表的选定索引更改属性。我相信这就是 Asp.Net 在幕后对观察者模式的实现。您可以在代码或 html 标记中设置 AutoPostBack 属性和事件连接。

public GetSchemeCode()
{
        DistCodeDropDownList.AutoPostBack = true;
        DistCodeDropDownList.SelectedIndexChanged += new EventHandler(DistCodeDropDownList_SelectedIndexChanged);

        // TODO: Hook up the other DropDownLists here. as well
}

    void DistCodeDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        CodeOutputLabel.Text = GetNewSchemeCode();
    }

Do something like the code below to hook up the Dropdown list's selected index changed property. That is Asp.Net's implementation of the Observer pattern behind the scenes I believe. You can set the AutoPostBack property and the Event hookup either in code or in the html markup.

public GetSchemeCode()
{
        DistCodeDropDownList.AutoPostBack = true;
        DistCodeDropDownList.SelectedIndexChanged += new EventHandler(DistCodeDropDownList_SelectedIndexChanged);

        // TODO: Hook up the other DropDownLists here. as well
}

    void DistCodeDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        CodeOutputLabel.Text = GetNewSchemeCode();
    }
半暖夏伤 2024-12-18 04:48:56

C#已经取代了观察者模式,它有事件;事件是观察者模式的语言级实现。您想要做的是在您的对象上创建一个事件,然后让您的观察者订阅它。

C# has superseded the Observer pattern, it has events; events are a language level implementation of the Observer pattern. What you want to do is create an event on your object and then make your observers subscribe to it.

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