1 Replies - 6572 Views - Last Post: 19 March 2011 - 02:38 AM Rate Topic: -----

#1 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 294
  • View blog
  • Posts: 1,533
  • Joined: 07-April 08

ASP.NET repeater control with Conditional

Posted 07 March 2011 - 10:28 AM

I'm trying to create a Private Message system for a website and decided on using the repeater control. I've got the repeater in and wired up so that it displays the messages properly; however, I need to add a conditional that changes the message to be bold based off of a flag from the database.

Here is my code:
<div id='accordion'>
    <asp:Repeater ID="Repeater1" runat="server" >
        <ItemTemplate>
            <div class='header' messageid="<%# DataBinder.Eval(Container.DataItem, "ParentID")%>" style='color:Black;'>
                <%# Response.Write(Convert.ToBoolean(DataBinder.Eval(Container.DataItem,"IsRead")) == true ? "<table width='100%' style='border:solid 1px black;font-weight:bolder;'>" : "<table width='100%' style='border:solid 1px black;'>") %>
                    <tr>
                        <td width="25%">
                            <%# DataBinder.Eval(Container.DataItem, "Sender")%>
                        </td>
                        <td width="50%">
                            <%# DataBinder.Eval(Container.DataItem, "Title").ToString().Length > 30 ? DataBinder.Eval(Container.DataItem, "Title").ToString().Substring(0, 30) + "..." : DataBinder.Eval(Container.DataItem, "Title").ToString()%>
                        </td>
                        <td width="25%">
                            <%# DataBinder.Eval(Container.DataItem, "MessageDate")%>
                        </td>
                    </tr>
                </table>
            </div>
            <div>
                <asp:LinkButton ID="Reply" runat="server" Text="Reply" />
                | <asp:LinkButton ID="Delete" runat="server" Text="Delete" />
                | <asp:LinkButton ID="Forward" runat="server" Text="Forward" />
                <br />
                <div class="Body<%# DataBinder.Eval(Container.DataItem, "ParentID")%>" style="width:400px; height:200px;"></div>
            </div>
            
        </ItemTemplate>
    </asp:Repeater>
</div>


and the code behind
 private void FetchData(int pPage, int pPageSize)
    {
        PageSize = pPageSize;
        var _UserContext = MembershipProvider.GetCurrentUserContext();
        PagedDataSource _Pager = new PagedDataSource();
        if (_UserContext != null)
        {
            ServiceProxy _Client = new ServiceProxy();

            _PMList = _Client.GetReceivedMessegesforUser(out _numberofpages, out RowCount, _UserContext.User.LastSessionId.Value, pPage, pPageSize).ToList<PrivateMessage>();

            _Pager.DataSource = _PMList;
            _Pager.AllowCustomPaging = true;
            _Pager.AllowPaging = true;
            _Pager.PageSize = pPageSize;
            Repeater1.DataSource = _Pager;
            Repeater1.DataBind();

            if (!IsPostBack)
            {
                CreatePager();
            }
        }
    }



Basically the error is happening on this line:

                <%# Response.Write(Convert.ToBoolean(DataBinder.Eval(Container.DataItem,"IsRead")) == true ? "<table width='100%' style='border:solid 1px black;font-weight:bolder;'>" : "<table width='100%' style='border:solid 1px black;'>") %>



I've tried Convert.ToBoolean, and (Boolean) but each one comes up with the same error:
error CS1502: The best overloaded method match for 'System.Convert.ToString(object, System.IFormatProvider)' has some invalid arguments

I'm not sure at this point how to get the conditional to work. The Eval statement will always bring back a true/false from the database.

EDIT: in looking at the ToBoolean it appears like i'm missing an IFormatProvider, but in looking at MSDN it seems like i have to create an instance of IFormatProvider, but I don't think i can do that within the scope of the Conditional's server tags can i?

This post has been edited by rgfirefly24: 07 March 2011 - 11:40 AM


Is This A Good Question/Topic? 0
  • +

Replies To: ASP.NET repeater control with Conditional

#2 FungalMonkey  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 37
  • Joined: 13-December 09

Re: ASP.NET repeater control with Conditional

Posted 19 March 2011 - 02:38 AM

Is this because databinder.eval... is an object not immediately recognized as a database boolean value?

Have you try
<%# Response.Write(Convert.ToBoolean(Eval("IsRead")) ? "<table width='100%' style='border:solid 1px black;font-weight:bolder;'>" : "<table width='100%' style='border:solid 1px black;'>") %>



or

<%# Response.Write(Eval("IsRead") ? "<table width='100%' style='border:solid 1px black;font-weight:bolder;'>" : "<table width='100%' style='border:solid 1px black;'>") %>




Does it make any difference?

FungalMonkey
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1