Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i was trying to call a javascript function from a back code
this is my javascript function
C#
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script type="text/javascript">
        function myFunction() 
			{
                alert("hello");
			}
</script>



<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<asp:Button ID="btnSearch" Text="Go" runat="server" OnClick="btnSearch_Click" />



i tried calling the function like this on back code but not working.....
C#
protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "myFunction()", true);
        }
Posted
Updated 31-Jul-14 22:32pm
v3

I tried your example with my testing application and it's working properly as you can also try the code given by me:

XML
<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">

    <script type="text/javascript">
        function GetMSG() {
            alert("hello");
        }
    </script>

</asp:Content>
<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="ContentPlaceHolder2">
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</asp:Content>


And write a code for invoke function on button click even on code behind page:

C#
protected void Button1_Click(object sender, EventArgs e)
       {
           ScriptManager.RegisterStartupScript(Page, Page.GetType(), "GetMSG", "GetMSG();", true);
           //this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "GetMSG()", true);
       }


please try this example. I hope its helping to you.....Thank You......:-)
 
Share this answer
 
Comments
breab 1-Aug-14 4:48am    
your works just fine
this is inside the asp:content
<input type="text" id="hidden" runat="server" style="display:none" />

and my real function is



function myFunction() {
var fname = document.getElementById("hidden").value;
alert(fname);
alert("djd");
var mapOptions =
{
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var infowindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById('displayMap'), mapOptions);
alert("ff");
//get Location info


$.ajax({
type: "GET",
url: "Location\\" + fname + "",
dataType: "xml",
success: function (xml) {
$(xml).find("Location").each(function () {
//alert("dd");

Loc = $(this).find("Loc_Name").text();
Lat = $(this).find("Latitude").text();
Long = $(this).find("Longitude").text();
var loc = new google.maps.LatLng(Lat, Long);
//alert(Lat);
//center teh incident
map.setCenter(loc);

var marker = new google.maps.Marker({
position: new google.maps.LatLng(Lat, Long),
map: map,
title: 'My workplace',
icon: 'http://google-maps-icons.googlecode.com/files/factory.png'

});


google.maps.event.addListener(marker, 'click', function () {

infowindow.setContent(Loc);
infowindow.open(map, marker);

});



});
}

});
}
First of all I would suggest you to write your JavaScript in .js file and add reference to your aspx page.
HTML
<script src="../JS/MyJavascript.js" type="text/javascript"></script>

Then try calling your JavaScript function like this:
C#
ScriptManager.RegisterClientScriptBlock(base.Page, this.GetType(), "MyScript", "myFunction();", true);


--Amy
 
Share this answer
 
v2
If you want to call it when the button is clicked you can do so on the client side, befreo postback, by adding in page_load
btnSearch.Attributes.Add("onlcick", "myFunction()")

You can even then prevent the postback by returning false form your JavaScript function.
However, if you really want to call it from the click event after postback in code:
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "myFunction()", True)
 
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