投稿数 - 437, コメント - 56286, トラックバック - 156

System.Web.UI.DataBinder

ASP.NET の開発で重宝するのが System.Web.UI.DataBinder だが、使いづらい場合がある。

DataBinder で主に使われるのが Eval メソッドだが、このメソッドが何をしているのかというと、単にリフレクションを行っているだけだ。

C# 2.0
using System;
using System.Collections.Generic;
using System.Web.UI;

public class TestData
{
  private int _id;
  private string _name;

  public int ID
  {
    get { return _id; }
    set { _id = value; }
  }
  public string Name
  {
    get { return _name; }
    set { _name = value; }
  }
}

public class Program
{
  static void Main( string[] args )
  {
    List<TestData> list = new List<TestData>();
    TestData d = new TestData();
    d.ID = 1;
    d.Name = "A";
    list.Add( d );
    Console.WriteLine( DataBinder.Eval( list[ 0 ], "ID" ) );
    Console.WriteLine( DataBinder.Eval( list[ 0 ], "Name" ) );
  }
}

Visual Basic 8.0
Imports System
Imports System.Collections.Generic;
Imports System.Web.UI

Public Class TestData
  Private _id As Integer
  Private _name As String

  Public Property ID() As Integer
    Get
      Return _id
    End Get
    Set(ByVal value As Integer)
      _id = value
    End Set
  End Property

  Public Property Name() As String
    Get
      Return _name
    End Get
    Set(ByVal value As String)
      _name = value
    End Set
  End Property
End Class

Module Program
  Public Sub Main()
    Dim list As New List(Of TestData)
    Dim d As New TestData()
    d.ID = 1
    d.Name = "A"
    list.Add(d)
    Console.WriteLine(DataBinder.Eval(list(0), "ID"))
    Console.WriteLine(DataBinder.Eval(list(0), "Name"))
  End Sub
End Module

実行結果
1
A

実際はこんな使い方をする事は無いと思うが、DataBinder の働きを検証するには十分だろう。

ASP.NET 1.x の開発では DataGrid で DataBinder.Eval を多用すると思う。DataSource にバインドしたデータの一項目が Container.DataItem としてアクセスできるからだ。
※Container.DataItem とは何か?巧く説明している記事が @IT にある。
[ASP.NET]Container.DataItemの正体は?
※ちなみに DataBinder.Eval に関しても @IT に。
[ASP.NET]DataBinder.Evalメソッドを使用するメリット/デメリットは?

さて、DataBinder.Eval が上手く機能しないシナリオが次だ。

C# 2.0
using System;
using System.Collections.Generic;
using System.Web.UI;

public class TestData
{
  public int ID;
  public string Name;
}

public class Program
{
  static void Main( string[] args )
  {
    List<TestData> list = new List<TestData>();
    TestData d = new TestData();
    d.ID = 1;
    d.Name = "A";
    list.Add( d );
    Console.WriteLine( DataBinder.Eval( list[ 0 ], "ID" ) );
    Console.WriteLine( DataBinder.Eval( list[ 0 ], "Name" ) );
  }
}

Visual Basic 8.0
Imports System
Imports System.Collections.Generic;
Imports System.Web.UI

Public Class TestData
  Public ID As Integer
  Public Name As String
End Class

Module Program
  Public Sub Main()
    Dim list As New List(Of TestData)
    Dim d As New TestData()
    d.ID = 1
    d.Name = "A"
    list.Add(d)
    Console.WriteLine(DataBinder.Eval(list(0), "ID"))
    Console.WriteLine(DataBinder.Eval(list(0), "Name"))
  End Sub
End Module

実行結果
ハンドルされていない例外 System.Web.HttpException: DataBinding: 'TestData' には ID という名前のプロパティは含まれません。

最初のコードとの違いは、フィールドをプロパティでカプセル化しているかどうかだ。しかし、TestData のクライアントからすれば、フィールドであろうがプロパティであろうが使い方は同じである。

「実行結果の例外メッセージは的確に問題を指摘している。'ID という名前のプロパティがない'と言っているから、プロパティを定義してやればよいのだ」誰もがそう指摘するだろう。

