Geeks With Blogs

@paulschapman
  • paulschapman @BBCClick Just complained to the BBC about every showing of Click being cancelled for wall to wall olympics! about 730 days ago
  • paulschapman iPlayer says watch Click live - and you get Olympics - 24 channels of sport not enough :( about 730 days ago
  • paulschapman I think the cat just had a heart attack - the Red Arrows flew over! about 732 days ago
  • paulschapman Officially nothing worth watching on syfy now they have gone and cancelled Sanctuary. about 798 days ago

Paul Chapman .Net Musings

I needed to get a pop-up window to come up in ASP.Net. Now while JavaScript happily does this (with the alert(); function ASP.Net does not. So I needed to work out how to do it. Another wrinkle in this comes from Ajax. None of the JavaScript code seemed to work when placed in an AJAX page.

As I have now been tasked with changing this code and moving from the Anthem Ajax library to a pure MS solution it was time for this died in the wool VB/C# developer to get his head around getting these scripts working.

To do this.

  • Create a simple ASP.NET Web Project
  • Right click with the mouse on the Project title (this will be bold)
  • Select ‘Add’ and then ‘New Item’

image

  • Select ‘AJAX Web Form’
  • Add a Button to the page
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="TrustTest.TestPage" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script type="text/javascript">
    
      function pageLoad() {
      }
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Scripts>
                
            </Scripts>
        </asp:ScriptManager>
    </div>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Button ID="Button1" runat="server" Text="Press Me!" 
                onclick="Button1_Click" />
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>
  • Now add a 'click' event handler to the button
        protected void Button1_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "alertScript", "alert('you pressed me!');", true);
 
        }
 
  • And there it is. Now when ever you click on the button a pop up appears saying ‘you pressed me!’
 
image
Posted on Sunday, September 27, 2009 11:55 AM | Back to top


Comments on this post: Running a client side script from server side code in ASP.Net

No comments posted yet.
Your comment:
 (will show your gravatar)
 


Copyright © paulschapman | Powered by: GeeksWithBlogs.net | Join free