tags:

views:

117

answers:

3

Sorry if the post title wasn't clear, I will try to explain a little better here.

I am working with a web control that is databound to a data table. The output of the data is as such:

<asp:Repeater ID="RssRepeater" Visible="false" EnableViewState="false" runat="server">
    <asp:literal ID="sb_description" Text='<%# DataBinder.Eval (Container.DataItem, "description") %>' EnableViewState="false" runat="server" />
    ''// Rest of structure...
</asp:Repeater>

I wrote a function that, in theory, should trim a passed string to a specified number of words:

protected string CreateTeaser(string Post)
{
    int wordLimit = 50;
    System.Text.StringBuilder oSB = new System.Text.StringBuilder();

    string[] splitBy = new string[] { " " };

    string[] splitPost = Post.Split(splitBy,
                         System.StringSplitOptions.RemoveEmptyEntries);

    for (int i = 0; i <= wordLimit - 1; i++)
    {
        oSB.Append(string.Format("{0}{1}", splitPost[i], 
                  (i < wordLimit - 1) ? " " : ""));
    }

    oSB.Append(" ...");

    return oSB.ToString();
}

I tried this abomination:

<asp:literal ID="sb_description" Text='<%= CreateTeaser(%> <%# DataBinder.Eval (Container.DataItem, "description") %><%=); %>' EnableViewState="false" runat="server" />

But of course it did not work. So, is it possible to use this function on the Databinder.Eval( ... ) while it is inside this literal control? If so, how should I go about doing this? If not, what would be a possible alternative to what I am trying to do?

Thanks SO!

+1  A: 

It would be a lot easier to do that in the RowDataBound event.

Otávio Décio
Thanks for your reply! I am utilizing an ASP:Repeater control to display my data. This control does not have that property, is there something else I can do?
Anders
+1  A: 

You can submit the Eval result directly to your method (using your original Eval syntax and casting to a string):

<asp:literal 
    ID="sb_description" 
    Text='<%= CreateTeaser((string)DataBinder.Eval (Container.DataItem, "description")) %>'
    EnableViewState="false" 
    runat="server" 
/>
Jeff Sternal
thanks, this got me on the right track :)
Anders
A: 

I would not use the <%# bleh %> stuff at all for this. You can databind your Repeater in your codebehind using the OnItemDataBound event for the asp:Repeater.

Just set your repeater's Datasource and call DataBind on it.

List<stuff> feeds = //list of rss feeds, I am guessing.
RssRepeater.DataSource = feeds;
RssRepeater.DataBind();

Then you can do something more specific per-item in the event

protected void RssRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    Literal label = (Literal)e.Item.FindControl("sb_description");
    label.Text = CreateTeaser(post); //post coming from your repeater somewhere. I can't see its definition
    //post could probably be e.Item.DataItem, depending on how you do your DataSource
}

This approach is a lot easier to read and maintain than muddying up your aspx

Andy_Vulhop
Thanks, man. I ended up using a derivative of this, using the info that Mr. Sternal posted to figure it out.
Anders