如何访问JS变量在C# [英] How to access JS variable in C#
本文介绍了如何访问JS变量在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我们说我有
< DIV ID =嗒嗒> 500℃; / DIV>
(背景:asp.net aspx页面)
我如何让C#code访问该值?
<%
INT numberiwant = ###; // 500理想
%GT;
我正沿着jQuery的的线条思考的东西
<脚本类型=文/ JavaScript的>
VAR值= $('#胡说')HTML()。
//然后做一些魔法,并传递到C#?
< / SCRIPT>
有没有更好的办法?
解决方案
使用一个隐藏的输入框
<输入ID =hiddenControl类型=隐藏=服务器/>
用JavaScript把你的价值在输入框中
//调用这个方法,你回传之前,也许在表单提交
功能SetPostbackValues(){
VAR hiddenControl ='<%= hiddenControl.ClientID%GT;';
的document.getElementById(hiddenControl).value的= 500;
//或其他一些code让你的价值
}
访问与服务器端的输入框中的值
保护无效btnSubmit_Click(对象发件人,EventArgs的发送){
VAR hiddenValue = hiddenControl.Value;
}
Let's say I have
<div id="blah">500</div>
(Context: asp.net aspx page) How do I allow a c# code access that value?
<%
int numberiwant = ###; // Ideally 500
%>
I was thinking of something along the lines of jQuery:
<script type="text/javascript">
var value = $('#blah').html();
// Then do some magic and pass to c#?
</script>
Is there a better way?
解决方案
Use a hidden input box
<input id="hiddenControl" type="hidden" runat="server" />
Put your value in the input box with javascript
//call this method before you postback, maybe on form submit
function SetPostbackValues() {
var hiddenControl = '<%= hiddenControl.ClientID %>';
document.getElementById(hiddenControl).value = 500;
//or some other code to get your value
}
Access the value with the input box on the server side
protected void btnSubmit_Click(object sender, EventArgs e) {
var hiddenValue = hiddenControl.Value;
}
这篇关于如何访问JS变量在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文