0

I'm pretty new in ASP.net, and im trying to validate some output from my SQL DB.

I want to test if "img" from my DB is set to somthing, and if, i wan't to output it.

<asp:SqlDataSource ID="selectFromNews" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
    ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"    
    SelectCommand="SELECT * FROM [news] ORDER BY [time] DESC">
</asp:SqlDataSource>

<asp:Repeater ID="newsRepeater" runat="server" DataSourceID="selectFromNews" onitemcommand="newsRepeater_ItemCommand">
    <HeaderTemplate></HeaderTemplate>
    <ItemTemplate>
        <div class="newsBox">
            <h1><%# Eval("title") %></h1>
            /* Test if "img" is set to somthing, and output it here, if not, do somthing else */
            <p><%# Eval("text") %></p>
        </div>
    </ItemTemplate>
    <FooterTemplate></FooterTemplate>
</asp:Repeater>

I was thinking getting the data from behindcode, and then testing and sending an output back to a Literal, but dont know how i would access the repeaters data from there?

4

3 に答える 3

3

最初にリピーターに画像コントロールを追加します

    <asp:Image ID="ImageID" runat="server" ImageUrl='<%# Eval("img")%>' Visible="false" />

次に、リピーターの ItemDataBound イベントでこのイメージ コントロールを次のように取得します。

     protected void YourRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
      {
           if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
               Image ImageID= e.Item.FindControl("ImageID") as Image;
              // now play with your ImageID control..
            }
       }
于 2012-08-28T08:13:32.707 に答える
0

残念ながら、<% If ... Else ... End %>リピーター内でのブロックの使用は利用できません (私を信じてください、私は何度もブロックが欲しかったのです)。

これを行う最も簡単な方法は、コード ビハインド関数を呼び出すことです。例えば...

protected string MyFunction(RepeaterItem row)
{
   // return based on row.DataItem value
   return (row.DataItem.ImgExists ? "Image" : "No Image");
}

そして、マークアップでは、Containerオブジェクトを渡すだけです...

<%#MyFunction(Container)%>

編集...より複雑な視覚的な変更については、ItemDataBoundTimとUsman(およびNikhil)が提供するソリューションをお勧めします

于 2012-08-28T08:10:51.717 に答える
0

リピーターのItemDataBoundイベントを使用する必要があります。

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    RepeaterItem ri = e.Item;
    DataRowView dr = (DataRowView)ri.DataItem;
    Panel Panel1 = (Panel)ri.FindControl("Panel1");
    // do your evaluation here according the values in the DataRowView     
    // use ri.FindControl("ID") to find controls in the Itemtemplate
}

aspx:

<ItemTemplate>
    <asp:Panel id="Panel1" runat="server" class="newsBox">
        <h1><%# Eval("title") %></h1>
         add controls here with runat=server, then you can find them from codebehind
        <p><%# Eval("text") %></p>
    </asp:Panel>
</ItemTemplate>
于 2012-08-28T08:12:18.540 に答える