动态接受消息的问题
现在碰到问题:触发web的一个方法,但是无法再方法中修改页面
想用委托写个接受消息的例子:
1.MessageEventArgs(是事件参数)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary>
/// 消息中心的Args
/// </summary>
public class MessageEventArgs:EventArgs
{
/// <summary>
/// 接受消息的用户名
/// </summary>
private string userName;
public string UserName
{
get { return userName; }
set { userName = value; }
}
/// <summary>
/// 消息内容
/// </summary>
private string msg;
public string Msg
{
get { return msg; }
set { msg = value; }
}
public MessageEventArgs(string userName, string msg)
{
this.userName = userName;
this.msg = msg;
}
}
/// 消息中心的Args
/// </summary>
public class MessageEventArgs:EventArgs
{
/// <summary>
/// 接受消息的用户名
/// </summary>
private string userName;
public string UserName
{
get { return userName; }
set { userName = value; }
}
/// <summary>
/// 消息内容
/// </summary>
private string msg;
public string Msg
{
get { return msg; }
set { msg = value; }
}
public MessageEventArgs(string userName, string msg)
{
this.userName = userName;
this.msg = msg;
}
}
2.MessageManage类消息管理
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class MessageManage
{
private static MessageManage m;
/// <summary>
/// 创建一个私有的构造函数(使其单例)
/// </summary>
private MessageManage()
{
}
//只能通过这个方法访问此类的对象
public static MessageManage Get()
{
if (m == null)
{
m = new MessageManage();
}
return m;
}
/// <summary>
/// 消息处理委托
/// </summary>
/// <param name="sender">事件触发者</param>
/// <param name="e">事件参数</param>
public delegate void MessageDelegate(object sender, MessageEventArgs e);
/// <summary>
/// 消息事件
/// </summary>
public event MessageDelegate MessageEvent;
/// <summary>
/// 触发事件
/// </summary>
/// <param name="e">事件参数</param>
protected virtual void OnMessageEvent(MessageEventArgs e)
{
MessageEvent(this, e);
}
/// <summary>
/// 有消息进入
/// </summary>
/// <param name="msg">消息内容</param>
/// <param name="useName">消息接受者</param>
public void ShowMessage(string msg,string useName)
{
MessageEventArgs e=new MessageEventArgs(useName, msg);
OnMessageEvent(e);
}
}
{
private static MessageManage m;
/// <summary>
/// 创建一个私有的构造函数(使其单例)
/// </summary>
private MessageManage()
{
}
//只能通过这个方法访问此类的对象
public static MessageManage Get()
{
if (m == null)
{
m = new MessageManage();
}
return m;
}
/// <summary>
/// 消息处理委托
/// </summary>
/// <param name="sender">事件触发者</param>
/// <param name="e">事件参数</param>
public delegate void MessageDelegate(object sender, MessageEventArgs e);
/// <summary>
/// 消息事件
/// </summary>
public event MessageDelegate MessageEvent;
/// <summary>
/// 触发事件
/// </summary>
/// <param name="e">事件参数</param>
protected virtual void OnMessageEvent(MessageEventArgs e)
{
MessageEvent(this, e);
}
/// <summary>
/// 有消息进入
/// </summary>
/// <param name="msg">消息内容</param>
/// <param name="useName">消息接受者</param>
public void ShowMessage(string msg,string useName)
{
MessageEventArgs e=new MessageEventArgs(useName, msg);
OnMessageEvent(e);
}
}
3.页面后台代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//第一次打开注册事件
MessageManage.Get().MessageEvent += new MessageManage.MessageDelegate(ShowMsg);
}
}
/// <summary>
/// 触发事件后被调用的方法(问题就在这里,虽然调用了,但不能改变页面)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void ShowMsg(object sender, MessageEventArgs e)
{
//此处Seesion["userName"]为123
if (e.UserName == Session["userName"].ToString())
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "ggg", "<script>MyFun('"+e.Msg+"');</script>");
}
}
/// <summary>
/// 这是Ajax方法,调用方法时会触发事件,通知所有注册事件的方法,并调用
/// </summary>
/// <param name="msg"></param>
[AjaxMethod]
public static void Send(string msg)
{
MessageManage.Get().ShowMessage("我发送的","123");
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//第一次打开注册事件
MessageManage.Get().MessageEvent += new MessageManage.MessageDelegate(ShowMsg);
}
}
/// <summary>
/// 触发事件后被调用的方法(问题就在这里,虽然调用了,但不能改变页面)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void ShowMsg(object sender, MessageEventArgs e)
{
//此处Seesion["userName"]为123
if (e.UserName == Session["userName"].ToString())
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "ggg", "<script>MyFun('"+e.Msg+"');</script>");
}
}
/// <summary>
/// 这是Ajax方法,调用方法时会触发事件,通知所有注册事件的方法,并调用
/// </summary>
/// <param name="msg"></param>
[AjaxMethod]
public static void Send(string msg)
{
MessageManage.Get().ShowMessage("我发送的","123");
}
}
现在问题:1:这个思路是不是本身就有问题,问题在哪里,可以改进吗
2:是不是web不发送请求,就不能调用页面的JS方法,或者改变页面元素