Solved

ScriptManager.RegisterClientScriptBlock not working fom masterpage code behind file

Posted on 2010-08-13
7
2,229 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
Comment Utility
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
Comment Utility
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
Comment Utility
Did you try Page instead of ScriptManager?

like so:

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UniqueScript", "TestJS();", true)
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Rajeshbala
Comment Utility
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
Comment Utility
0
 

Author Comment

by:Rajeshbala
Comment Utility
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
Comment Utility
Fixed the issue
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…
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…

744 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

16 Experts available now in Live!

Get 1:1 Help Now