2011-12-28 6 views
3

私はデータを動的に表示する方法を見つけようとしていますが、運が生まれていません。 私はアラートシステム(SQL Server DBテーブルで構成されています)を持っていて、そのテーブル内の各(関連する)行に対して、ASPページに表示する文字列を作成し、チェックボックス私は警告を「読んだ」とマークすることができます)。ASP.NETアプリケーションでのデータの動的表示

私は既にデータを持っており、私はそれを正しく行っていると思いますが、このデータを私が望むように完全にページに表示することはできませんでした。ここで

は、C#のコードです:

protected void getAlertas(string matricula) 
{ 
    string query = "SELECT * FROM ALERTAS A "; 
    query += "WHERE EXISTS ("; 
    query += "SELECT ID, MATRICULA FROM VEICULOS V WHERE V.MATRICULA = @matricula "; 
    query += "AND A.IDVEICULO = V.ID "; 
    query += "AND LIDA = 0)"; 

    SqlCommand comm = new SqlCommand(query, Utils.connectDB()); 
    matricula = matricula.ToString().Replace("\"", "'"); 
    comm.Parameters.AddWithValue("@matricula", matricula); 

    StringBuilder builder = new StringBuilder(); 
    DataTable dt = new DataTable(); 
    SqlDataAdapter da = new SqlDataAdapter(); 
    StringCollection alertValues = new StringCollection(); 

    StringBuilder HTML = new StringBuilder(); 

    try 
    { 
     comm.Connection.Open(); 
     SqlDataReader reader = comm.ExecuteReader(); 
     dt.Load(reader); 

     foreach (DataRow row in dt.Rows) 
     { 
      Label label = new Label(); 
      CheckBox cBox = new CheckBox(); 

      foreach (DataColumn column in dt.Columns) 
      { 
       alertValues.Add(row[column].ToString()); 
      } 

      builder.Append(alertValues[0]); //ID do alerta 
      builder.Append(":"); 

      builder.Append(matricula); 

      if (int.Parse(alertValues[10]) == 0) //se motor desligado 
      { 
       //string output = "Motor desligado às " + alertValues[3] + "do dia " + alertValues[4]; 
       builder.Append("Motor desligado às "); 

      } 
      else if(int.Parse(alertValues[10]) == 1) //se motor ligado 
      { 
       //string output = "Motor ligado às " + alertValues[3] + "do dia " + alertValues[4]; 
       builder.Append("Motor ligado às "); 
      } 

      builder.Append(alertValues[3]); //hora 
      builder.Append("do dia "); 
      builder.Append(alertValues[4]); //data 

      //HTML.Append(" <div id=\"notifications\" runat=\"server\" class=\"notifications\">"); 
      //HTML.Append(" <div class=\"notificationText\">"); 
      //HTML.Append("<asp:Label ID=\"Label1\" runat=\"server\" Text=\"" + builder.ToString() + "\"></asp:Label>"); 
      //HTML.Append("</div>"); 
      //HTML.Append("<div class=\"setRead\">"); 
      //HTML.Append("<asp:CheckBox ID=\"" + alertValues[0] + "\" runat=\"server\" />"); 
      //HTML.Append("</div>"); 
      //HTML.Append("</div>"); 


      label.Text = builder.ToString(); 
      cBox.ID = alertValues[0]; 
     } 
    } 
    finally 
    { 
     comm.Connection.Close(); 
    } 
} 

そして、ここでは、私がデータを提示したいASPページです:

<div id="contentRight"> 
     <div id="head"> 
      <p> 
       <b>Notificações</b></p> 
     </div> 
     <div id="notifications" runat="server" class="notifications"> 
      <div class="notificationText"> 
       <asp:Label ID="Label1" runat="server" Text="Nada para mostrar"></asp:Label> 
      </div> 
      <div class="setRead"> 
       <asp:CheckBox ID="AlertIDRead" runat="server" /> 
      </div> 
     </div> 
     <hr /> 
     <div id="OkBtn" align="center"> 
      <asp:Button ID="OkButton" class="Button" runat="server" Text="Ok" /> 
     </div> 
    </div> 

目的はに構築された文字列を提示することができることですページ、およびこれらの各行についても、読み取り専用としてマークすることを簡単にするために、(DB上の)alertIDと同じIDを持つCheckBoxを提供します。

私は本当にここで失われ、いくつかの助けを使うことができます。

