1

私はXMLソースを持っており、フィールドの1つは「説明」です。これは長さが変わる可能性がありますが、常にかなり長いです。これをasp.netリピーターに渡すときは、一貫性と簡潔さのために、表示される文字数を制限したいと思います。これを行う方法はありますか?言う...300文字。

前もって感謝します!

私のフロントエンドコード:

       <asp:Repeater ID="xPathRepeater" runat="server">
        <ItemTemplate>
            <li>
                <h3><%#XPath ("title") %></h3>
                <p><%#XPath("description")%></p>
            </li>
        </ItemTemplate>
       </asp:Repeater>

背後にある私のコード:

    protected void XMLsource()
{
    string URLString = "http://ExternalSite.com/xmlfeed.asp";

    XmlDataSource x = new XmlDataSource();
    x.DataFile = URLString;
    x.XPath = String.Format(@"root/job [position() < 5]");

    xPathRepeater.DataSource = x;
    xPathRepeater.DataBind();
}
4

2 に答える 2

3

返されたXPathクエリの値にSubStringを使用できるのではないでしょうか。

于 2012-03-26T18:02:59.007 に答える
1

XMLは以下のようになると思います。

<Root>
   <Row id="1">
     <title>contact name 1</name>
     <desc>contact note 1</note>
   </Row>
   <Row id="2">
     <title>contact name 2</title>
     <desc>contact note 2</desc>
   </Row>
</Root>

ここからの参照

HTMLを次のように置き換えます。

<h3><asp:Label ID="title" runat="server"></asp:Label></h3>
<p><asp:Label ID="desc" runat="server"></asp:Label></p>

OnItemDataBoundリピーターのイベントを登録し、次のコードを記述します。

protected void ED_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item)
    {
        Label title = (Label)e.Item.FindControl("title");
        title.Text = ((System.Xml.XmlElement)e.Item.DataItem).ChildNodes[0].InnerText;

        Label desc = (Label)e.Item.FindControl("desc");
        desc.Text = ((System.Xml.XmlElement)e.Item.DataItem).ChildNodes[1].InnerText.Substring(1, 300) + "...";
    }
}
于 2012-03-26T19:13:02.430 に答える