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

5
  • 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); Commented Oct 18, 2011 at 7:45
  • 1
    You should probably add what your exact problem is. JS error? Server error? etc. Commented Oct 18, 2011 at 8: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? Commented Oct 18, 2011 at 8:23
  • I try this with firebug. Result: that caller method call Js function but nothing happened Commented Oct 18, 2011 at 8:25
  • Have you tried @Manuel's solution? That will delay the execution of the function until the DOM is fully loaded. Commented Oct 18, 2011 at 8:28

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

I do this, as sample I use simple alert in function and call that function from code behind with that caller method.
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);

4 Comments

reflected the changes in the question.
your solution Didn't work , but +1 for pointing to Dom Tree and Using JQuery Event
@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...
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.
-1

you can call js function from server side like this

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

Hope this would help you

1 Comment

plz look at my question this isn't solution

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.