如何在ajax调用函数时停止page_load?

时间:2017-08-22 16:45:57

标签: c# asp.net ajax

嗨我遇到问题,只要AJAX计时器调用它的函数,我就会调用我的Page_Load函数。但是我的Page_Load函数中有些东西我只想激活一次,而不是每5秒激活一次(当应该调用AJAX方法时)。如何从AJAX函数中解除Page_Load函数,即只调用一次Page_Load?

由于

protected void Page_Load(object sender, EventArgs e)
{
    if (!ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack)
    {
        SendButton.Click += new EventHandler(SendButton_Click);
        if (Request.QueryString["RecID"] != null)
            Session["receivinguserid"] = Request.QueryString["RecID"];
        else
            Response.Redirect("~/Home.aspx");
        UserCredential sendingUser = (UserCredential)Session["authenticatedUser"];
        UserCredential receivingUser = Global.Users.Find(m => m.UserID == Convert.ToInt32(Session["receivinguserid"]));
        Conversation currentConversation = new Conversation((int)sendingUser.UserID, (int)receivingUser.UserID);
        if (sendingUser.Conversations.Find(m => m.ReceivingUserID == receivingUser.UserID) == null)
        {
            Global.Users.Find(m => m.Name == (string)Session["authenticatedUsersUsername"]).Conversations.Add(new Conversation((int)((UserCredential)Session["authenticatedUser"]).UserID, Convert.ToInt32(Session["receivinguserid"])));
            Global.Users.Find(m => m.UserID == Convert.ToInt32(Session["receivinguserid"])).Conversations.Add(new Conversation(Convert.ToInt32(Session["receivinguserid"]), (int)((UserCredential)Session["authenticatedUser"]).UserID));
        }
        else
        {
            currentConversation = sendingUser.Conversations.Find(m => m.ReceivingUserID == receivingUser.UserID);
            foreach (Message m in currentConversation.ReadMessageList)
            {
                if (m.UserID == sendingUser.UserID)
                {
                    ChatLabel.Text += "<br> " + sendingUser.Name.ToString() + ": " + m.MessageText + " " + m.MessageDate.ToString("dddd, dd MMMM HH:mm");
                }
                else if (m.UserID == receivingUser.UserID)
                {
                    ChatLabel.Text += "<br>" + receivingUser.Name.ToString() + ": " + m.MessageText + " " + m.MessageDate.ToString("dddd, dd MMMM HH:mm");
                }
            }
            foreach (Message m in sendingUser.Conversations.Find(m => m.ReceivingUserID == receivingUser.UserID).UnReadMessageList)
            {
                ChatLabel.Text += "<br>" + receivingUser.Name.ToString() + ": " + m.MessageText + " " + m.MessageDate.ToString("dddd, dd MMMM HH:mm");
                sendingUser.Conversations.Find(t => t.ReceivingUserID == receivingUser.UserID).ReadMessageList.Add(m);
            }
            sendingUser.Conversations.Find(m => m.ReceivingUserID == receivingUser.UserID).UnReadMessageList.Clear();
        }
    }
}

这是AJAX功能:

public void UpdateChat(object sender, EventArgs e)
{
    UserCredential sendingUser = (UserCredential)Session["authenticatedUser"];
    UserCredential receivingUser = Global.Users.Find(m => m.UserID == Convert.ToInt32(Session["receivinguserid"]));
    foreach (Message m in sendingUser.Conversations.Find(c => c.ReceivingUserID == receivingUser.UserID).UnReadMessageList)
    {
        ChatLabel.Text += "<br>" + receivingUser.Name.ToString() + ": " + m.MessageText + " " + m.MessageDate.ToString("ddd, dd MMMM HH:mm");
        Global.Users.Find(u => u.UserID == (int)sendingUser.UserID).Conversations.Find(t => t.ReceivingUserID == receivingUser.UserID).ReadMessageList.Add(m);
    }
    Global.Users.Find(m => m.UserID == (int)sendingUser.UserID).Conversations.Find(t => t.ReceivingUserID == receivingUser.UserID).UnReadMessageList.Clear();
}

这是ScriptManager:

&#13;
&#13;
<a>
		<asp:ScriptManager ID="ScriptManager1" runat="server">
		</asp:ScriptManager>
			<asp:Timer ID="Timer1" OnTick="UpdateChat" Interval="5000" runat="server">
			</asp:Timer>
</a>
&#13;
&#13;
&#13;

