locked
<script type="text/javascript"> inside <script runat="server"> RRS feed

  • Question

  • User1120880999 posted

    Hi,

    I got problem with showing javascrip alert. I've struggled with such alert:

    only content controls are allowed directly in a content page that contains content controls

    I've got such code:

    <script runat="server">
    
        protected void Page_Load()
        {
    
            if (this.IsPostBack)
            {
                string eventTarget = (this.Request["__EVENTTARGET"] == null) ? string.Empty : this.Request["__EVENTTARGET"];
    
                if (eventTarget == "SetSessionVariable")
                {
                    Button_Click();
                }
            }
            else
            {
            }
    
        protected void Button_Click()
        {
                string message = ClassBusiness.Configuration.Instance.MinimumAgeMessage();
                Response.Write("<script type='text/javascript'>alert('" + message + "');</script>");
        }
    </script>
    
        <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolderHead" Runat="Server">
            <link href="CSS/Summary.css" rel="stylesheet" type="text/css" />
        </asp:Content>
    
        <asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolderBody" Runat="Server">
          <input type="button" name="proceedPayment" value="Proceed to payment" onclick="setSessionVariable()"/>
        </asp:Content>

    I was trying ClientScriptManager but it doesn't work, too. As I understand it is so because it is still about putting script inside script.  

    How can I do it correctly?

    Thanks,

    Tomek 

    Monday, July 29, 2013 8:18 AM

Answers

  • User455156504 posted


    What I mean is something like the following

    Master Page
    
    <html>
    <head>
    // js and css files used in every site
    ...
    
    <asp:PlaceHolder id="ScriptSection" runat="server"></asp:PlaceHolder>
    
    </head>
    <body>
    ...
    </body>
    </html>
    
    Child Page
    
    ...
    <asp:Content runat="server" contentplaceholderid="ScriptSection">
    
    ...
    private string msgbox(string message)
    
    {
      return("<script type='text/javascript'>alert('" + message + "');</script>");
    }
    ...
    
    </asp:Content>
    ...

    Here you have your scripts in the child page also wrapped in a content control, because only content controls can be top level elements in a child page.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 30, 2013 12:16 PM
  • User455156504 posted

    I haven't used it that way yet, but there's no other way, because you can only have content controls in a child page. Normally you would put this code into the code-behind file.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 31, 2013 12:01 PM

All replies

  • User-186167190 posted

    You will get the error due to the following reasons,

    1) Make sure the ContentPlaceHolderId in the master page matches with the Content page. It is case sensitive.

    2) Make sure that web.config file is updated with ajax extensions

    Let me know if the suggested answer is not fixing your issue.

    Monday, July 29, 2013 8:29 AM
  • User1120880999 posted

    sh_asp.net

    1) Make sure the ContentPlaceHolderId in the master page matches with the Content page. It is case sensitive.

    there are no diffrences.

    sh_asp.net

    2) Make sure that web.config file is updated with ajax extensions

    <assemblies>
    <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    </assemblies>
    
    <controls>
    <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    <add tagPrefix="telerik" namespace="Telerik.Web.UI" assembly="Telerik.Web.UI"/>
    </controls>
    
    <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

    do this parts look like "ajax crumbs" from web.config? 

    Monday, July 29, 2013 9:03 AM
  • User-186167190 posted
    1. It sometimes happens when you are referencing an assembly in web.config and it does not exists in the bin folder e.g. AjaxControlToolKit.dll
    2. Remove the below from your aspx page as you are using master page.
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head runat="server">
          <title></title>
          <link href="CSS/Style.css" type="text/css" rel="Stylesheet" />
      </head>
      <body dir="rtl">
          <form id="form1" runat="server">
          </form>

    Monday, July 29, 2013 10:04 AM
  • User455156504 posted

    Hi,

    you have to place your script section also in a content control. For that, you only have to add a placeholder into your master page which is placed in your head section. If you want to use scripts, CSS, or something else related to the head section you put that into that content control in your child pages.

    Monday, July 29, 2013 2:07 PM
  • User1120880999 posted

    Horizon_Net thank you for replay, however I'm not sure if I get you. Can you give me some example based on my code, please.

    To make situation clear, I have to change a bit some code which unfortunalty had been compilled to ddl. That's why I have html code and c# code in one file. I'm using functions which I found in dll. For some reasons validation errors are shown in this way:

    private string msgbox(string message)
    
    {
      return("<script type='text/javascript'>alert('" + message + "');</script>");
    }

    The code i put at the begining of the post, this is how I'm trying to apply this function.

    Monday, July 29, 2013 3:33 PM
  • User455156504 posted


    What I mean is something like the following

    Master Page
    
    <html>
    <head>
    // js and css files used in every site
    ...
    
    <asp:PlaceHolder id="ScriptSection" runat="server"></asp:PlaceHolder>
    
    </head>
    <body>
    ...
    </body>
    </html>
    
    Child Page
    
    ...
    <asp:Content runat="server" contentplaceholderid="ScriptSection">
    
    ...
    private string msgbox(string message)
    
    {
      return("<script type='text/javascript'>alert('" + message + "');</script>");
    }
    ...
    
    </asp:Content>
    ...

    Here you have your scripts in the child page also wrapped in a content control, because only content controls can be top level elements in a child page.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 30, 2013 12:16 PM
  • User1120880999 posted

    Thanks. Now I got you.

    But I'm affraid if I write clear enough. This msgbox is c# function not a javascript. So it doesn't make sence to put this function into ContentPlaceHoler, does it?

    Wednesday, July 31, 2013 6:38 AM
  • User455156504 posted

    I haven't used it that way yet, but there's no other way, because you can only have content controls in a child page. Normally you would put this code into the code-behind file.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 31, 2013 12:01 PM