如標題
//寫法為擴充方法
public static T FindMasterPageControl<T>(this Control co, string ContentPlaceHolder, string controlID)
{
Page page = HttpContext.Current.Handler as Page;
Control c = page.Master.FindControl(ContentPlaceHolder).FindControl(controlID);
if (c == null)
throw new Exception("找不到 id=" + controlID + " 的控制項");
try
{
T rtn = (T)Convert.ChangeType(c, typeof(T));
return rtn;
}
catch (InvalidCastException e1)
{
throw new Exception("id-" + controlID + " 的控制項存在, 但無法轉型成指定型別" + e1.Message);
}
catch (Exception e2)
{
throw e2;
}
}
以上文章僅用紀錄資料使用.....