个人建议
如果需要考虑性能,哪么显示用repeater最好,如果不考虑性能,哪么用 gridview最方便。
但是lz非不用控件要显示数据的话,也是可以的,在后台读取了数据以后,动态生成一个表格,把表格添加到页面上就可以了!
代码如下:
string StrConn = @"User Id=sa;Password=perishryu;Initial Catalog=pubs;Data Source=oathryu\sql2005";
SqlDataAdapter da;
DataSet ds;
Table tb;
TableHeaderRow thr;
TableHeaderCell th;
TableRow tr;
TableCell td;
public override void DataBind()
{
ds = new DataSet();
da = new SqlDataAdapter("select * from jobs",StrConn);
da.Fill(ds);
tb= new Table();
thr = new TableHeaderRow();
tb.Rows.Add(thr);
//添加表头
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
th = new TableHeaderCell();
th.Text = ds.Tables[0].Columns[i].Caption;
thr.Cells.Add(th);
}
thr.Attributes.Add("style", "background-Color:#aaddff");
//添加数据
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
tr = new TableRow();
tr.Attributes.Add("style", "background-Color:#aaddff");
tr.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#369';this.style.color='#fff';");
tr.Attributes.Add("onmouseout", "this.style.backgroundColor=c;this.style.color='#000';");
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
td = new TableCell();
td.Attributes.Add("onmouseover", "this.style.cursor='hand';");
td.Text = ds.Tables[0].Rows[i][j].ToString();
tr.Cells.Add(td);
}
tb.Rows.Add(tr);
}
//设定样式
tb.BorderColor = System.Drawing.Color.Red;
tb.BorderStyle = BorderStyle.Dotted;
tb.BorderWidth = 1;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBind();
this.Controls.Add(tb);
}
}