Repeater: condition of a field being blank or not? to Tag or not to Tag

Discussion in 'ASP .Net' started by HockeyFan, Dec 7, 2006.

  1. HockeyFan

    HockeyFan Guest

    I have the following inside a repeater:

    <ItemTemplate>
    <b><%#Container.DataItem("TitleText")%></b><br />
    <%#Container.DataItem("ParagraphText")%>

    <br /><br />
    </ItemTemplate>


    if for some reason, the TitleText is blank or null I'd like to not have
    the <br/> tag. In other words, I only want the <br/> tag if TitleText
    has something in it.
    Can I do this, and if so, how best to accomplish?
     
    HockeyFan, Dec 7, 2006
    #1
    1. Advertising

  2. HockeyFan

    Karl Seguin Guest

    There are a number of ways, the cleanest and one that'll scale the best is
    to hook into the ItemDataBound event of your repeater and control the output
    programatically.

    For example, you could do:

    <asp:literal id="Extra" runat="server"><br /><br /></asp:Literal>

    and in the ItemDataBound do:

    if (records are null)
    {
    ((Literal)e.Item.FindControl("Extra")).Visible = false;
    }

    You can read up more at:
    http://openmymind.net/index.aspx?documentId=8#4.2

    Karl
    --
    http://www.openmymind.net/
    http://www.fuelindustries.com/


    "HockeyFan" <> wrote in message
    news:...
    >I have the following inside a repeater:
    >
    > <ItemTemplate>
    > <b><%#Container.DataItem("TitleText")%></b><br />
    > <%#Container.DataItem("ParagraphText")%>
    >
    > <br /><br />
    > </ItemTemplate>
    >
    >
    > if for some reason, the TitleText is blank or null I'd like to not have
    > the <br/> tag. In other words, I only want the <br/> tag if TitleText
    > has something in it.
    > Can I do this, and if so, how best to accomplish?
    >
     
    Karl Seguin, Dec 7, 2006
    #2
    1. Advertising

  3. HockeyFan

    HockeyFan Guest

    I ended up solving the problem as follows:

    <ItemTemplate>
    <b><%#Container.DataItem("TitleText")%></b><%#IsBlankOrNull(Container.DataItem("TitleText"),
    "", "<br />")%>
    <%#Container.DataItem("ParagraphText")%>

    <br /><br />
    </ItemTemplate>

    and then the following in my base class:

    Public Function IsBlankOrNull(ByVal blnValue As Object, ByVal
    objValueIfTrue As String, ByVal objValueIfFalse As String) As String
    If blnValue Is DBNull.Value Then
    Return objValueIfFalse
    ElseIf CType(blnValue, String) = Nothing Then
    Return objValueIfFalse
    Else
    If CType(blnValue, String) = "" Then
    Return objValueIfFalse
    Else
    Return objValueIfTrue
    End If
    End If
    End Function


    Karl Seguin wrote:
    > There are a number of ways, the cleanest and one that'll scale the best is
    > to hook into the ItemDataBound event of your repeater and control the output
    > programatically.
    >
    > For example, you could do:
    >
    > <asp:literal id="Extra" runat="server"><br /><br /></asp:Literal>
    >
    > and in the ItemDataBound do:
    >
    > if (records are null)
    > {
    > ((Literal)e.Item.FindControl("Extra")).Visible = false;
    > }
    >
    > You can read up more at:
    > http://openmymind.net/index.aspx?documentId=8#4.2
    >
    > Karl
    > --
    > http://www.openmymind.net/
    > http://www.fuelindustries.com/
    >
    >
    > "HockeyFan" <> wrote in message
    > news:...
    > >I have the following inside a repeater:
    > >
    > > <ItemTemplate>
    > > <b><%#Container.DataItem("TitleText")%></b><br />
    > > <%#Container.DataItem("ParagraphText")%>
    > >
    > > <br /><br />
    > > </ItemTemplate>
    > >
    > >
    > > if for some reason, the TitleText is blank or null I'd like to not have
    > > the <br/> tag. In other words, I only want the <br/> tag if TitleText
    > > has something in it.
    > > Can I do this, and if so, how best to accomplish?
    > >
     
    HockeyFan, Dec 7, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -
    Replies:
    12
    Views:
    702
    Remon van Vliet
    Jun 15, 2005
  2. shruds
    Replies:
    1
    Views:
    881
    John C. Bollinger
    Jan 27, 2006
  3. Bill W.
    Replies:
    13
    Views:
    306
    Phillip Gawlowski
    May 9, 2011
  4. Sound
    Replies:
    2
    Views:
    466
    Randy Webb
    Sep 28, 2006
  5. jr
    Replies:
    3
    Views:
    454
Loading...

Share This Page