javascript OnClientClick 在 ASP.NET 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13497916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OnClientClick Not Working In ASP.NET
提问by elucid8
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.
我正在使用 ASP.NET 将值传递给 JavaScript 函数,但由于某种原因我无法确定,当我尝试从另一个控件传递值时它不起作用。相反,它表现得好像有一个语法错误,它只是提交回主表单。
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#
在 .NET 4.0 上运行的 Web 应用程序语言: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.
使用一段时间后,我确定您不能在 ASP 控件中使用 <%%> 标记。此外,如果您正在寻找控制值的动态评估,请避免使用<%=someControl.Text%>
或类似的结构,因为它们仅在请求提交到服务器后才会评估。如果您在运行时需要来自另一个控件的静态值,只需在页面加载事件中设置该值或在后面的代码中以其他方式处理它。
回答by Adil
Javacript will search for variable name = txtToSay.Text
in saySomething function call, Put quotes around it to make it string value
Javacript will search for variable name = txtToSay.Text
在 saySomething 函数调用中,在它周围加上引号以使其成为字符串值
Change
改变
OnClientClick="saySomething(<%=txtToSay.Text%>);"
To
到
OnClientClick="saySomething('<%=txtToSay.Text%>');"
You can get the txtToSay.Text without passing it this way
你可以得到 txtToSay.Text 而不用这种方式传递
<script type="text/javascript">
function saySomething(txt){
alert(document.getElementById('<%=txtToSay.Text%>').value);
};
</script>
回答by Darren Wainwright
you need to put '
around your text in the saySomething()
call.
你需要'
在saySomething()
通话中加入你的文字。
Like this:
像这样:
<asp:Button runat="server" ID="btnSaySomething1" Text="Say Something" OnClientClick="saySomething('<%=txtToSay.Text%>');" />
UPDATE
更新
<%= %>
won't work inside an asp.net control. Can you set it from the code-behind?
<%= %>
不会在 asp.net 控件内工作。您可以从代码隐藏中设置它吗?
I.E
IE
btnSaySomething1.OnClientClick = "Text to say"