0

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

  • Why use $ ? <%$ , must be <%= – Mate Jan 11 '18 at 05:01
  • Yes bro, I tried all the types of tags but the resultant is same – thebuddingdeveloper Jan 11 '18 at 05:02
  • What is the file extension of your page? – Schalk Jan 11 '18 at 05:04
  • @Schalk.Netgen its `.aspx` – thebuddingdeveloper Jan 11 '18 at 05:05
  • Server-side code appearing commented out normally means that it's not getting processed - and the reason for this is normally using the wrong file extension. My speciality is ASP.NET/Razor so I wish I could help more, but I'd suggest looking for the issue somewhere else than the embedded code. Can you post a screenshot of your inspected page with the code in question highlighted? – Schalk Jan 11 '18 at 05:07
  • @Schalk.Netgen yes bro, as you requested – thebuddingdeveloper Jan 11 '18 at 05:15
  • Possible duplicate of [Why will <%= %> expressions as property values on a server-controls lead to a compile errors?](https://stackoverflow.com/questions/370201/why-will-expressions-as-property-values-on-a-server-controls-lead-to-a-co) – Jon P Jan 11 '18 at 05:41

1 Answers1

1

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>
Mate
  • 4,976
  • 2
  • 32
  • 38