thebuddingdeveloper
thebuddingdeveloper

Reputation: 65

Prevent Embedded ASP Code from being escaped

This one looks similar to the question that is asked here but the scenario asked by the OP looks different as his accepted answer do does not meet my requirement.In my case, I am trying to embed an asp snippet as parameter of a Javascript method called from an onclick of button , but the result is that the tags gets escaped when compiled. The ASP code is :

<asp:Button runat="server" ID="Calculate_Mean" Text="Calculate Mean" 
OnClientClick="mean('<%$TextBoxA.ClientID + ',' + TextBoxB.ClientID + ',' +
TextBoxC.ClientID%>')"

The Outcome I got is:

<input type="submit" name="Calculate_Mean" 
value="Calculate Mean"
onclick="mean(&#39;&lt;%=TextBoxA.ClientID%>&#39;);" id="Calculate_Mean" />

I've referred and tried the following pages but still couldn't solve the issue:

Passing ASP.net control to JS Function
Different Embedded ASP Code Blocks and its uses

EDIT: Here is a screenshot of the rendered page: Screenshot of Page rendered

Upvotes: 0

Views: 83

Answers (1)

Mate
Mate

Reputation: 5284

Maybe you can change asp:button for a html button:

<button  onclick="mean( document.getElementById('<%=TextBoxA.ClientID%>'));" ></button>

Or try getting client ids inside js function:

ASPX

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function mean() {
            var a = document.getElementById('<%=TextBoxA.ClientID%>');
            var b = document.getElementById('<%=TextBoxB.ClientID%>');
            var c = document.getElementById('<%=TextBoxC.ClientID%>');
            alert(a.value);
            alert(b.value);
            alert(c.value);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBoxA" runat="server" ></asp:TextBox>
                <asp:TextBox ID="TextBoxB" runat="server" ></asp:TextBox>
                <asp:TextBox ID="TextBoxC" runat="server"  ></asp:TextBox>
    <asp:Button runat="server" ID="Calculate_Mean" Text="Calculate Mean" 
        OnClientClick="mean();" />
    </div>
    </form>
</body>
</html>

Upvotes: 1

Related Questions