1、在頁面中指定地方增加PlaceHolder
2、在PlaceHolder中增加控制項。
註:產生控制項的代碼需放在Page_Load中 ,且不能加IsPostBack判斷。否則找到不控制項。
範例程式碼:
string sSQl = "";
sSQl = @"select sfcId,sfcDeptID,sfcTableName,sfcTabletitle
from hp_satisfactioncreate ssc";
sSQl = string.Format(sSQl, Resources.Resource.SysSatisfactionDept);
DataView dvData = TDAIClient.BusinessDB.GetDataView(sSQl);
if (dvData != null)
{
//增加斷行符號
//LiteralControl lcVal = new LiteralControl("<br/>");
//增加空格
//LiteralControl lcSpace = new LiteralControl(@" ");
if (dvData.Count > 0)
{
/*
* 此處產生一個兩列的表
*/
//定義表
HtmlTable htTab = new HtmlTable();
htTab.Style.Add(HtmlTextWriterStyle.Width, "100%");
//定義表中的行
HtmlTableRow htrTr = new HtmlTableRow();
for (int iRow = 0; iRow < dvData.Count; iRow++)
{
//定義行中的例
HtmlTableCell htcTd = new HtmlTableCell();
htcTd.Style.Add(HtmlTextWriterStyle.TextAlign, "center");
htcTd.Style.Add(HtmlTextWriterStyle.Color, "blue");
//定義LinkButton控制項
LinkButton lbn = new LinkButton();
lbn.Text = dvData[iRow]["sfcTabletitle"].ToString();
lbn.ToolTip = dvData[iRow]["sfcId"].ToString();
string sUrl = "SysSatisfactionDept.aspx?strID={0}";
sUrl = string.Format(sUrl, dvData[iRow]["sfcId"].ToString());
lbn.PostBackUrl = sUrl;
//增加控制項
htcTd.Controls.Add(lbn);
htrTr.Cells.Add(htcTd);
htTab.Rows.Add(htrTr);
if (iRow % 2 == 0)
{
htrTr = new HtmlTableRow();
}
}
phControl.Controls.Add(htTab);
}
}