確かにその通りで、フィールドを直接公開するのは一部の例外を除きご法度のはずである。そういう意味では DataBinder に不備などないように思う。しかし、Web サービスとの連携では DataBinder は使い物にならない。

ご存知の通り、Visual Studio には Web サービスへのプロキシクラスを自動生成する機能がある(Web 参照という機能だ)。この自動生成されたクラスで使用する DTO は全て Public フィールドを公開している。Public プロパティではない。

「Public プロパティではなく、Public フィールドを公開する」

これは単なる DTO なのだから問題はないと思う。しかし、これら DTO の配列などを DataGird 等にバインドし、DataBinder を使うと途端に破綻する。Public フィールドを DataBinder がリフレクションで取り出さないからだ。

「DataBinder を使わずにキャストすれば良いのでは?」

確かにその通りであり、その方法で十分に機能する。

例えば、

<%# DataBinder.Eval(Container.DataItem, "ID" %>
としている箇所を
<%# ((TestData)Container.DataItem).ID %>

とすれば良いのだ。暫くはこれで問題ないと思っていた。

しかし、DataBinder は甘くなかった。Sysetm.Web.UI.WebControls.ListControl から派生ししている全てのクラスは DataTextField プロパティを持っているが、コイツが内部で DataBinder.Eval を使っているのだ。DropDownList に Web サービスプロキシの DTO をデータバインドして、表示する項目を DataTextField に指定すると、DataBinder.Eval に Public フィールドを指定できない問題が顔を出す。

DataTextField という名前になっているのが、更に奇妙に感じる。ここで言う「フィールド」はクラスの「フィールド」ではなく、DataTable の「フィールド」だったのだ。様々なコントロールが DataSet と連携する事が前提になっている好例で、名前付けにも垣間見える。

ASP.NET 2.0 になって、DataBinder は変わったのか?と思ったがそんな事は全く無い。GridView は DataBinder を使っていないようだが、結局内部では DataBinder と似たような事をしていて、Public フィールドは指定できない。

リフレクションでフィールドを取得する事も出来るのにプロパティ限定にした理由は何だろうか。明らかに余計な制限だと思うのだが、どうだろうか?

投稿日時 : 2006年8月13日 17:27

フィードバック

# re: System.Web.UI.DataBinder

ASP.NET 2.0 より前の Ver の Bugs かと思ってたけど 2.0 でも public field は Eval でサポートされないのかー。ザンネン><
そもそも public field にデータが入ってる事が望ましくないから Eval 等で使えないようにして capsule 化を推奨してるのかな?
2006/08/14 23:57 | seven

# re: System.Web.UI.DataBinder

>そもそも public field にデータが入ってる事が望ましくないから Eval 等で使えないようにして capsule 化を推奨してるのかな?

確かに、DataBinder の挙動は正しいと言えば正しいね。
自動生成される Web サービスプロキシがフィールド公開でさえなければ…。
2006/08/16 20:21 | 囚人

# プロパティとフィールドの違い

プロパティとフィールドの違い
2006/12/22 23:55 | 囚人のジレンマな日々

# プロパティとフィールドの違い

プロパティとフィールドの違い
2006/12/23 0:01 | 囚人のジレンマな日々

# re: System.Web.UI.DataBinder

DataTableなどに格納されるDataRowやらDataRowViewやらにはフィールドはあるでしょうか?
という話につながっていく…
2006/12/23 2:26 | なちゃ

# re: System.Web.UI.DataBinder

「列」の値の事をフィールドと言っているんじゃないかなぁ。
2006/12/23 16:41 | 囚人

# ブライトリング時計

時計,バッグ,財布,ルイヴィトンコピー,エルメスコピー
弊店に主要な販売する商品は時計,バッグ,財布,ルイヴィトンコピー,エルメスコピー,
シャネルコピー,グッチコピー,プラダコピー,ロレックスコピー,カルティエコピー,オメガコピー,
ウブロ コピーなどの世界にプランド商品です。
2006年に弊社が設立された、
弊社は自社製品を世界中に販売して、高品質な製品と優れたアフターサービスで、
過半数の消費者からの良い評判を獲得していた。
我々自身の生産拠点と生産設備を持って、
製品の質を保証すると消費者にサポートするために、製品も工場で厳格な人工的なテストを受けました。
消費者の継続的なサポートに感謝するために、そして、企業が低コスト、高品質な製品を提供してあげます。
弊店に望ましい製品を見つけることを願って。
ここで、弊社が皆の仕事でも幸せな人生でも成功することを望んてあげます。
誠にありがとうございます。
ブライトリング時計 http://www.fujisanwatch.com
2017/08/14 17:53 | cvpfszo@softbank.jp

