---
title: "动态生成asp.net控件"
author: "water84222"
date: "2006-07-12T14:06:00Z"
tags: []
slug: "asp-net"
draft: false
lastmod: "2006-07-12T14:06:00Z"
id: 156595
url: "https://onlyLove.iteye.com/blog/156595"
---

public void createdatagrid()  
{  
datatable flowtable=new workflowbll().getflowtablebycode(functioncode);  
foreach(datarow datarow in flowtable.rows)  
{  
table table=new table();  
table.cellpadding=0;  
table.cellspacing=0;  
table.width=unit.percentage(98);  
table.attributes.add("align","center");  
table.attributes.add("style","height:expression(document.body.offsetheight-toolbar.offsetheight-spacebar.offsetheight-33)");  
table.cssclass="plpanelunselect";  
table.attributes.add("imagessrc","images/contact.gif");  
table.attributes.add("panelcaption",(string)datarow\["bmc"]);  
tablerow tablerow=new tablerow();  
tablecell tablecell=new tablecell();  
tablecell.verticalalign=verticalalign.top;  
panel panel=new panel();  
panel.attributes.add("style","overflow: auto; width: 100%; height: 100%");  
datagrid=new datagrid();  
datagrid.borderwidth=0;  
datagrid.id=(string)datarow\["bdm"];  
datagrid.enableviewstate=false;  
datagrid.width=unit.percentage(100);  
datagrid.font.size=fontunit.parse("12px");  
datagrid.style\["border-collapse"]="none";  
datagrid.itemdatabound += new datagriditemeventhandler(datagrid\_itemdatabound);  
datagrid.datasource=new workflowbll().getflowdatabycode(functioncode,(string)datarow\["bdm"],datacode);  
datagrid.databind();  
panel.controls.add(datagrid);  
tablecell.controls.add(panel);  
tablerow.cells.add(tablecell);  
table.rows.add(tablerow);  
placeholder.controls.add(table);  
}  
}  
private void datagrid\_itemdatabound(object sender, system.web.ui.webcontrols.datagriditemeventargs e)  
{  
if ((e.item.itemtype==listitemtype.item)||(e.item.itemtype==listitemtype.alternatingitem))  
{  
htmlinputtext inputtext=new htmlinputtext();  
inputtext.attributes.add("class","clsinput");  
inputtext.value= e.item.cells\[1].text;  
//getflowwrite  
e.item.cells\[1].controls.add(inputtext);  
}  
for (int i=0;i {  
if (e.item.itemtype==listitemtype.header)  
{  
e.item.cells\[i].attributes.add("class","irtitle");  
}  
else{  
e.item.attributes.add("class","irbody");  
e.item.attributes.add("overclassname","irbodyover");  
e.item.attributes.add("cssclick","irbodyclick");  
}  
e.item.cells\[i].attributes.add("align","center");  
e.item.cells\[i].attributes.add("width",convert.tostring(100/e.item.cells.count)+"%");  
e.item.cells\[i].attributes.add("nowrap","true") ;  
}  
}