以前在页面中显示数据记录信息都是在GridView控件中,虽然这个空间很好用,但是美中不足的地方也是有的,例如在每个数据记录前面加上图标,使用GridView控件做出来就很不自然。
经过请教了解在后台代码中结合HTML语言也可以实现类似GridView控件显示的效果,而且更好。不多说了,代码先!
方法代码如下:
protected string str;
protected void Page_Load(object sender, EventArgs e)
{
databind();
}
private void databind()
{
string sql, picstr, left_str, cen_str, hig_str;
int n;
n = 8;
left_str = "8px";
cen_str = "2px";
hig_str = "20px";
sql = "Select TOP 8 A.KeCBH,A.KeCMC,convert(char(10),A.TianJRQ,120) As TianJRQ,B.FuZRBH,B.YongHBH,D.XingM ";
sql += " From T_JingPKC A ,T_KeCRY B ,T_JiaoS D ";
sql += " Where A.KeCDJBH=3 And A.KeCBH=B.KeCBH And B.YongHBH=D.YongHBH ";
sql += " And A.KeCLXBH=1 And B.LeiXBH=2 Order By A.TianJRQ DESC";
str = "<table width=100% border=0 cellpadding=0 cellspacing=0>";
DataSet ds = new DataSet();
ds = 获取.DataSets(sql);
string kcid, kcmc, fzr, rq;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
kcid = ds.Tables[0].Rows[i]["KeCBH"].ToString();
kcmc = ds.Tables[0].Rows[i]["KeCMC"].ToString();
fzr = ds.Tables[0].Rows[i]["XingM"].ToString();
rq = ds.Tables[0].Rows[i]["TianJRQ"].ToString();
str += "<tr><td width=100% style='padding-left:";
str += left_str + ";letter-spacing:" + cen_str + ";line-height:" + hig_str;
str += ";background-position: left center; background-repeat: no-repeat; background-image: url(" + picstr + ");'>";
str += "<a href=http://211.69.16.170/JPKC/JPKC_MB.aspx?kecbh=" + kcid + " target=_blank >";
str += "<font color='#000000'>";
str += " ";
str += kcmc;
str += "</font>";
str += "</a>";
if (Convert.ToInt32((System.DateTime.Now - Convert.ToDateTime(rq)).Days) < 5)
str += "[<font color=red>new</font>]";
str += "</td>";
str += "</tr>";
}
str += "<tr><td width=100% align=right>";
str += "<a href='' target=_blank>更多>>></a>";
str += "</td></tr>";
str += "</table>";
}
以上主要使用字符变量str来设置显示样式,进而生成表格的形式,来显示数据信息。
同时在.aspx.cs文件中返回输出字符串参数str
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MainInfor_GJJPKC.aspx.cs" Inherits="MainInfor_GJJPKC" %>
document.write("<%=str%>");
如有什么更好的方法和建议,请给与指点!