javascript - OnClientClick event in In ASP.NET - Stack Overflow

admin2025-03-20  4

I'm using ASP.NET to pass a value to a JavaScript function and, for some reason I haven't been able to determine, it isn't working when I try to pass in a value from another control. Instead, it acts like there is a syntax error and it just submits back to the main form.

Does anyone know why?

Example:

<asp:TextBox ID="txtToSay" runat="server" Text="Something"></asp:TextBox>

<asp:Button runat="server" ID="btnSaySomething1" Text="Say Something"
OnClientClick="saySomething(<%=txtToSay.Text%>);"  /> <!-- doesn't work -->

<asp:Button runat="server" ID="btnSaySomething1" Text="Say Something"
OnClientClick="saySomething('<%=txtToSay.Text%>');"  /> <!-- doesn't work -->

<asp:Button runat="server" ID="btnSaySomething2" Text="Say Something"
OnClientClick="saySomething('Something');"  /> <!-- works -->

<script type="text/javascript">
    function saySomething(txt){
        alert(txt);
    };
</script>

Additional Information:

Web Application running on .NET 4.0 Language: C#

Update:

After working with this a while, I've determined that you can't use <%%> tags in ASP controls. Additionally, if you're looking for dynamic evaluation of control values AVOID AVOID AVOID using <%=someControl.Text%> or similar constructs since they are only evaluated once a request is submitted to the server. If you need a static value from another control at runtime, simply set that value in the page load event or handle it another way in the code behind.

I'm using ASP.NET to pass a value to a JavaScript function and, for some reason I haven't been able to determine, it isn't working when I try to pass in a value from another control. Instead, it acts like there is a syntax error and it just submits back to the main form.

Does anyone know why?

Example:

<asp:TextBox ID="txtToSay" runat="server" Text="Something"></asp:TextBox>

<asp:Button runat="server" ID="btnSaySomething1" Text="Say Something"
OnClientClick="saySomething(<%=txtToSay.Text%>);"  /> <!-- doesn't work -->

<asp:Button runat="server" ID="btnSaySomething1" Text="Say Something"
OnClientClick="saySomething('<%=txtToSay.Text%>');"  /> <!-- doesn't work -->

<asp:Button runat="server" ID="btnSaySomething2" Text="Say Something"
OnClientClick="saySomething('Something');"  /> <!-- works -->

<script type="text/javascript">
    function saySomething(txt){
        alert(txt);
    };
</script>

Additional Information:

Web Application running on .NET 4.0 Language: C#

Update:

After working with this a while, I've determined that you can't use <%%> tags in ASP controls. Additionally, if you're looking for dynamic evaluation of control values AVOID AVOID AVOID using <%=someControl.Text%> or similar constructs since they are only evaluated once a request is submitted to the server. If you need a static value from another control at runtime, simply set that value in the page load event or handle it another way in the code behind.

Share Improve this question edited Mar 22, 2021 at 22:29 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 21, 2012 at 16:41 elucid8elucid8 1,4224 gold badges20 silver badges40 bronze badges 2
  • 1 View the page source, it will show you the problem! – epascarello Commented Nov 21, 2012 at 16:44
  • See my updated answer - it's because you're trying to use inline code in a server control... – Darren Wainwright Commented Nov 21, 2012 at 17:17
Add a ment  | 

2 Answers 2

Reset to default 2

Javacript will search for variable name = txtToSay.Text in saySomething function call, Put quotes around it to make it string value

Change

OnClientClick="saySomething(<%=txtToSay.Text%>);" 

To

OnClientClick="saySomething('<%=txtToSay.Text%>');" 

You can get the txtToSay.Text without passing it this way

<script type="text/javascript">
    function saySomething(txt){
        alert(document.getElementById('<%=txtToSay.Text%>').value);
    };
</script>

you need to put ' around your text in the saySomething() call.

Like this:

    <asp:Button runat="server" ID="btnSaySomething1" Text="Say Something" OnClientClick="saySomething('<%=txtToSay.Text%>');"  />

UPDATE

<%= %> won't work inside an asp control. Can you set it from the code-behind?

I.E

btnSaySomething1.OnClientClick = "Text to say"
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1742415795a212653.html

最新回复(0)