0

I want to call jQuery function from server.
The server side button that calls function is inside an UpdatePanel.
this is server side function :

//Button Click handler (It is inside Updatepanel)
string ScriptName = null;
ScriptName = "function pageLoad(sender, args) { if (args.get_isPartialLoad()) {DisplayMessage(); }}";
ScriptManager.RegisterStartupScript(this, GetType(), "DisplayMessage", ScriptName, true);

and this is client side function :

 <script type="text/javascript">
        function DisplayMessage() {
            //Alert("Called");

        }
</script>

It doesn't work.
What's wrong ? (Is my question and problem clear?)

3
  • 1
    you call "DisplayMEssage", notice the capital E? Commented Jun 29, 2011 at 13:17
  • DisplayMEssage is the name of script not the name of Javascript function Commented Jun 29, 2011 at 13:19
  • I updated the code. no difference. Commented Jun 29, 2011 at 13:20

2 Answers 2

3

That's weird, as the following works perfectly fine for me:

<%@ Page Language="C#" %>
<script type="text/c#" runat="server">
    protected void BtnTrigger_Click(object sender, EventArgs e)
    {
        string script = "function pageLoad(sender, args) { if (args.get_isPartialLoad()) {DisplayMessage(); }}";
        ScriptManager.RegisterStartupScript(
            this, 
            GetType(), 
            "DisplayMEssage", 
            script, 
            true
        );
    }
</script>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript">
        function DisplayMessage() {
            alert('called');
        }
    </script>
</head>
<body>
    <form id="Form1" runat="server">
        <asp:ScriptManager ID="scm" runat="server" />

        <asp:LinkButton 
            ID="BtnTrigger" 
            runat="server" 
            Text="Trigger" 
            OnClick="BtnTrigger_Click" 
        />

        <asp:UpdatePanel ID="up" runat="server">
            <Triggers>
                <asp:AsyncPostBackTrigger 
                    ControlID="BtnTrigger" 
                    EventName="Click" 
                />
            </Triggers>
        </asp:UpdatePanel>
    </form>
</body>
</html>

Also do you really need all the complex script, why not simply use string script = "DisplayMessage();";?

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

3 Comments

Would you please put BtnTrigger into updatepanel ? In my case button that calls jquery function is into updatepanel. and the property of updatepanel childrenastrigger is setts true
Thank you. In my condition how can I find the problem ? Any way ?
@shaahin, start by eliminating parts of your code until you narrow it down to a simple example as mine which works. Hopefully by the time you get to a working example by elimination you would have found the offending code. Unfortunately I don't see how I can help you any more as I don't dispose with your code. I just showed you a fully working implementation.
0

create a display:none div in the page. Put it in update panel. This will not disturb your design. to execute every sclient side script in page

<div id="scriptContainer" runat="server" style="display:none;"></div>

then paste your js in it on an event.

scriptContainer.innerHTML = "<script type=text/javascript>DisplayMessage();</script>";

hope it helps.

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.