2017-02-24 8 views
0

私はウェブ開発には初めて.netを使用しており、ビジネスロジックからテーブルへのデータバインディングに問題があります。私は基本的にテーブルを動的に生成しようとしています。asp.netでのデータバインディング:listview

表Iは

<asp:ListView ID="processList" runat="server" 
      DataKeyNames="procName" GroupItemCount="1" 
      ItemType="SerMon.RemoteProcess" SelectMethod="fetchFromQueue"> 
      <EmptyDataTemplate> 
       <table > 
        <tr> 
         <td>No data was returned.</td> 
        </tr> 
       </table> 
      </EmptyDataTemplate> 
      <EmptyItemTemplate> 
       <td/> 
      </EmptyItemTemplate> 
      <GroupTemplate> 
       <tr id="itemPlaceholderContainer" runat="server"> 
        <td id="itemPlaceholder" runat="server"></td> 
       </tr> 
      </GroupTemplate> 
      <ItemTemplate> 
       <td runat="server"> 
        <table id="myTable" class="table table-striped table-hover "> 
         <thead> 
          <tr> 
           <th>#</th> 
           <th>Process</th> 
           <th>Status</th> 
           <th>Machine</th> 
          </tr> 
         </thead> 
         <tbody> 
          <tr> 
           <td>1</td> 
           <td> 
            <asp:Label runat="server" ID="lblId"><%#: Item.ProcName%></asp:Label></td> 
           <td> 
            <asp:Label runat="server" ID="Label1"><%#: Item.Procstatus%></asp:Label></td> 
           <td> 
            <asp:Label runat="server" ID="Label2"><%#: Item.mcName%></asp:Label></td> 
          </tr> 
          <tr> 
         </tbody> 
        </table> 

        </p> 
       </td> 
      </ItemTemplate> 

     </asp:ListView> 

を移入するリストビューで

public List<RemoteProcess> fetchFromQueue() 
    { 
     List<RemoteProcess> pl = new List<RemoteProcess>(); 
     foreach (CloudQueueMessage message in queue.GetMessages(5, TimeSpan.FromMinutes(1))) 
     { 
      Debug.Write(message.AsString); 
      RemoteProcess m = JsonConvert.DeserializeObject<RemoteProcess>(message.AsString); 
      pl.Add(m); 
      //queue.DeleteMessage(message); 
     } 
     return pl; 
    } 

表を表に移入するために呼び出されるメソッドは、生成されたが、何のデータをtheresのはありません。また、何らかの理由で5つのテーブルが生成されます(これは常にgetMessage関数で指定された数値と同じです)。

+1

あなたの場合、 'ListView'コントロールの代わりに' GridView'コントロールを使う方が良いと思います。 – VDWWD

+0

5つのテーブルを避けるために 'GroupItemCount =" 1 "'を削除してみてください。 –

+0

@ChetanRanpariyaいいえ、それを試してみました。うまくいきません – NeuralLynx

答えて

0

ListViewでGroupTemplateが必要かどうかわかりません。あなたが複数のテーブルを取得していた理由は、テーブルのヘッダと行を両方ともアイテムテンプレートで定義したためです。 ItemTemplateは項目表示の構造のみを持つ必要があります。 LayoutTemplateは、データがどのように見えるかについての全体的なレイアウトの構造を持つ必要があります。

私はそれが適切な表のように見えるようにListViewを変更しました。

<asp:ListView ID="processList" runat="server" 
    DataKeyNames="procName" 
    ItemType="WebApplication1.Models.RemoteProcess" SelectMethod="fetchFromQueue"> 
    <EmptyDataTemplate> 
     <table> 
      <tr> 
       <td>No data was returned.</td> 
      </tr> 
     </table> 
    </EmptyDataTemplate> 
    <EmptyItemTemplate> 
     <td /> 
    </EmptyItemTemplate> 
    <LayoutTemplate> 
     <table runat="server" id="table1"> 
      <thead> 
       <tr runat="server"> 
        <th>#</th> 
        <th>Process</th> 
        <th>Status</th> 
        <th>Machine</th> 
       </tr> 
       <tr id="itemPlaceholder" runat="server"></tr> 
      </thead> 
     </table> 
    </LayoutTemplate> 
    <ItemTemplate> 
     <tr runat="server"> 
      <td>1</td> 
      <td> 
       <asp:Label runat="server" ID="lblId"><%#: Item.procName%></asp:Label></td> 
      <td> 
       <asp:Label runat="server" ID="Label1"><%#: Item.Procstatus%></asp:Label></td> 
      <td> 
       <asp:Label runat="server" ID="Label2"><%#: Item.mcName%></asp:Label></td> 
     </tr> 
    </ItemTemplate> 
</asp:ListView> 

私はGroupTemplateを削除し、私はどのようにグループのデータをに分からなかったとして、それに関連する設定をしていることをここで注意してください。

これは、問題を解決するのに役立ちます。

+0

それはうまくいった!複数のテーブルを取得できませんが、データはまだ入力されていません。 procNameとMachine列は空ですが、ステータス列は常に0です。渡されるオブジェクトをダブルチェックし、必要なデータが含まれているだけで表示されません。 – NeuralLynx

+0

ソースからデータを取得しているかどうかを確認しますか? 'return pl'にブレークポイントを置き、コードをデバッグしてください。 'pl'に値が入力されているのを見てください。 –