Solved

ScriptManager.RegisterClientScriptBlock not working fom masterpage code behind file

Posted on 2010-08-13
7
2,270 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

912 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now