Shahin
Shahin

Reputation: 12841

Call jQuery function from server (In Updatepanel)

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?)

Upvotes: 0

Views: 3165

Answers (2)

Bilal Murtaza
Bilal Murtaza

Reputation: 795

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.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

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();";?

Upvotes: 3

Related Questions