おのれ鍋奉行が!

クライアントコールバック

最終更新:

lmes2

- view
管理者のみ編集可

クライアントコールバック


概要

ASP.NETにおいて、クライアントサイドの処理⇒サーバーサイドの処理⇒クライアントサイドの処理、という本来ならば画面の再描画が必要な処理手順を、画面の再描画なしで行う方法。

これを、「クライアントコールバック」と呼ぶ。らしい。


参照


参考


元ネタ


前提条件


手順

コールバックを実装したwebユーザーコントロール(ascx)を、default.aspxに設置する。
具体的には、以下のソースをコピーして実行。

default.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml" >
  5. <head id="Head1" runat="server">
  6. <title>クライアントコールバック - C#(ASP.NET V2.0)</title>
  7. </head>
  8. <form id="frmMain" runat="server">
  9. <div>
  10. <uc1:WebUserControl ID="WebUserControl1" runat="server" />
  11. </div>
  12. </form>
  13. </body>
  14. </html>
  15.  

default.aspx
  1. using System;
  2. using System.Web.UI;
  3.  
  4. public partial class _Default : System.Web.UI.Page
  5. {
  6. void Page_Load(object sender, EventArgs e)
  7. {
  8. }
  9. }
  10.  

WebUserControl.ascx
  1. <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
  2. <script type="text/javascript">
  3. <!--
  4. /// <summary>
  5. /// コールバックを発生させるCallServer()を呼ぶ為のクライアントスクリプト
  6. /// </summary>
  7. function LookUpStock()
  8. {
  9. var lb = document.getElementById("<%= this.lbStock.ClientID %>");
  10. var product = lb.options[lb.selectedIndex].text;
  11. CallServer(product, "");
  12. }
  13.  
  14. /// <summary>
  15. /// コールバックの結果を受け取るクライアントスクリプト
  16. /// </summary>
  17. /// <param name="arg"></param>
  18. /// <param name="context"></param>
  19. function ReceiveServerData(arg, context)
  20. {
  21. Results.innerText = arg;
  22. }
  23. -->
  24. <div>
  25. <asp:ListBox ID="lbStock" runat="server"></asp:ListBox>
  26. <br />
  27. <br />
  28. <button onclick="LookUpStock();">Look Up Stock</button>
  29. <br />
  30. <br />
  31. Items in stock: <span id="Results"></span>
  32. <br />
  33. </div>
  34.  

WebUserControl.ascx.cs
  1. using System;
  2. using System.Web.UI;
  3. using System.Collections.Specialized;
  4.  
  5. public partial class WebUserControl : System.Web.UI.UserControl, System.Web.UI.ICallbackEventHandler
  6. {
  7. private string returnValue;
  8. private ListDictionary catalog;
  9.  
  10. protected void Page_Load(object sender, EventArgs e)
  11. {
  12. /// <summary>
  13. /// コールバックを発生させるクライアントスクリプト「CallServer」の定義。
  14. /// CallServerの実体は、ClientScriptManagerクラスによって動的に生成される。
  15. /// </summary>
  16. ClientScriptManager clientScriptManager = (ClientScriptManager)Page.ClientScript;
  17. string cbReference = clientScriptManager.GetCallbackEventReference(this, "arg", "ReceiveServerData", "");
  18. string callbackScript = "function CallServer(arg, context) {" + cbReference + "; }";
  19. clientScriptManager.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true);
  20.  
  21. // リストボックス
  22. this.catalog = new ListDictionary();
  23. this.catalog.Add("monitor", 12);
  24. this.catalog.Add("laptop", 10);
  25. this.catalog.Add("keyboard", 23);
  26. this.catalog.Add("mouse", 17);
  27. this.lbStock.DataSource = this.catalog;
  28. this.lbStock.DataTextField = "key";
  29. this.lbStock.DataBind();
  30. this.lbStock.SelectedIndex = 0;
  31. }
  32.  
  33. #region ICallbackEventHandler メンバ
  34.  
  35. /// <summary>
  36. /// コールバックが実行される際に呼ばれるメソッド
  37. /// </summary>
  38. /// <param name="eventArgument">クライアントサイドからの引数</param>
  39. public void RaiseCallbackEvent(string eventArgument)
  40. {
  41. if (this.catalog[eventArgument] == null)
  42. {
  43. this.returnValue = "-1";
  44. }
  45. else
  46. {
  47. this.returnValue = this.catalog[eventArgument].ToString();
  48. }
  49. }
  50.  
  51. /// <summary>
  52. /// コールバックの実行後、呼ばれるメソッド。
  53. /// 戻り値がクライアントサイドに渡される。
  54. /// </summary>
  55. /// <returns>クライアントサイドに引き渡す値</returns>
  56. public string GetCallbackResult()
  57. {
  58. return this.returnValue;
  59. }
  60.  
  61. #endregion
  62. }
  63.  

not found (5.jpg)
記事メニュー
目安箱バナー