Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ScriptManager.RegisterClientScriptBlock not working fom masterpage code behind file

Posted on 2010-08-13
7
Medium Priority
?
2,511 Views
Last Modified: 2012-05-10
Hi,
  The have the jquery powered signup and signin div on master page. when the user clicks the link  the modal popup comes uo and the user signup. i kept my functionality on the master.cs.
Everything works fine in master.cs except calling a javascript fuction from master.cs.
    I have included Script manager on the master page, i am using update panel on the master page.master.cs. The fuction  ScriptManager.RegisterClientScriptBlock does not fires the javascript function



Below is the code from Masterpage.Master

<head runat="server">
   <script type="text/javascript">
        function TestJs()

       {
            alert("Testing javscript fire");

       }
    
         function closeDialog(id) {
            $('#' + id).dialog("close");
        }
        
       </script>

</head>

<body>
 
    <form id="form1" class="niceform" runat="server">
       <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div id="webpage-container">
    
      
        <div id="modaldialog-container">
            <div id="signup-container">
                <asp:UpdatePanel ID="Upsignup" UpdateMode="Conditional" ChildrenAsTriggers="true" runat="server">
                    <ContentTemplate>

                    //signup contents
                    </ContentTemplate>                
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="btnSubmit" EventName="Click" />
                        <asp:AsyncPostBackTrigger ControlID="btnReset" EventName="Click" />
                    </Triggers>
                           
                </asp:UpdatePanel>
            </div> <!--end signup container-->
       
        </div>
        <!--end modaldialog-container-->
        
    
        <asp:ContentPlaceHolder id="bodycontents" runat="server">
        
        </asp:ContentPlaceHolder>
        
          
  </div> <!--end webpage-container-->
    </form>
</body>


Below is the code from Masterpage.Master.cs

protected void btnSubmit_Click(object sender, EventArgs e)
    {

        if (Page.IsValid)
        {

            CreateUserAccount();


            //Close the Dialog Box

            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "UniqueScript", "TestJS();", true)
       

        }


    }

Open in new window

0
Comment
Question by:Rajeshbala
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:Stephan
ID: 33431570
I think the problem lies with the loading event, it doesn't know when to fire it. Maybe use an onload function in order to trigger the function onload... in jQuery it goes like this:

$(document).load(function(){ TestJS(); });

Here is for default javascript:
http://javascript.about.com/library/blonload.htm
0
 

Author Comment

by:Rajeshbala
ID: 33432558
no this doesn't work for me..I am calling this javascript function from code-behind only if it successfully submitted condition..

    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "UniqueScript", "TestJS();", true)


0
 
LVL 16

Expert Comment

by:Stephan
ID: 33432648
Did you try Page instead of ScriptManager?

like so:

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UniqueScript", "TestJS();", true)
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

Author Comment

by:Rajeshbala
ID: 33432984
thanks for your reply stephan..But this doesn't work either..I am wondering is that possible to call javascript from master.cs instead from content page (aspx.cs)?
0
 
LVL 16

Accepted Solution

by:
Stephan earned 1500 total points
ID: 33433005
0
 

Author Comment

by:Rajeshbala
ID: 33436579
Thanks Stephan...I read the article...i fixed the issue..the problem in my case is the button is wrapped inside the update panel..i came to know that if a button is wrapped inside the update panel then the
Page.ClientScript.RegisterClientScriptBlock() won't execute....
  Instead i used the follwing.it worked
 string script = string.Format(@"closeDialog('{0}')", dialogId);
        ScriptManager.RegisterStartupScript(Page, this.GetType(), "UniqueID", script, true);

Reference:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=200
0
 

Author Closing Comment

by:Rajeshbala
ID: 33436582
Fixed the issue
0

Featured Post

Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question