# 激安コピー時計

コピー時計通販専門店
人気コピー時計通販専門店

◆在庫情報随時更新!
◆お客さんたちも大好評です:
◆新品種類がそろっています。
◆品質がよい、価格が低い、実物写真!
◆経営方針: 品質を重視、納期も厳守、信用第一!
◆超格安価格で、安心、迅速、確実、にお客様の手元にお届け致します。

豊富な品揃えで最新作も随時入荷致しておりますのでごゆっくりとご覧ください。
広大な客を歓迎してご光臨!
2017/10/11 4:49 | sfudrdoujzn@softbank.jp

# ルイ.ヴィトン財布コピー品

本当にいいお買い物と安心の配送をして頂き大満足です。
こちらのリクエストに応えて頂き助かりました。
梱包も情報もありがとうございます。
楽しく安心して商品が届く、いいですね!
2017/11/14 3:51 | mntpgbhk@excite.co.jp

# 偽ブランド 通販

おすすめ人気ブランド腕時計, 最高等級時計大量入荷!
◆N品質シリアル付きも有り 付属品完備!
☆★☆━━━━━━━━━━━━━━━━━━━☆★☆
以上 宜しくお願い致します。(^0^)
広大な客を歓迎して買います!── (*^-^*)
2017/11/24 17:08 | mzeuqcy@ezwen.ne.jp

# ブランド通販店

弊社は各ランクのブランド商品満載し、ブランド通販店で一番信用のある店なので!。
品質はこちらが間違いなく保証します。
https://www.ginzaoff.com

■取扱ブランド ロレックス時計コピー、カルティエ時計コピー、IWC時計コピー、
ブライトリング時計コピー、パネライ時計コピー.
◆ スタイルが多い、品質がよい、価格が低い、実物写真!
◆ ご入金頂いてから最速4日、遅くとも7日程度でご指定場所へ発送出来る予定でございます
◆ 商品送料を無料にいたします

◆信用第一、良い品質、低価格は 私達の勝ち残りの切り札です。
◆ 当社の商品は絶対の自信が御座います。
◇ N品質 シリアル付きも有り 付属品完備!

◆ 必ずご満足頂ける品質の商品のみ販売しております。
◇ 品質を最大限本物と同等とする為に相応の材質にて製作している為です。
◆ 絶対に満足して頂ける品のみ皆様にお届け致します。

興味あれば、是非一度サイトをご覧になって下さい。
今後ともよろしくご愛顧くださいますよう、お願い申し上げます
https://www.ginzaoff.com
お取り引きを開始させていただきたく思います。
詳細に関してはどうぞお気軽にご連絡ください。
2019/09/16 23:41 | Georgefipse

# Superb knowledge, With thanks! custom research paper writing services essay

Superb knowledge, With thanks!
custom research paper writing services
essay

# Superb knowledge, With thanks! custom research paper writing services essay

Superb knowledge, With thanks!
custom research paper writing services
essay

# Superb knowledge, With thanks! custom research paper writing services essay

Superb knowledge, With thanks!
custom research paper writing services
essay

# Superb knowledge, With thanks! custom research paper writing services essay

Superb knowledge, With thanks!
custom research paper writing services
essay

# With thanks. Fantastic stuff! writing service thesis help

With thanks. Fantastic stuff!
writing service
thesis help

# With thanks. Fantastic stuff! writing service thesis help

With thanks. Fantastic stuff!
writing service
thesis help

# With thanks. Fantastic stuff! writing service thesis help

With thanks. Fantastic stuff!
writing service
thesis help

# With thanks. Fantastic stuff! writing service thesis help

With thanks. Fantastic stuff!
writing service
thesis help

# Nicely put, Appreciate it! essay custom research paper writing services

Nicely put, Appreciate it!
essay
custom research paper writing services

# You revealed it really well! paper writing services paper writing services

