Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Calling a Javascript function from codebehind button click event

Posted on 2010-08-30
14
Medium Priority
?
632 Views
Last Modified: 2012-05-10
Do you know what could be the reason the following call to javascript alert is not working from the codebehind button click event? Here is the code and debugging even takes me to the line of code in the if block but I do not see the alert prompted:
 
protected void btnAddCurrentPart_Click(object sender, EventArgs e)
{
if (!blnCurrentPartCompatible)

{


ClientScriptManager script = Page.ClientScript;

if (!script.IsClientScriptBlockRegistered(this.GetType(), "Alert"))

{

 script.RegisterClientScriptBlock(this.GetType(), "Alert", "<script type=text/javascript>alert('Hello there')</script>");

}


return;

}

}

0
Comment
Question by:skaleem1
[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
  • 6
  • 3
  • 2
  • +2
14 Comments
 
LVL 31

Accepted Solution

by:
MlandaT earned 1000 total points
ID: 33560530
What HTML is being generated?

Also try this:  

script.RegisterClientScriptBlock(this.GetType(), "Alert", "<script type=""text/javascript"">alert('Hello there')</script>");

Also consider:

Page.ClientScript.RegisterStartupScript(this.GetType(), "AlertScript", "alert('Hello World');", True)
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 33560541
I believe this only needs 1 argument, the script key: IsClientScriptBlockRegistered("Alert")
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 33560561
I see it also accepts 2, never mind.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:skaleem1
ID: 33560604
I even changed it to:

ClientScriptManager script = Page.ClientScript;

                if (!script.IsClientScriptBlockRegistered("Alert"))
                {

                    script.RegisterClientScriptBlock(this.GetType(), "Alert", "<script type=text/javascript>alert('Hello there')</script>");

                }

but still does not work. Please see that it is being called from a button click event. Is it an issue?
0
 
LVL 1

Author Comment

by:skaleem1
ID: 33560636
I even tried it on the OnClientClick event but still does not work:

<asp:Button ID="btnAddCurrentPart" runat="server" Text="Add Current Part" Enabled="false" OnClientClick="javascript:CheckCompatibility(); return false;" OnClick="btnAddCurrentPart_Click" />

function CheckCompatibility(CheckCompatibilityForCurrentPartInPortFolio) //
{
                       
            alert(CheckCompatibilityForCurrentPartInPortFolio);

}
0
 
LVL 60

Assisted Solution

by:HainKurt
HainKurt earned 1000 total points
ID: 33560666
put this code to page_onload

ClientScriptManager script = Page.ClientScript;
if (!script.IsClientScriptBlockRegistered(this.GetType(), "Alert")) {
 script.RegisterClientScriptBlock(this.GetType(), "Alert", "<script type=text/javascript>alert('Hello there')</script>");
}
0
 
LVL 1

Author Comment

by:skaleem1
ID: 33560780
HainKurt,

I know that it works when I place this in the Page_onload event, however I have a logic in the button click event and I only want this to fire when the logic is not true, the complete code is as follows:

protected void btnAddCurrentPart_Click(object sender, EventArgs e)
        {
            RetrieveDataGateway rdg = new RetrieveDataGateway();

           
            bool blnCurrentPartCompatible = false;
            blnCurrentPartCompatible = isCurrentPartCompatibleWithPortfolio();
            if (!blnCurrentPartCompatible)
            {
               
                ClientScriptManager script = Page.ClientScript;

                if (!script.IsClientScriptBlockRegistered(this.GetType(), "Alert"))
                {

                    script.RegisterClientScriptBlock(this.GetType(), "Alert", "<script type=text/javascript>alert('Hello there')</script>");

                }

               
                return;
            }
            else
            {
                //Do whatever you want to process
            }
        }
0
 
LVL 31

Expert Comment

by:MlandaT
ID: 33560894
If you put a breakpoint... does that condition [isCurrentPartCompatibleWithPortfolio()] ever evaluate to 'false'... which would make the if work...

It certainly does not look like the problem is coming from the javascript.
0
 
LVL 1

Author Comment

by:skaleem1
ID: 33561189
Yes it does and it even steps into the if block and through the line of code that triggers the javascript without any error...
0
 
LVL 31

Expert Comment

by:MlandaT
ID: 33561314
Strange... does the javascript get emitted to the browser at all? If you View Source for the page... do you see the javascript: alert('Hello there')

Try changing this line:

     script.RegisterClientScriptBlock(this.GetType(), "Alert", "<script type=text/javascript>alert('Hello there')</script>");

TO:

     script.RegisterClientScriptBlock(this.GetType(), "Alert", "alert('Hello there');", true);
0
 
LVL 7

Expert Comment

by:urir10
ID: 33561409
can you also try this:

 ScriptManager.RegisterClientScriptBlock(btnSubmit, typeof(Button), "Saved", "alert('Hello there');", true);
0
 
LVL 60

Expert Comment

by:HainKurt
ID: 33562733
if you do this

response.write("<script type=text/javascript>alert('Hello there')</script>");

what happens ;)
0
 
LVL 1

Author Comment

by:skaleem1
ID: 33567603
Thanks for all your help however the reason was something entirely different which I found out after extensive debugging and reviewing the html view. Long story short, the button btnAddCurrentPart is part of an ajaz based update panel that was causing not to fire the client side script before the postback. What I did was to force a postback in the page load event, something like this:

btnAddCurrentPart.OnClientClick = String.Format("fnClickOK('{0}','{1}','{2}','{3}','{4}')", btnAddCurrentPart.UniqueID, "", "AddCurrentPart", _User, "");

In the fnClickOK function, I do all my client side validation before forcing a postback so that my button click event code behind executes:

 function fnClickOK(sender, e, Flag, UserLogin, IsPrivate)
       {
//Do my client side validation here:
__doPostBack(sender,e);
}

and that solved the problem. The notable thing is how the Update Panel affects the postbacks unless you design the aspx page taking into considerations with all the important options this update panel comes with...

However, because some of you pointed me to the right direction, i.e, checking the html etc., I will split the points between two of you, thanks for your help anyways.
0
 
LVL 1

Author Closing Comment

by:skaleem1
ID: 33567629
Because you pointed me to the right direction, i.e, checking the html etc., I will split the points between two of you, thanks for your help anyways.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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

636 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