<p> UpdatePanel - это контрол Microsoft ASP.NET AJAX, который позволяет довольно удобно интегрировать AJAX в ваше приложение. Но именно ввиду его удобства и простоты использования со стороны разработчика, некоторые вещи далеко не очевидны. К примеру как вызвать клиентский JavaScript по завершению очередного обновления этой панели? </p> <p> Трюк заключается в том, чтобы добавить EndRequestHandler в request manager: </p> <font size="2"> <p> Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler); </p> </font> <p> Вот пример формы с двумя TextBox, куда вы вводите текст, а сервер возвращает длину текста и отображает ее во втором TextBox: </p> <p> <span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"><%@ page language="C#" autoeventwireup="true" codebehind="DemoJScriptUpdate.aspx.cs" inherits="CharterWeb.DemoJScriptUpdate" %></span><br /> <font face="Courier New">  <br />  <span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"><</span><span style="font-weight: normal; font-size: 11px; color: maroon; font-family: Courier New; background-color: transparent">script</span> <span style="font-weight: normal; font-size: 11px; color: red; font-family: Courier New; background-color: transparent">runat</span></font><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">="server"</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">></span><br /> <font face="Courier New"> protected void txtDataOnChange(object sender, EventArgs e) { <br />  txtLength.Text = txtData.Text.Length.ToString();<br />  }<br />  <span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"></</span><span style="font-weight: normal; font-size: 11px; color: maroon; font-family: Courier New; background-color: transparent">script</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">></span> <br />  <span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"><</span><span style="font-weight: normal; font-size: 11px; color: maroon; font-family: Courier New; background-color: transparent">html</span> <span style="font-weight: normal; font-size: 11px; color: red; font-family: Courier New; background-color: transparent">xmlns</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">="<a href="</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"></span>http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml<span style="font-weight: normal; font-size: 11px; color: blue; font-family: courier new; background-color: transparent"></</span><span style="font-weight: normal; font-size: 11px; color: maroon; font-family: courier new; background-color: transparent">a</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: courier new; background-color: transparent">></span>"><br />  <span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"><</span><span style="font-weight: normal; font-size: 11px; color: maroon; font-family: Courier New; background-color: transparent">head</span> <span style="font-weight: normal; font-size: 11px; color: red; font-family: Courier New; background-color: transparent">id</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">="Head1"</span> <span style="font-weight: normal; font-size: 11px; color: red; font-family: Courier New; background-color: transparent">runat</span></font><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">="server"</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">></span><br /> <font face="Courier New">  <br />  <br />  <span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"><</span><span style="font-weight: normal; font-size: 11px; color: maroon; font-family: Courier New; background-color: transparent">script</span> <span style="font-weight: normal; font-size: 11px; color: red; font-family: Courier New; background-color: transparent">type</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">="text/javascript"</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">></span> <br />  function EndRequestHandler(sender, args) {<br />  if (args.get_error() == undefined) <br />  alert("Your text has: " + document.getElementById("txtLength").value + " character(s)");<br />  else<br />  alert("There was an error" + args.get_error().message); <br />  }<br />  function load() { <br />  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);<br />  }<br />  <span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"></</span><span style="font-weight: normal; font-size: 11px; color: maroon; font-family: Courier New; background-color: transparent">script</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">></span> <br />  <br />  <span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"><</span><span style="font-weight: normal; font-size: 11px; color: maroon; font-family: Courier New; background-color: transparent">form</span> <span style="font-weight: normal; font-size: 11px; color: red; font-family: Courier New; background-color: transparent">id</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">="form1"</span> <span style="font-weight: normal; font-size: 11px; color: red; font-family: Courier New; background-color: transparent">runat</span></font><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">="server"</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">></span><br /> <font face="Courier New"> <font size="2"><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"><?</span><span style="font-weight: normal; font-size: 11px; color: maroon; font-family: Courier New; background-color: transparent">xml:namespace</span> <span style="font-weight: normal; font-size: 11px; color: red; font-family: Courier New; background-color: transparent">prefix</span> <span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">=""</span></font><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"></span> asp /><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"></</span><span style="font-weight: normal; font-size: 11px; color: maroon; font-family: Courier New; background-color: transparent">asp:scriptmanager</span> <span style="font-weight: normal; font-size: 11px; color: red; font-family: Courier New; background-color: transparent">id</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">="_scriptManager"</span> <span style="font-weight: normal; font-size: 11px; color: red; font-family: Courier New; background-color: transparent">runat</span></font><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">="server"</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">></span><br /> <font face="Courier New"> <br />  <span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"><</span><span style="font-weight: normal; font-size: 11px; color: maroon; font-family: Courier New; background-color: transparent">asp:updatepanel</span> <span style="font-weight: normal; font-size: 11px; color: red; font-family: Courier New; background-color: transparent">id</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">="UpdatePanel1"</span> <span style="font-weight: normal; font-size: 11px; color: red; font-family: Courier New; background-color: transparent">runat</span></font><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">="server"</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">></span><br /> <font face="Courier New"> <span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"><</span></font><span style="font-weight: normal; font-size: 11px; color: maroon; font-family: Courier New; background-color: transparent">contenttemplate</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">></span><br /> <font face="Courier New"> Write something: <span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"></</span><span style="font-weight: normal; font-size: 11px; color: maroon; font-family: Courier New; background-color: transparent">asp:textbox</span> <span style="font-weight: normal; font-size: 11px; color: red; font-family: Courier New; background-color: transparent">id</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">="txtData"</span> <span style="font-weight: normal; font-size: 11px; color: red; font-family: Courier New; background-color: transparent">runat</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">="server"</span> <span style="font-weight: normal; font-size: 11px; color: red; font-family: Courier New; background-color: transparent">autopostback</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">="true"</span> <span style="font-weight: normal; font-size: 11px; color: red; font-family: Courier New; background-color: transparent">ontextchanged</span></font><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">="txtDataOnChange"</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">></span><br /> <font face="Courier New"> <font size="2"><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"><</span><span style="font-weight: normal; font-size: 11px; color: maroon; font-family: Courier New; background-color: transparent">br</span> <span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">/></span></font></font><br /> <font face="Courier New"> Server says the length is: <span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"></</span><span style="font-weight: normal; font-size: 11px; color: maroon; font-family: Courier New; background-color: transparent">asp:textbox</span> <span style="font-weight: normal; font-size: 11px; color: red; font-family: Courier New; background-color: transparent">id</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">="txtLength"</span> <span style="font-weight: normal; font-size: 11px; color: red; font-family: Courier New; background-color: transparent">runat</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">="server"</span> <span style="font-weight: normal; font-size: 11px; color: red; font-family: Courier New; background-color: transparent">autopostback</span></font><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">="true"</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">></span><br /> <font face="Courier New"> <span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"></</span></font><span style="font-weight: normal; font-size: 11px; color: maroon; font-family: Courier New; background-color: transparent">contenttemplate</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">></span><br /> <font face="Courier New"> <span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"></</span></font><span style="font-weight: normal; font-size: 11px; color: maroon; font-family: Courier New; background-color: transparent">asp:updatepanel</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">></span><br /> <font face="Courier New"> <br />  <span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent"></</span><span style="font-weight: normal; font-size: 11px; color: maroon; font-family: Courier New; background-color: transparent">form</span><span style="font-weight: normal; font-size: 11px; color: blue; font-family: Courier New; background-color: transparent">></span>  <br /> </font> </p>

