0

テーブルを含むリピーターがあります。リピーターアイテムテンプレートでテーブルのいくつかのテーブルセルを非表示にしたいASPXソースコードは次のとおりです。

  <ItemTemplate>
  <table style="width: 100%" align="center">
    <tr>
      <td style="width: 60px;" align="center">
        <img src="upload/companylogo/<%# Eval("companylogo") %>" />
      </td>
      <td align="left">
        <asp:Label runat="server" CssClass="lblcname" ID="Label1" Text='<%# Eval("companyname") %>' /></td>
      <td align="right">
        <asp:Label runat="server" ID="Label2" Text='<%# Eval("city") %>' /></td>
    </tr>
    <tr>
      <td runat="server" id="address" colspan="3">
        <asp:Label runat="server" ID="Label3" Text='<%# Eval("address") %>' />
      </td>
    </tr>
    <tr>
      <td colspan="3" align="right" id="vp" runat="server">
        <a href='nfonews.aspx?id=<%# Eval("mpid") %>'>view Profile</a>
        &raquo; Send Inquiry </td>
    </tr>
    <tr>
      <td colspan="3" style="height: 20px; background-image: url(image/stripe_head_bg.png)"></td>
    </tr>
  </table>
</ItemTemplate>

そして私のコードビハインド:

SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows == true)
{
  dr.Read();
  if (dr["payment"].ToString()  == "Yes")
  {
    Repeater1.DataBind();
    if (Repeater1.Items.Count == 0)
    {
      Repeater1.Visible = false;
    }
    else
    {
      Repeater1.Visible = true;
    }
  }
}
4

2 に答える 2

3

グリッドの ItemDataBound イベントで、FindControl を使用してセルを検索します。

属性を追加します。onitemdatabound="myRepeater_ItemDataBound"

次に、コードビハインドで

protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   ListItemType rowType = (ListItemType)e.Item.ItemType;
   if (rowType == ListItemType.Pager || rowType == ListItemType.Header || rowType == ListItemType.Footer)
      return;

   TableCell cell = (TableCell)e.Item.FindControl("address");
}
于 2009-03-25T11:19:41.613 に答える
0

最も簡単な方法は、tdのvisible属性を使用し、サーバー側の式に基づいて値を割り当てることです。特定の列を表示/非表示にする条件について言及していないため、以下のコードは可能な方法の例です。

<table style="width: 100%" align="center">
  <tr>
    <td style="width: 60px;" align="center" runat="server" visible="<%#showCompanyLogo %>">
      <img src="upload/companylogo/<%# Eval("companylogo") %>" />
    </td>
    <td align="left" runat="server" visible="<%#showCompanyName %>">
      <asp:Label runat="server" CssClass="lblcname" ID="Label1" Text='<%# Eval("companyname") %>' /></td>
    <td align="right" runat="server" visible="<%#showCity %>">
      <asp:Label runat="server" ID="Label2" Text='<%# Eval("city") %>' /></td>
  </tr>
...
</table>

showCompanyLogo, showCompanyName and showCityProtectedコードビハインドで宣言され、評価する条件に応じて設定されるブール変数(アクセスレベル付き)です。

runat="server"サーバー式を評価するには、テーブルセルが必要であることに注意してください。

または、Ianによって提示されたソリューションを使用することもできますが、テーブル全体を、すべての要素が。とマークされたテーブルWebサーバーコントロールに変換する必要がありますrunat="server"

于 2009-03-25T11:35:26.707 に答える