在asp.net 2.0中,如果要在绑定列中显示比如日期格式等,如果用下面的方法是显示不了的
主要是由于htmlencode属性默认设置为true,已防止XSS攻击,安全起见而用的,所以,可以有以下两种方法解决 1、
HeaderText=\
将htmlencode设置为false即可
另外的解决方法为,使用模版列
Text='<%# Eval(\
Text=’<%# Bind(\-dd-yyyy}\
前台代码:
AutoGenerateColumns=\ DataSourceID=\AllowSorting=\BackColor=\BorderColor=\BorderStyle=\BorderWidth=\CellPadding=\Font-Size=\
SelectCommand=\出生日期], [起薪], [身份证号码], [姓名], [家庭住址], [邮政编码] FROM [飞狐工作室]\DataSourceMode=\ 附录-常用格式化公式: {0:C} 货币; {0:D4}由0填充的4个字符宽的字段中显示整数; {0:000.0}四舍五入小数点保留第几位有效数字; {0:N2}小数点保留2位有效数字;{0:N2}% 小数点保留2位有效数字加百分号; {0:D}长日期;{0:d}短日期;{0:yy-MM-dd} 例如07-3-25;;{0:yyyy-MM-dd} 例如2007-3-25 11.GridView实现用“...”代替超长字符串: 效果图: 解决方法:数据绑定后过滤每一行即可 for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { DataRowView mydrv; string gIntro; if (GridView1.PageIndex == 0) { mydrv = myds.Tables[\飞狐工作室\表名 gIntro = Convert.ToString(mydrv[\家庭住址\所要处理的字段 GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2); } else { mydrv = myds.Tables[\飞狐工作室\GridView1.PageIndex)]; gIntro = Convert.ToString(mydrv[\家庭住址\ GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2); } } 调用的方法: public string SubStr(string sString, int nLeng) { if (sString.Length <= nLeng) { return sString; } string sNewStr = sString.Substring(0, nLeng); sNewStr = sNewStr + \ return sNewStr; } 后台全部代码: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { SqlConnection sqlcon; + (5 * SqlCommand sqlcom; string strCon = \北风贸易;Uid=sa;Pwd=sa\ protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ViewState[\身份证号码\ ViewState[\ bind(); } } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; bind(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string sqlstr = \from 飞狐工作室 where 身份证号码='\+ GridView1.DataKeys[e.RowIndex].Value.ToString() + \ sqlcon = new SqlConnection(strCon); sqlcom = new SqlCommand(sqlstr,sqlcon); sqlcon.Open(); sqlcom.ExecuteNonQuery(); sqlcon.Close(); bind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { sqlcon = new SqlConnection(strCon); string sqlstr = \飞狐工作室 set 姓名='\ + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + \家庭住址='\ + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + \where 身份证号码='\ + GridView1.DataKeys[e.RowIndex].Value.ToString() + \ sqlcom=new SqlCommand(sqlstr,sqlcon); sqlcon.Open(); sqlcom.ExecuteNonQuery(); sqlcon.Close(); GridView1.EditIndex = -1; bind(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; bind(); } public void bind() { string sqlstr = \飞狐工作室\ sqlcon = new SqlConnection(strCon); SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon); DataSet myds = new DataSet(); sqlcon.Open(); myda.Fill(myds, \飞狐工作室\ GridView1.DataSource = myds; GridView1.DataKeyNames = new string[] { \身份证号码\ GridView1.DataBind(); for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { DataRowView mydrv; string gIntro; if (GridView1.PageIndex == 0) { mydrv = myds.Tables[\飞狐工作室\ gIntro = Convert.ToString(mydrv[\家庭住址\ GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2); } else { mydrv = myds.Tables[\飞狐工作室\GridView1.PageIndex)]; gIntro = Convert.ToString(mydrv[\家庭住址\ GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2); } } sqlcon.Close(); } public string SubStr(string sString, int nLeng) { if (sString.Length <= nLeng) { return sString; + (5 *