UpdatePanel - это контрол Microsoft ASP.NET AJAX, который позволяет довольно удобно интегрировать AJAX в ваше приложение. Но именно ввиду его удобства и простоты использования со стороны разработчика, некоторые вещи далеко не очевидны. К примеру как вызвать клиентский JavaScript по завершению очередного обновления этой панели?

Трюк заключается в том, чтобы добавить EndRequestHandler в request manager:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

Вот пример формы с двумя TextBox, куда вы вводите текст, а сервер возвращает длину текста и отображает ее во втором TextBox:

<%@ page language="C#" autoeventwireup="true" codebehind="DemoJScriptUpdate.aspx.cs" inherits="CharterWeb.DemoJScriptUpdate" %>
 
 <script runat
="server">
 protected void txtDataOnChange(object sender, EventArgs e) {
 txtLength.Text = txtData.Text.Length.ToString();
 }
 </script>
 <html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>">
 <head id="Head1" runat
="server">
 
 
 <script type="text/javascript">
 function EndRequestHandler(sender, args) {
 if (args.get_error() == undefined)
 alert("Your text has: " + document.getElementById("txtLength").value + " character(s)");
 else
 alert("There was an error" + args.get_error().message);
 }
 function load() {
 Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
 }
 </script>
 
 <form id="form1" runat
="server">
 <?xml:namespace prefix ="" asp /></asp:scriptmanager id="_scriptManager" runat="server">
 
 <asp:updatepanel id="UpdatePanel1" runat
="server">
 <contenttemplate>
 Write something: </asp:textbox id="txtData" runat="server" autopostback="true" ontextchanged="txtDataOnChange">
 <br />
 Server says the length is: </asp:textbox id="txtLength" runat="server" autopostback="true">
 </contenttemplate>
 </asp:updatepanel>
 
 </form> 

blog comments powered by Disqus