c#
  • asp.net
  • 2009-07-02 22 views 1 likes 
    1

    对不起,如果帖子标题不清楚,我会尽量在这里解释一下。是否可以即时修改Databound内容?

    我正在使用数据绑定到数据表的Web控件。数据的输出是这样的:

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

    我写的,从理论上讲,应该修剪传递的字符串到指定数量的单词的功能:

    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: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 2009-07-02 16:45:47

    1

    RowDataBound事件中这样做会容易很多。

    +0

    感谢您的答复!我正在使用ASP:Repeater控件来显示我的数据。这个控件没有那个属性,有什么我可以做的吗? – Anders 2009-07-02 15:58:12

    0

    我不会用<%#的Bleh%>的东西在所有的这。您可以使用asp:Repeater的OnItemDataBound事件在代码隐藏中将Repeater数据绑定。

    只需设置您的转发器的数据源并在其上调用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 
    } 
    

    这种做法是一个更容易阅读和维护比弄脏了你的aspx

    +0

    谢谢,伙计。我最终使用了这个派生物,用Sternal先生发布的信息来弄清楚。 – Anders 2009-07-02 18:11:48

    相关问题