擴充 AscxUtility.RenderControl<T> 多載,支援載入控制項後透過委派或反射設定屬性值。
public static HtmlString RenderControl<T>(string path, Action<T> setter)
public static HtmlString RenderControl<T>(string path, object propertyValues)
ASCX 定義:WebUserControl2.ascx 含 TextBox txtName、屬性 public string UserName { set { txtName.Text = value; } }
Controller:
public ActionResult UcWithValue() {
return Content(AscxUtility.RenderControl<WebUserControl2>(
"~/AspNets/WebUserControl2.ascx",
c => c.UserName = "Insus.NET").ToString());
}
委派模式型別安全、IntelliSense 友善;匿名物件模式適合動態屬性設定。解決「載入後如何賦值」的核心痛點。