<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ChatUserControl.ascx.cs"
Inherits="MetamorphismApp.ChatUserControl" %>
<asp:Timer ID="timer1" runat="server" OnTick="timer1_Tick" Interval="5000">
</asp:Timer>
<div id="divChatWindow" class="clChatWindow">
<div>
<asp:Label runat="server" Text='<%# Eval("username") %>' class="divHeader" ID="lblChatFriend"></asp:Label>
<asp:Image ID="imgFriend" runat="server" CssClass="classFriendImage"/>
<asp:LinkButton ID="lbClose" runat="server" CommandName="Close" CssClass="lbClose"
OnClick="lbClose_Click">Close</asp:LinkButton></div>
<div class="chatText" id="idChatText" runat="server">
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timer1" EventName="Tick" />
<asp:AsyncPostBackTrigger ControlID="btnSendChat" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Repeater runat="server" ID="rpChatMessages">
<ItemTemplate>
<asp:Image ID="imageForFriend" runat="server" CssClass="clFriendsImage" ImageUrl='<%# "HttpImageHandler.jpg?username=" + DataBinder.Eval(Container.DataItem,"fromusername").ToString() %>' />
<asp:Label ID="chatMessage" runat="server" Text='<%# Eval("Message") %>'></asp:Label>
<br>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<asp:TextBox ID="txtChatMessage" runat="server" Width="142px" CssClass="clChatMessage"
TextMode="MultiLine"></asp:TextBox>
<asp:LinkButton ID="btnSendChat" runat="server" CommandName="Insert" CommandArgument='<%# Eval("username") %>'
OnClick="btnSendChat_Click">Send</asp:LinkButton>
</div>
protected void btnSendChat_Click(object sender, EventArgs e)
{
TextBox txt = this.FindControl("txtChatMessage") as TextBox;
string username = lblChatFriend.Text;
ucc.InsertMessage(Session["username"].ToString(), username, txt.Text);
string javaScript = "<script type='text/javascript'>\n" + "CallScroller();\n" + "</script>";
ScriptManager.RegisterStartupScript(this, typeof(ChatUserControl), "startUpScript", javaScript, false);
}
btnSendChat function is in a user control code-behind file. CallScroller function doesn't get called.
That's because
RegisterStartupScriptis called when your DOM is loaded, the equivalent with jQuery to:Since you are capturing the events from your button inside the
UpdatePanelusing triggers, you are effectively partial rendering your view that's why the script registered withRegisterStartupScriptwill never run in this situationAlternatives:
React to the Sys.load event
This event is raised every time a page is partial rendered.
Handle the event as follows:
Add a
HiddenFieldinside yourUpdatePanel.ContentTemplateto use it as a flag to indicate when to call the JavaScript function, without it, your function would get called on every postFinally in the code behind turn on the flag in the event that best fits your needs:
That's it