Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ScriptManager.RegisterClientScriptBlock not working fom masterpage code behind file

Posted on 2010-08-13
7
Medium Priority
?
2,547 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
  • 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

926 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