-2

So this seems famous, but with little different.

JavaScript Function:

function ShowMessage(Message, Title, isAlarm) {
        $("#dtext").html(Message);
        $("span.ui-dialog-title").text(Title);
        $("#dialog").dialog({
            open: function(e) {
                var Dia = $(e.target);
                if (isAlarm == true) {
                    Dia.parents(".ui-dialog:first").find(".ui-dialog-titlebar").css("background", "red");
                    Dia.parents(".ui-dialog:first").find(".ui-dialog-titlebar").css("color", "White");
                }
                else {
                    Dia.parents(".ui-dialog:first").find(".ui-dialog-titlebar").css("background", "LightSeaGreen");
                    Dia.parents(".ui-dialog:first").find(".ui-dialog-titlebar").css("color", "White");
                }
            },
            show: "blind",
            hide: "clip",
            modal: true,
            resizable: false,
            buttons: {
                "Close": function(e) {
                    $(this).dialog("close");
                    return true;
                }
            }
        });
    }

As you see this method fill with Jquery-Code. if this is just java-script, We can use this Code to call that function but in this case this method don't work well.

C# calling JS Method:

 if (!Pointer.ClientScript.IsStartupScriptRegistered("message"))
        Pointer.Page.ClientScript.RegisterStartupScript
            (Pointer.Master.GetType(), "message", "ShowMessage('messageBox','" + Message + "',false);", true);

I don't Know how to resolve this problem. I Want just call this Js function from server-side?

Edit 1

Thank for your attention. some guy's want from me describe my problem better. Why I can't ? Cuz I don't know what's exactly problem.

I just can say, I test this program with FireBug and set break-point on first line on JS function but in run time when I call that Js function that break-point Hit for a few Millisecond and then page reload goes complete and nothing happened.

I'm newbie on JS and jQuery. So Instead hitting down vote plz try sample program with these Code's and help Me.

thank Again (specially Stefan, PirateKitten, Widor)

Edit 2

I made this Function(JQuery Message Function) to replace Old Function which only use simple JS alert. and I must say old version work(even if I call that from server-side with JS-Caller-Function I write).

in this case, even if I call new Function(JQ Function) with Js in page like :

<button type="button" onclick="ShowMessage('hi','title',false)">
  Display Message
</button>

It's worked, But when call that from server side, Function don't work.

Rev
  • 2,269
  • 8
  • 45
  • 75
  • 2
    could it be that the function is called to early?. Does this change work? `Pointer.Page.ClientScript.RegisterStartupScript(Pointer.Master.GetType(), "message", "$(function() { ShowMessage('messageBox'," + Message + "',false); }", true);` – Manuel van Rijn Oct 18 '11 at 07:45
  • 1
    You should probably add what your exact problem is. JS error? Server error? etc. – Stefan Oct 18 '11 at 08:06
  • You've said "don't work well", what do you actually mean? Is your javascript not rendering out correctly, have you looked to see if you have any JS errors? – Kasaku Oct 18 '11 at 08:23
  • I try this with firebug. Result: that caller method call Js function but nothing happened – Rev Oct 18 '11 at 08:25
  • Have you tried @Manuel's solution? That will delay the execution of the function until the DOM is fully loaded. – Kasaku Oct 18 '11 at 08:28

3 Answers3

2

Put simply; you can't.

JavaScript runs clientside, C# runs serverside.

However, you can generate JavaScript serverside and output it to the client.

See my answer here: Call javascript from vb.net code behind

Community
  • 1
  • 1
Widor
  • 13,003
  • 7
  • 42
  • 64
  • I do this, as sample I use simple alert in function and call that function from code behind with that caller method. – Rev Oct 18 '11 at 08:00
1

Code that is registered with Page.ClientScript.RegisterStartupScript is "rendered" at the end of the page. To be exact in a script tag before the form close tag. However, the code might run before the DOM tree is fully generated.

Since your ShowMessage function access the DOM tree your issue could be related to missing DOM tree elements.

If I do interpret your question correctly there are no JS erros on the page. This chould be as a result of the jquery selector behavior:

$("#dtext").html(Message);

This will set the HTML of all elements with the id dtext. Since there might be no elements with the ID dtext yet, $("#dtext") will return an empty collection and no actions are taken.

To workaround this issue you can try to run your code in the the jquery document ready event. jQuery fires this event if the page was fully loaded:

$(document).ready(function() { ShowMessage('messageBox','" + Message + "', false); }

ServerCode:

 if (!Pointer.ClientScript.IsStartupScriptRegistered("message"))
        Pointer.Page.ClientScript.RegisterStartupScript
            (Pointer.Master.GetType(), "message", "$(document).ready(function() {ShowMessage('messageBox','" + Message + "',false); }", true);
Stefan
  • 14,530
  • 4
  • 55
  • 62
  • reflected the changes in the question. – Stefan Oct 18 '11 at 10:22
  • your solution Didn't work , but +1 for pointing to Dom Tree and Using JQuery Event – Rev Oct 20 '11 at 06:58
  • @Rev: you should try to give a better explanation of your problem. What exactly is not working. "but in this case this method don't work well." is a rather moderate problem description. That's why your answer has three downvotes... – Stefan Oct 20 '11 at 07:04
  • Ok. What happens if replace all the code in the `ShowMessage` function with a simple `alert("foo");`? If this works, then try to add line by line of your existing code. Do so to identify the line that is not working. Try to output the results of your jQuery selctions: `console.log($("span.ui-dialog-title").length);` etc. – Stefan Oct 23 '11 at 13:00
-1

you can call js function from server side like this

Page.ClientScript.RegisterStartupScript(this.GetType(), "show", "ShowMessage()",true);

Hope this would help you

Fahad Hussain
  • 1,165
  • 1
  • 10
  • 17