+1

あなたはcontolsを作成しましたが、ページに追加されていません。代わりに、ラベルとチェックボックスをカプセル化するUserControlを使用する必要があります。次に、[LoadControl](http://msdn.microsoft.com/en-us/library/c0az2h86.aspx)を使用して、一致するアラートごとにこれらのUserControlを読み込み、それらをPanelのようなコンテナに追加します(何がdiv)。注意:以前と同じIDを持つすべてのポストバックで動的に作成されたコントロールを再作成して、イベントがトリガーされるか、ViewStateデータが正しく読み込まれるようにする必要があります。遅くともPage_Loadで起こるはずです。 –

+0

使用しているASP.NETのバージョンは何ですか? 3.5? 4.0? –

+0

こんにちはマット、私は3.5を使用しています。 –

答えて

1

項目を動的に追加できます。しかし、これは少し痛みです。ページが初めて読み込まれるときだけでなく、毎回それらを追加する必要があります。また、ポストバックの間にビューステートを保持し、チェックされたボックスを覚えておきたい場合は、ページライフサイクルの早い時期(たとえばPage_Initイベント)に追加する必要があります。

より簡単なアプローチは、ListViewなどのコントロールを使用することです。 ASP.NET 4.0では、コントロール名を必要に応じて簡単に取得できますが、ASP.NET 3.5で自動生成されたコントロール名では問題ありません。 ASPXページの例を次に示します。

<asp:ListView runat="server" ID="listView" DataKeyNames="AlertId"> 
    <LayoutTemplate> 
     <div id="contentRight"> 
      <div id="head"> 
       <p> 
        <b>Notificações</b></p> 
      </div> 
      <div id="notifications" runat="server" class="notifications"> 
       <asp:PlaceHolder ID="itemPlaceholder" runat="server" /> 
      </div> 
     </div>  
    </LayoutTemplate> 
    <ItemTemplate> 
     <div class="notificationText"> 
      <asp:Label ID="Label1" runat="server" Text='<%#Eval("AlertText")%>' /> 
     </div> 
     <div class="setRead"> 
      <asp:CheckBox ID="AlertChecked" runat="server" /> 
     </div> 
    </ItemTemplate> 
</asp:ListView> 
<hr /> 
<div id="OkBtn" align="center"> 
    <asp:Button ID="OkButton" class="Button" runat="server" Text="Ok" onclick="OkButton_Click" /> 
</div> 
<asp:Label runat="server" ID="lblDisplay" /> 

次に、コードビハインドのサンプルを示します。私はデータアクセスコードをハードコードされたリストに置き換えました。私はあなたがデータベースからデータをロードするのに必要なコードにこれを置き換えるのが快適であると思います。

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      // Creates a list of alerts and binds them to the list view. 
      // You would use your sql data reader here instead. You could convert the data 
      // into a list or just bind the data reader directly to the list view. If you 
      // bind the data reader to the list view then be sure to update the Eval statements 
      // in the list view templates. 
      var alerts = new[] 
      { 
       new { AlertId = 1, AlertText = "This is alert #1."}, 
       new { AlertId = 2, AlertText = "This is the second alert."}, 
       new { AlertId = 3, AlertText = "This is the third alert."}, 
       new { AlertId = 5, AlertText = "This is alert #5."} 
      }; 
      listView.DataSource = alerts; 
      listView.DataBind(); 
     } 
    } 

    protected void OkButton_Click(object sender, EventArgs e) 
    { 
     // get the list of alerts that have been checked 
     var checkedAlerts = new List<int>(); 
     foreach (var listViewItem in listView.Items) 
     { 
      var checkbox = (CheckBox)listViewItem.FindControl("AlertChecked"); 
      if (checkbox.Checked) 
      { 
       checkedAlerts.Add((int)listView.DataKeys[listViewItem.DataItemIndex].Value); 
      } 
     } 
     // now do something to record them as read; we'll just display them in a label for this sample. 
     lblDisplay.Text = "The following alerts were marked read: " + String.Join(",", checkedAlerts); 
    } 
+0

ちょうど私に起こった1つの事:文字列を構築するためにEvalメソッドを使用することは可能ですか? –

+0

<%# %>タグ内に文字列連結を含む完全な式を入れることができます。 <+ Eval( "AlertText")+ "。説明は次のとおりです。" + Eval( "AlertId")%> –

関連する問題