How to pass an interpreted parameter on the server to a javascript function?

-1

I am creating a control ascx that will have multiple impressions on a same page.

In this control, there is an image that I want to execute a javascript function when it is clicked. For example, like this:

<asp:Panel ID="divPai" runat="server" >
    <div>
        <div ...>
            <input .../>
        </div>
        <asp:Image ID="btnMensagem" 
                   Class="animated-transition rotate-90-degrees" 
                   ImageUrl="../../Images/bt_proximo.png" 
                   onclick="mostrarAlerta2();" 
                   runat="server" 
                   Style="padding:10px;"/>
    </div>
    <div class="div-manipulavel" ...>
        <input ... />
    </div>
</asp:Panel>

In a separate js file file I have all the functions I need to use in this control and the inclusion of this script is occurring normally.

I made the script import like this:

Page.ClientScript.RegisterClientScriptInclude(GetType(), "scriptsMeuControle", ResolveClientUrl("~/Componentes/Scripts/MeuControle.js"));

This would be the content file:

function mostrarAlerta(mensagem)
{
    alert('Conteúdo: ' + mensagem);
}

function mostrarAlerta2()
{
    alert('Função acionada!');
}

The problem happens when I need to pass a parameter to execute in the function, as is the case of mostrarAlerta(mensagem) .

I tried this way:

<asp:Image ID="btnMensagem"
           ...
           onclick="mostrarAlerta2('<% =divPai.ClientID %>');"/>

This does not work. The expression <% =divPai.ClientID %> is not interpreted and the content that I tried to pass is displayed literally:

  

Conteúdo: <% =divPai.ClientID %>

How do I pass a parameter that is interpreted server-side like this to a javascript function?

    
asked by anonymous 30.05.2018 / 22:04

1 answer

0

I found in this SO post answer to my question.

In summary,

  

Can not use expressions of type <% %> in controls that run on the server side.

The solution I chose was to roll back the <asp:Image .../> component to the corresponding native - <img /> - and the call was made correctly:

<asp:Panel ID="divPai" runat="server" >
    <div>
        <div ...>
            <input .../>
        </div>
        <img id="<% =divPai.ClientID %>_btnMensagem" 
             class="animated-transition rotate-90-degrees" 
             src="../../Images/bt_proximo.png" 
             onclick="mostrarAlerta2('<% =divPai.ClientID %>');" 
             style="padding:10px;"/>
    </div>
    <div class="div-manipulavel" ...>
        <input ... />
    </div>
</asp:Panel>

Tips that have been given by colleagues @ dvd and @ bfavaretto helped me complete the implementation of the javascript function in my real scenario .

    
10.06.2018 / 19:01