我在更新面板中添加了,它似乎捕获了大多数AJAX调用,但偶尔我的代码if(!ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack)似乎运行。这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Chat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        {
            SendButton.Click += new EventHandler(SendButton_Click);
            if (Request.QueryString["RecID"] != null)
                Session["receivinguserid"] = Request.QueryString["RecID"];
            else
                Response.Redirect("~/Home.aspx");
            UserCredential sendingUser = (UserCredential)Session["authenticatedUser"];
            UserCredential receivingUser = Global.Users.Find(m => m.UserID == Convert.ToInt32(Session["receivinguserid"]));
            Conversation currentConversation = new Conversation((int)sendingUser.UserID, (int)receivingUser.UserID);
            if (sendingUser.Conversations.Find(m => m.ReceivingUserID == receivingUser.UserID) == null)
            {
                Global.Users.Find(m => m.Name == (string)Session["authenticatedUsersUsername"]).Conversations.Add(new Conversation((int)((UserCredential)Session["authenticatedUser"]).UserID, Convert.ToInt32(Session["receivinguserid"])));
                Global.Users.Find(m => m.UserID == Convert.ToInt32(Session["receivinguserid"])).Conversations.Add(new Conversation(Convert.ToInt32(Session["receivinguserid"]), (int)((UserCredential)Session["authenticatedUser"]).UserID));
            }
            else
            {
                currentConversation = sendingUser.Conversations.Find(m => m.ReceivingUserID == receivingUser.UserID);
                foreach (Message m in currentConversation.ReadMessageList)
                {
                    if (m.UserID == sendingUser.UserID)
                    {
                        ChatLabel.Text += "<br> " + sendingUser.Name.ToString() + ": " + m.MessageText + " " + m.MessageDate.ToString("dddd, dd MMMM HH:mm");
                    }
                    else if (m.UserID == receivingUser.UserID)
                    {
                        ChatLabel.Text += "<br>" + receivingUser.Name.ToString() + ": " + m.MessageText + " " + m.MessageDate.ToString("dddd, dd MMMM HH:mm");
                    }
                }
                foreach (Message m in sendingUser.Conversations.Find(m => m.ReceivingUserID == receivingUser.UserID).UnReadMessageList)
                {
                    ChatLabel.Text += "<br>" + receivingUser.Name.ToString() + ": " + m.MessageText + " " + m.MessageDate.ToString("dddd, dd MMMM HH:mm");
                    sendingUser.Conversations.Find(t => t.ReceivingUserID == receivingUser.UserID).ReadMessageList.Add(m);
                }
                sendingUser.Conversations.Find(m => m.ReceivingUserID == receivingUser.UserID).UnReadMessageList.Clear();
            }
        }
    }
    public void SendButton_Click(Object sender, EventArgs e)
    {
        UserCredential sendingUser = (UserCredential)Session["authenticatedUser"];
        UserCredential receivingUser = Global.Users.Find(m => m.UserID == Convert.ToInt32(Session["receivinguserid"]));
        Global.Users.Find(m => m.UserID == (int)sendingUser.UserID).Conversations.Find(t => t.ReceivingUserID == receivingUser.UserID).ReadMessageList.Add(new Message(MessageTextBox.Text, (int)sendingUser.UserID, DateTime.Now));
        Global.Users.Find(m => m.UserID == (int)receivingUser.UserID).Conversations.Find(t => t.ReceivingUserID == sendingUser.UserID).UnReadMessageList.Add(new Message(MessageTextBox.Text, (int)sendingUser.UserID, DateTime.Now));
        ChatLabel.Text += "<br>" + sendingUser.Name.ToString() + ": " + MessageTextBox.Text + " " + DateTime.Now.ToString("ddd, dd MMMM HH:mm");
        MessageTextBox.Text = String.Empty;
    }
    public void UpdateChat(object sender, EventArgs e)
    {
        UserCredential sendingUser = (UserCredential)Session["authenticatedUser"];
        UserCredential receivingUser = Global.Users.Find(m => m.UserID == Convert.ToInt32(Session["receivinguserid"]));
        foreach (Message m in sendingUser.Conversations.Find(c => c.ReceivingUserID == receivingUser.UserID).UnReadMessageList)
        {
            ChatLabel.Text += "<br>" + receivingUser.Name.ToString() + ": " + m.MessageText + " " + m.MessageDate.ToString("ddd, dd MMMM HH:mm");
            Global.Users.Find(u => u.UserID == (int)sendingUser.UserID).Conversations.Find(t => t.ReceivingUserID == receivingUser.UserID).ReadMessageList.Add(m);
        }
        Global.Users.Find(m => m.UserID == (int)sendingUser.UserID).Conversations.Find(t => t.ReceivingUserID == receivingUser.UserID).UnReadMessageList.Clear();
    }
}

&#13;
&#13;
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Chat.aspx.cs" Inherits="Chat" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
		<a>
		<asp:ScriptManager ID="ScriptManager1" runat="server">
		</asp:ScriptManager>
			<asp:Timer ID="Timer1" runat="server">
			</asp:Timer>
		</a>
		<asp:UpdatePanel runat="server">
			<Triggers>
				<asp:AsyncPostBackTrigger ControlID="tim" EventName="Tick" />
			</Triggers>
			<ContentTemplate>
				<asp:Timer ID="tim" Interval="5000" Enabled="true" OnTick="UpdateChat" runat="server"></asp:Timer>
				<asp:Label ID="ChatLabel" runat="server"></asp:Label>
			</ContentTemplate>
		</asp:UpdatePanel>
		<asp:TextBox ID="MessageTextBox" runat="server"></asp:TextBox>
		<asp:Button ID="SendButton" runat="server" Text="Send" />
    </div>
    </form>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

如果你正在使用Scriptmanager,你可以试试这个。将代码添加到Page_Load。

 if (ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack)
        {
            // partial (asynchronous) postback occured
            // insert Ajax custom logic here
        }
        else
        {
            // regular full page postback occured
            // custom logic accordingly                
        }

https://forums.asp.net/t/1562871.aspx?Can+we+check+partial+postback+

中找到此信息