这两种方法都有效,但是使用以下方法的方法更好:
PlaceHolder PH = ctl.PlaceHoldNu == 1 ? (PlaceHolder)Page.Master.FindControl("PlaceHolder1") : (PlaceHolder)FindControl("PlaceHolder" + ctl.PlaceHoldNu);
Run Code Online (Sandbox Code Playgroud)
要么
PlaceHolder PH;
if (ctl.PlaceHoldNu == 1)
PH = (PlaceHolder)Page.Master.FindControl("PlaceHolder1");
else
PH = (PlaceHolder)FindControl("PlaceHolder" + ctl.PlaceHoldNu);
Run Code Online (Sandbox Code Playgroud)
另一种解决方案是:
PlaceHolder PH = ctl.PlaceHoldNu == 1
? (PlaceHolder)Page.Master.FindControl("PlaceHolder1")
: (PlaceHolder)FindControl("PlaceHolder" + ctl.PlaceHoldNu);
Run Code Online (Sandbox Code Playgroud)
此解决方案使用最少的行但保持可读性.