我在描述栏中有 200 个单词,但我只想在 Datalist 的标签中显示 100 个单词。SqlDataAdapter adp = new SqlDataAdapter("select * from tbitem", ConfigurationManager.ConnectionStrings["cn"].ConnectionString); DataSet ds = new DataSet(); adp.Fill(ds); DataList1.DataSource = ds; DataList1.DataBind();在 HTML 中<asp:DataList ID="DataList1" runat="server"> <ItemTemplate> <b>Name: </b> <%#Eval("itemnam") %><br /> <b>Description: </b> <asp:Label ID="Label1" runat="server" Text='<%#Eval("itemdescription") %>'></asp:Label> <br /><br /> </ItemTemplate> </asp:DataList>描述是Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Viverra justo nec ultrices dui. At varius vel pharetra vel turpis nunc eget lorem dolor. Iaculis at erat pellentesque adipiscing commodo elit at imperdiet. Arcu non odio euismod lacinia at quis risus. Quam adipiscing vitae proin sagittis nisl. Consectetur lorem donec massa sapien faucibus et molestie. Mattis vulputate enim nulla aliquet porttitor lacus luctus accumsan. Quis imperdiet massa tincidunt nunc. Risus sed vulputate odio ut enim blandit volutpat. Nunc id cursus metus aliquam eleifend. Tristique magna sit amet purus gravida quis blandit turpis. Sollicitudin tempor id eu nisl nunc. Enim eu turpis egestas pretium aenean pharetra magna. Nibh nisl condimentum id venenatis. Duis at tellus at urna condimentum mattis pellentesque.它背后的代码是什么?
1 回答
蛊毒传说
TA贡献1895条经验 获得超3个赞
一种方法是,
您可以遍历数据集中数据表中的行,然后只从每行的列中取出 100 个拆分词,然后从拆分词中生成字符串并将其绑定到同一列
该代码未经测试,但您可以使用如下所示的代码,
DataSet ds = new DataSet();
adp.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
{
row["itemdescription"] = string.Join(" ", Convert.ToString(row["itemdescription"]).Split(new char[] { ' ' }).Take(100));
}
ds.Tables[0].AcceptChanges();
DataList1.DataSource = ds;
DataList1.DataBind();
- 1 回答
- 0 关注
- 63 浏览
添加回答
举报
0/150
提交
取消