Here is the solution:
1. Keep only the
PlaceHolder
control in the aspx:
<asp:placeholder id="ViewPlaceholder" runat="server"></asp:placeholder>
2. Put the conditional code in the CodeBehind:
protected void Page_Init(object sender, EventArgs e)
{
if (Request.QueryString["UseAjax"] == "true")
{
bool AppValue = Convert.ToBoolean(ConfigurationManager.AppSettings.Get("AddScriptManager"));
if (AppValue)
{
ScriptManager script = new ScriptManager();
script.ID = "smanager1";
script.ScriptMode = ScriptMode.Release;
ViewPlaceholder.Controls.Add(script);
}
}
}
Unfortunately, if you put the conditional code in the Markup, it will not work as expected in case if you want to include a
ScriptManager
. In such requirement, you have to add the
ScriptManager
in the CodeBehind as described above.