Solved

ScriptManager.RegisterClientScriptBlock not working fom masterpage code behind file

Posted on 2010-08-13
7
2,298 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

775 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