You revealed it really well!
paper writing services
paper writing services

# Nicely put. Thanks. paper writing service thesis help

Nicely put. Thanks.
paper writing service
thesis help

# Nicely put. Thanks. paper writing service thesis help

Nicely put. Thanks.
paper writing service
thesis help

# Nicely put. Thanks. paper writing service thesis help

Nicely put. Thanks.
paper writing service
thesis help

# Appreciate it! Plenty of forum posts! custom research paper writing services paper writing service

Appreciate it! Plenty of forum posts!

custom research paper writing services
paper writing service

# Very good tips, Many thanks! thesis statement help thesis statement help

Very good tips, Many thanks!
thesis statement help
thesis statement help

# Amazing facts Thanks. paper writing service thesis writing service

Amazing facts Thanks.
paper writing service
thesis writing service

# Amazing a lot of wonderful tips! writing service thesis statement help

Amazing a lot of wonderful tips!
writing service
thesis statement help

# Nicely put. Many thanks. Demetra https://americandental.ru/, Quentin,

Nicely put. Many thanks.
Demetra
https://americandental.ru/, Quentin,

# With thanks, Numerous forum posts! thesis writing service edu essay

With thanks, Numerous forum posts!

thesis writing service
edu essay

# Pretty component to content. I just stumbled upon your web site and in accession capital to claim that I get in fact lopved account your weblog posts. Anyway I'll be subscribing on your feeds or even I achievement you get entry tto persistently quickly.

Pretty component to content. I just stumbled upon your web site and in accession capital to claim that I
get iin fact loved acccount your weblog posts.
Anyway I'll be sjbscribing on your feeds or even I achievement you get entry to persistently quickly.


https://streetchallenge.ru/
Freddie (Freddie)

# ロレックス バネ棒 値段

財布を購入、注文した翌日に届いてびっくり。丁寧に梱包されていて、ショップからのお礼のメモもあり。品物は展示品でキズや汚れありって書かれてましたが、どこに??って感じで新品同様でした!ショップの評価が良かったので決めたのですが、間違いなかったです!!
ロレックス バネ棒 値段 https://www.bag37.com/product-27493.html?number_of_uploads=0&action=add_product
2022/01/19 2:12 | aoaqtx@softbank.ne.jp

# buy cheap doxycycline online https://doxycyline1st.com/
buy doxycycline for dogs

buy cheap doxycycline online https://doxycyline1st.com/
buy doxycycline for dogs
2022/02/26 0:46 | Jusidkid

# online doxycycline https://doxycyline1st.com/
buy doxycycline for dogs

online doxycycline https://doxycyline1st.com/
buy doxycycline for dogs
2022/02/26 9:49 | Jusidkid

# buy cheap clomid https://clomiden.fun/

buy cheap clomid https://clomiden.fun/
2022/04/12 13:04 | Clomids

# prednisone 54 https://prednisoneus.shop/

prednisone 54 https://prednisoneus.shop/
2022/04/16 23:00 | Prednisone

# Keflex https://allpharm.store/

Keflex https://allpharm.store/
2022/07/21 22:12 | AllPharm

# cvs prescription prices without insurance: https://medrxfast.com/

cvs prescription prices without insurance: https://medrxfast.com/
2022/08/03 19:15 | MedsRxFast

# metformin 227 https://glucophage.top/
where to buy metformin 500 mg

metformin 227 https://glucophage.top/
where to buy metformin 500 mg
2022/08/23 8:00 | Niujsdkj

# best medication for ed https://ed-pills.xyz/
top erection pills

best medication for ed https://ed-pills.xyz/
top erection pills
2022/09/17 7:56 | EdPills

# order doxycycline online https://buydoxycycline.icu/

order doxycycline online https://buydoxycycline.icu/
2022/10/08 11:59 | Doxycycline

#  https://clomidforsale.site/

https://clomidforsale.site/
2022/11/13 14:29 | ForSale

# ed pills gnc https://cheapestedpills.com/
ed meds

ed pills gnc https://cheapestedpills.com/
ed meds
2022/12/10 16:34 | CheapPills

# Generic Name. Actual trends of drug.
https://edonlinefast.com
Everything information about medication. Read information now.

