Click here to Skip to main content
15,886,783 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
Hi I am using below code to conditionally add sriptmanager tag ,but below code is always adding scriptmanager tag irrespective of condition.
Please check the flaw in this code and suggest how can I add ScriptManager

    <form id="Form1" method="post" runat="server" class=top_form>
    <asp:placeholder id="ViewPlaceholder" runat="server"></asp:placeholder>
               <%
    if (Request.QueryString["UseAjax"] == "true")
    {
                   bool AppValue= Convert.ToBoolean(ConfigurationManager.AppSettings.Get("AddScriptManager"));
                  if (AppValue){
    %>
            <asp:ScriptManager ID="smanager1" runat="server" ScriptMode="Release" />

    <%}
    }%>
    </form>

Thanks,
Salmon
Posted

1 solution

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:

C#
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.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900