Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,
I am using some scripts in my .aspx pages. It was working before I put update panel in these pages. I need both things, update panel and scripts. How to enable these scripts inside the update panel in asp.net? I am using c# and asp.net 3.5 framework.
The scripts that I used in the pages are:

1.
C#
Response.Write("<Script>alert('Successfully Updated')</Script>");


2.
C#
string url = "./Quick.aspx";
   string cmd = "window.open('" + url + "', '_blank', 'height=600,width=600,status=yes,toolbar=no,menubar=no,location=yes,scrollbars=yes,resizable=no,titlebar=no' );";
ScriptManager.RegisterStartupScript(this, this.GetType(), "newWindow", cmd, true);
   }



These codes are used in the .aspx.cs pages.

Any idea about this issue to solve?

Thanks....
Posted
Updated 5-Nov-12 10:20am
v2
Comments
ZurdoDev 5-Nov-12 8:49am    
Try putting the scripts into JS files and then add references to the JS files. Then use regular HTML events to call the JS. You're using C# which is server side code and modifying the Response object which does not always work well with the UpdatePanel.
willempipi 5-Nov-12 8:51am    
Why don't you use a Literal control inside your updatepanel where you put your <script></script>?
n.podbielski 5-Nov-12 13:46pm    
It is not problem with update panel. I suspect that it is security issue. Browsers never execute scripts inserted by ajax. It would be dangerous since you could do anything with the page in script.
njammy 5-Nov-12 10:40am    
Neha, from where you are calling these scripts?

C#
// Is a simply way..
//Common.Response.Write("&lt;Script>alert('Successfully Updated')&lt;/Script>");

        string url = "Quick.aspx";
        string cmd = "alert('Successfully Updated');window.open('" + url + "', '_blank', 'height=600,width=600,status=yes,toolbar=no,menubar=no,location=yes,scrollbars=yes,resizable=no,titlebar=no' );";
        ScriptManager.RegisterStartupScript(this, this.GetType(), "newWindow", cmd, true);
 
Share this answer
 
Comments
Vyas Kandarp 14-May-14 15:52pm    
Works for me Thanx..
mosayeb2 7-Jun-15 15:59pm    
خیلی عالی بووووود.ممنونم ازتون
very gooooooooooooooooood
tanx
try this
ScriptManager.RegisterStartupScript(this,this.GetType(),"name", "alert('Successfully Updated');", true);

select as answer if it works for u.
 
Share this answer
 
v2
Comments
Awadhesh Kumar Singh 17-Feb-15 1:51am    
Worked perfectly.. Tested OK!!
Basically scripts inserted with UpdatePanel are never executed. They are just sitting there and doing nothing. You have to execute them after inserting into page.

I created class for my current project:
C#
public static class UpdatePanelMethods
{

    public static void AddUpdateScript(this UpdatePanel panel, string key, string script)
    {
        if (!panel.Page.ClientScript.IsClientScriptBlockRegistered(key))
        {
            ScriptManager.RegisterClientScriptBlock(
                panel, typeof(UpdatePanel), key, script, true);
        }
    }
}

You are calling this like:
upSkorowidz.AddUpdateScript("upScript", "foo(bar)");

It's not ideal. It's string, so it better be really short and simple.
If you have to execute something much more complicated insert it like regular script but enclose this in some function:
HTML
<script>
foo(bar){
[whole script]
}
</script>

That way you can complex things in that script and you got intellisense.
What is downside?
You have to call this method (AddUpdateScript) in .cs code. JS scritp have to be more complicated since you modify this to simple call in string in .cs file.

If this reaaaally not the case for you, you can also use img.error event. It is possibly one of most ugly thing I wrote in my career :(
HTML
<%if (Page.IsPostBack)
  {//just for loading scripts %>
<script type="text/javascript" id="someScript">
[some js]
</script>
<img src="dummyImg.jpg" onerror="eval(jQuery('#someScript').text())" alt"" width="0px" height="0px" />
<%} %>

What is happening?
There is not image dummyImg.jpg in server. So browser calls onerror event which executes our script. It is one of 2 events that can be executed by html injected in HTML after AJAX.
Second is frame.onload, but I did not want a frame in my html and did not tested it.
I read that also object.onload should work but I have found that untrue.
But it is very dirty solution and I would advice against it unlesss you want to end in DailyWTF :)
 
Share this answer
 
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "_RefreshPage", "javascript:refreshPage();", true);
 
Share this answer
 
Try this

ScriptManager.RegisterStartupScript(this, this.GetType(), "popupScript", "javascript:showalert();", true);


in the design view write following

XML
<script type="text/javascript">
      function showalert() {
         //javascript code to display alert
      }
</script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900