Generic Name. Actual trends of drug.
https://edonlinefast.com
Everything information about medication. Read information now.
2023/02/18 15:46 | EdOnline

# online doxycycline - https://doxycyclinesale.pro/#

online doxycycline - https://doxycyclinesale.pro/#
2023/04/21 17:41 | Doxycycline

# 1 mg prednisone daily - https://prednisonesale.pro/#

1 mg prednisone daily - https://prednisonesale.pro/#
2023/04/22 4:58 | Prednisone

# cytotec buy online usa - https://cytotecsale.pro/#

cytotec buy online usa - https://cytotecsale.pro/#
2023/04/29 5:10 | Cytotec

# canadian pharmaceuticals online https://pillswithoutprescription.pro/#

canadian pharmaceuticals online https://pillswithoutprescription.pro/#
2023/05/15 4:04 | PillsPresc

# erection pills: https://edpills.pro/#

erection pills: https://edpills.pro/#
2023/05/15 15:44 | EdPillsPro

# medication without prior prescription https://pillswithoutprescription.pro/#

medication without prior prescription https://pillswithoutprescription.pro/#
2023/05/16 10:04 | PillsPro

# prednisone over the counter uk https://prednisonepills.pro/# - prednisone 1mg purchase

prednisone over the counter uk https://prednisonepills.pro/# - prednisone 1mg purchase
2023/06/04 21:36 | Prednisone

# men's ed pills https://edpill.pro/# - best pill for ed

men's ed pills https://edpill.pro/# - best pill for ed
2023/06/27 14:56 | EdPills

# ï»¿paxlovid https://paxlovid.life/# paxlovid generic

paxlovid https://paxlovid.life/# paxlovid generic
2023/07/25 20:48 | Paxlovid

# ルイ ヴィトン モノグラム ヴェルニ 307

大変わかりやすい説明、至らぬ私でお手間とらせてしまぃ、申し訳ございませんでした。
ありがとうございました。
ルイヴィトン コインケース ヴェルニ ジッピー コインパース M93053 オレンジサンセット 新品 コンパクト 財布 ラウンドファスナー小銭入れ カードケース レザー エナメル 革 ルイ・ヴィトン
色々あったのに、ご親切に説明して頂け、助かりました。
商品もめっちゃ気に入りました。
お世話になりました。
ありがとうございました。
ルイ ヴィトン モノグラム ヴェルニ 307 https://www.copysale.net/Copy-sale-6132-PRADA-_25E3_2583_2597_25E3_2583_25A9_25E3_2583_2580-_25E3_2583_2597_25E3_2583_25A9_250C6D0B6CA2.htm
2023/09/15 17:15 | ahkmsf@yahoo.co.jp

# paxlovid pill https://paxlovid.bid/ paxlovid covid

paxlovid pill https://paxlovid.bid/ paxlovid covid
2023/10/25 23:23 | Paxlovid

# ルイ ヴィトン 財布 エピ pm

2023年人気の商品を超えて、当店が売買する中.
すべての商品はすべて新しく着きます.
すべての商品の品質がすべて検査するのが合格です.
みんなを歓迎して選り取りで買います.
■主要取扱商品 バッグ、財布、腕時計、マフラー!
○ スタイルが多い、品質がよい、価格が低い!
○ S品質 シリアル付きも有り 付属品完備!
○必ずご満足頂ける品質の商品のみ販売しております.

品質の承諾:
◆入金の後で3-7日高い府に到着する
◆もし、税関に止まられた場合、再びに無料で再送いたします。到着してまで。
当社の商品は絶対の自信が御座います。
S品質 シリアル付きも有り 付属品完備!
ルイ ヴィトン 財布 エピ pm https://www.b2tokei.com/product/detail.aspx-id=4370.htm
2023/11/10 8:17 | cxrqaqdzj@hotmail.co.jp

# generic for doxycycline https://doxycycline.forum/ doxycycline 100mg

generic for doxycycline https://doxycycline.forum/ doxycycline 100mg
2023/11/25 13:35 | Doxycycline

# natural remedies for ed https://edpills.tech/# ed pills gnc

natural remedies for ed https://edpills.tech/# ed pills gnc
2023/12/23 8:31 | EdPills

コメントの投稿

タイトル
名前
URL
コメント