Skip to content

Instantly share code, notes, and snippets.

@seraphy
Created November 16, 2011 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seraphy/1369603 to your computer and use it in GitHub Desktop.
Save seraphy/1369603 to your computer and use it in GitHub Desktop.
ASP.NET4 AjaxでJavaScriptとページメソッド間の通信方法のメモ
public partial class AspajaxPageMethodsTest: System.Web.UI.Page
{
/*
* Web.configで、ASP.NET AjaxでWebMethodsを有効にする.
* <configuration>
* <system.webServer>
* <modules runAllManagedModulesForAllRequests="true">
* <!-- <add name="MyModule1" type="aspextjs.MyModule1"/> -->
* <add name="ScriptModule" type="System.Web.Handlers.ScriptModule"/>
* </modules>
* </system.webServer>
* </configuration>
*
* ASPXページのスクリプトマネージャで、PageMethodsを有効とする.
* <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
*
* js側では,スクリプトマネージャで生成されたPageMethods上のメソッドを使って
* アクセスする.
*
* PageMethods.GetComboInfo(
* // 引数リスト
* dep, area, block, fieldName,
* // 成功した場合のハンドラ
* function (results, context, methodName) {
* //alert("method=" + methodName + "/results=" + results);
* // JavaScriptSerializerを使うことで eval前に エスケープされた日付型を
* // JavaScriptの日付に変換できるようにしてからevalしてくれる.
* var deserialized = Sys.Serialization.JavaScriptSerializer.deserialize(results);
* updateFields(deserialized);
* },
* // 通信エラー時のハンドラ
* function onfailure(result) {
* alert(result.get_message());
* }
* );
*
* [参考文献]
* http://msdn.microsoft.com/ja-jp/magazine/cc163499.aspx
* http://msdn.microsoft.com/ja-jp/library/bb515101.aspx
* http://www.atmarkit.co.jp/fdotnet/aspnetajax/aspnetajax02/aspnetajax02_04.html
*/
[WebMethod]
public static string GetComboInfo(
string dep, string area, string block, string changed)
{
// JavaScriptからの引数を取得
System.Diagnostics.Debug.Print(
String.Format("dep:{0}, area:{1}, block:{2}, changed:{3}",
dep, area, block, changed
));
int iDep = 0, iArea = 0, iBlock = 0;
int.TryParse(dep, out iDep);
int.TryParse(area, out iArea);
int.TryParse(block, out iBlock);
// 返却用オブジェクトの構築
var deps = new List<object>();
var areas = new List<object>();
var blocks = new List<object>();
var persons = new List<object>();
var ps = new[] {
new {List = deps, Max = 10, Filter = -1, FilterBase = 1,
pFmt = "第{0}部"},
new {List = areas, Max = 100, Filter = iDep, FilterBase = 10,
pFmt = "第{0}課"},
new {List = blocks, Max = 1000, Filter = iArea, FilterBase = 100,
pFmt = "第{0}係"},
new {List = persons, Max = 3000, Filter = iBlock, FilterBase = 1000,
pFmt = "{0}郎"},
};
foreach (var p in ps)
{
for (int idx = 0; idx < p.Max; idx++)
{
if (p.Filter < 0 || idx % p.FilterBase == p.Filter)
{
p.List.Add(new
{
id = idx,
name = string.Format(p.pFmt, idx),
dt = DateTime.Now // 日付型は、そのまま設定可能.
});
}
}
}
var results = new {
dep = deps,
area = areas,
block = blocks,
person = persons
};
// JSONに変換して文字列を返す.
// 数値、文字列、日付などの標準オブジェクトは、特にカスタムする必要なく
// シリアライズ可能. 日付は前後にエスケープ文字を含む特殊な文字列になるため
// 単純にevalするのではなく、ASP.NET Ajaxの用意するクラスでデシリアライズする.
// (evalしても文法エラーなどにはならないので日付がなければevalでも可)
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var buf = new StringBuilder();
serializer.Serialize(results, buf);
System.Diagnostics.Debug.Print("JSON=" + buf.ToString());
return buf.ToString();
}
protected void Page_Load(object sender, EventArgs e)
{
// HttpContext context = HttpContext.Current;
// context.Items["PageID"] = "c2";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment