重點:一定要寫在Page_Init
protected void Page_Init(object sender, EventArgs e)
{
//動態新增
DataTable dt = new DataTable();
dt.Load(GetZipCity());
if (dt.Rows.Count > 0)
{
LinkButton[] btnZipCity = new LinkButton[dt.Rows.Count];
for (int i = 0; i < btnZipCity.Length; i++)
{
btnZipCity[i] = new LinkButton();
btnZipCity[i].ID = "btnZipCity" + i.ToString();
btnZipCity[i].Text = dt.Rows[i]["CityName"].ToString();
btnZipCity[i].CommandName = dt.Rows[i]["CityID"].ToString();
btnZipCity[i].Click += new System.EventHandler(btnZipCity_Click);
btnZipCity[i].Command += new CommandEventHandler(btnZipCity_Click);
this.PlaceHolder.Controls.Add(btnZipCity[i]);
Label olab = new Label();
olab.Text = " | ";
PlaceHolder.Controls.Add(olab);
}
}
}
// ZipCity Button 的 Click 事件導向函數
protected void btnZipCity_Click(object sender, EventArgs e)
{
LinkButton ZipCity = (LinkButton)sender;
QueryZipCity = ZipCity.CommandName;
}
參考資料:http://careychen.pixnet.net/blog/post/22622126-%5Basp.net%5D-%E5%9C%A8-web-form-%E5%8B%95%E6%85%8B%E5%BB%BA%E7%AB%8B%E6%8E%A7%E5%88%B6%E9%A0%85%E7%9A%84%E6%AD%A3%E7%A2%BA%E6%96%B9%E6%B3%95
0 意見: