实现观察者模式来执行类中的方法
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
执行类似下面的代码的操作来连接下拉列表的选定索引更改属性。我相信这就是 Asp.Net 在幕后对观察者模式的实现。您可以在代码或 html 标记中设置 AutoPostBack 属性和事件连接。
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.
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.