5

I am new to Asp.Net and i have my aspx page like this

<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="TestJs.aspx.cs" Inherits="tms.Test.TestJs" %>

<asp:Content ID="Content1" ContentPlaceHolderID="StyleSection" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentSection" runat="server">
    <div class="container">
        <div class="panel">
            <asp:Button ID="btnAlert" OnClick="btnAlert_OnClick" runat="server"/>
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ScriptSection" runat="server">
    <script type="text/javascript">
        function myFunc() {
            $.alert("Hello Mz");
        }
    </script>
</asp:Content>

And my .cs file looks like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace tms.Test
{
    public partial class TestJs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnAlert_OnClick(object sender, EventArgs e)
        {
            ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "newFunc", "myFunc()", true);
        }
    }
}

When I click the button the script does not call up and give some object expected error.

I am really stuck at this. Please help me. Thanx In Advance.

Murtaza Munshi
  • 1,065
  • 4
  • 13
  • 40

2 Answers2

3

In the button click event scriptmanager can be called

protected void btn_click(object sender, EventArgs e){
ScriptManager.RegisterStartupScript(this,this.GetType(),"Your Comment","myFunc();", true);}

Change your script like below :

<script type="text/javascript">
    function myFunc() {
        alert("Hello Mz");
    }
</script>
Arumoy Roy
  • 108
  • 12
Rakesh Sajja
  • 148
  • 9
  • May i ask where do i put this code. On the button click event or Page_Load event. – Murtaza Munshi Jun 02 '15 at 05:46
  • It should work perfectly. You have to put the code in your button click event, not page load. – Rakesh Sajja Jun 02 '15 at 05:49
  • Replace $.alert("Hello Mz"); with alert("Hello Mz"); – Arumoy Roy Jun 02 '15 at 05:55
  • var script = String.Format(@"alert('Hello Mz');"); ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "mzFunc", script, true); - I tried this and it worked – Murtaza Munshi Jun 02 '15 at 06:01
  • are you sure your myFunc() is rendering in the page? Because it is under different content place holder. Try to put your block inside ContentSection place holder. – Rakesh Sajja Jun 02 '15 at 06:04
  • Can you ensure that in master page "ScriptSection" is below the "ContentSection". I am just guessing if somehow that javascript function is not properly loaded. Also just for checking keep both button section and javascript in the same contentplaceholder and check if it works – Arumoy Roy Jun 02 '15 at 06:28
1

First of all, I don't understand why you are using the ScriptManager here. An ASP Button has a property named onClientClick. See this MSDN link for more information.

You can change your button's code in the HTML like:

<asp:Button ID="btnAlert" OnClick="btnAlert_OnClick" onClientClick="myFunc();" runat="server"/>

Note: The onClientClick will be executed before the onClick event. This is written out of my head and untested!

Complexity
  • 5,682
  • 6
  • 41
  • 84
  • Yes you are right. But actually i want to do some C# code behind and then i want to call js. But it wasnt working so i created a testpage like this and asked you to guide me. – Murtaza Munshi Jun 02 '15 at 05:41