62,046
社区成员
发帖
与我相关
我的任务
分享
protected void Page_Load(object sender, EventArgs e)
{
setbin();
}
private string sConnectionString = ConfigurationManager.ConnectionStrings["CompanyConnectionString"].ToString();
private void setbin()
{
SqlConnection conn = new SqlConnection(sConnectionString);
string sql = "SELECT left(Co_Content,50) FROM CompanyInfo";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds=new DataSet();
da.Fill(ds);
//DataTable dt = ds.Tables[0];
DataList1.DataSource = ds;
DataList1.DataBind();
}
}
前台:
<HeaderTemplate>
公司简介
</HeaderTemplate>
<ItemTemplate>
<br /> <asp:Label ID="Label2" runat="server"
style="white-space: pre; break-all" Text='<%# Eval("Co_Content") %>'
Visible="False"></asp:Label>
<br />
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Co_Content") %>'
Height="174px" TextMode="MultiLine"
Width="240px" BackColor="#99FF33" BorderColor="Red" BorderStyle="Dashed"
BorderWidth="0px" ReadOnly="True"></asp:TextBox>
<img alt="" src="Image/<%# Eval("Co_Image") %>" dir="rtl" height="174" width="240" />
<br />
<asp:Label ID="Label3" runat="server" Text='<%# Eval("Co_Image") %>'
Width="40px"></asp:Label>
<br />
</ItemTemplate>
<SelectedItemStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
</asp:DataList>
<div>
/// <summary>
/// 截取字符串长度的方法,可判断中英文字符
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string stringformat(string str,int n)
{
///
///格式化字符串长度,超出部分显示省略号,区分汉字跟字母。汉字2个字节,字母数字一个字节
///
string temp = string.Empty;
if (System.Text.Encoding.Default.GetByteCount(str) <= n)//如果长度比需要的长度n小,返回原字符串
{
return str;
}
else
{
int number = 0;
string st = string.Empty;
while (number < n)
{
st = str.Substring(0, 1);
temp += st;
str = str.Substring(1, str.Length - 1);
if (System.Text.Encoding.Default.GetByteCount(st) > 1)
{
number += 2;
}
else
{
number++;
}
}
return (temp + "...");
}
}
SELECT left(Co_Content,50) as Co_Content FROM CompanyInfo