2009-07-02 4 views
1

申し訳ありません投稿のタイトルが明確でない場合は、ここで少し詳しく説明します。データバインドされたコンテンツをオンザフライで変更することはできますか?

データテーブルにデータバインドされているWebコントロールを使用しています。私は、この醜態を試してみました

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(); 
} 

::私は理論的には、単語の指定された数に渡された文字列をトリミングする必要があり、機能を書いた

<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> 

:のようなデータの出力がある

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

もちろん、動作しませんでした。したがって、このリテラルコントロール内にある間にDatabinder.Eval(...)でこの関数を使用することは可能ですか?もしそうなら、私はこれをどうやって行うべきですか?そうでない場合は、私がやろうとしていることに対する可能な代替手段は何でしょうか?

ありがとうございます!

答えて

2

あなたは(あなたの元Eval構文を使用して、文字列にキャストする)あなたの方法に直接Eval結果を提出することができます:

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

ありがとう、これは正しいトラックで私を得た:) – Anders

1

RowDataBoundイベントでこれを行う方がはるかに簡単です。

+0

おかげでお返事をあなたのaspxをmuddyingよりも維持することがずっと簡単である場合に、アイテム単位のより具体的な何かを行うことができます! ASP:Repeaterコントロールを使用してデータを表示しています。このコントロールにはそのプロパティはありません。他にも何かできますか? – Anders

0

私はこのために全く<%#のblehの%>のものを使用することはありません。 asp:RepeaterのOnItemDataBoundイベントを使用して、コードビハインドでリピータをデータバインドできます。

リピーターのデータソースを設定し、その上でDataBindを呼び出します。

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

は、その後、あなたは

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 
} 

このアプローチは、読んで

+0

ありがとう、男。私は、Sternalさんが投稿した情報を使って、これを派生させてしまいました。 – Anders

関連する問題