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.

6
  • Check this out stackoverflow.com/questions/30587817/… Commented Jun 2, 2015 at 5:33
  • Try this ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "newFunc", "myFunc()", false); Make last argumanet false. Commented Jun 2, 2015 at 5:34
  • @MairajAhmad: Yes let me check it and get back to you. Commented Jun 2, 2015 at 5:35
  • @MairajAhmad: It worked but it doesn't show alert. "myFunc()" gets printed on top of my page. Commented Jun 2, 2015 at 5:37
  • Replace $.alert("Hello Mz"); with alert("Hello Mz"); Commented Jun 2, 2015 at 5:38

3 Answers 3

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>
Sign up to request clarification or add additional context in comments.

6 Comments

May i ask where do i put this code. On the button click event or Page_Load event.
It should work perfectly. You have to put the code in your button click event, not page load.
Replace $.alert("Hello Mz"); with alert("Hello Mz");
var script = String.Format(@"alert('Hello Mz');"); ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "mzFunc", script, true); - I tried this and it worked
are you sure your myFunc() is rendering in the page? Because it is under different content place holder. Try to put your <script></script> block inside ContentSection place holder.
|
2

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!

1 Comment

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.
0

Download the files sweetalert-dev.js, sweetalert.css, and sweetalert.min.js. Add these files to your project. Refer to these files in <head> tag and put this function in <head> tag.

Then, in your C# button click handler, use the following:

ScriptManager.RegisterStartupScript(this, this.GetType(), "Your Comment", "JSalert();", true); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.