简体   繁体   English

从JS代码获取返回值到C#

[英]Get return value from JS code to C#

I want to get the answer of a window.prompt() alert box through the C# Code Behind file. 我想通过C#代码隐藏文件获取window.prompt()警报框的答案。 It's just a one-liner of JavaScript code, so I thought I could even execute it from the Code Behind file. 它只是JavaScript代码的一排,所以我想我什至可以从Code Behind文件中执行它。 It doesn't really matter to me whether it's a <script> tag in the .aspx file or executed through the .aspx.cs file. 对我来说,它实际上是.aspx文件中的<script>标记还是通过.aspx.cs文件执行,对我而言.aspx.cs

I thought of having the script executed (called from the C# part) and then having the return value assigned to a certain not visible field, but is there any better way to do it? 我想到要执行脚本(从C#部分调用),然后将返回值分配给某个不可见的字段,但是还有更好的方法吗?

The obvious way would probably go something like this: 最明显的方法可能是这样的:

.aspx file:
<script>
function foo() {
    document.getElementById('MyFieldID').value = window.prompt('Answer this question:', '');
}
</script>

.aspx.cs file:
////////////////////////////////////////////////
//MyFieldID.Text now contains whatever I want//
//////////////////////////////////////////////

What do you say? 你说什么? Is there any better way? 有什么更好的办法吗?

Better way is always opinion based. 更好的方法总是基于意见。 What I'd say is you have a few options, all depend on what you're doing. 我要说的是,您有几种选择,所有选择都取决于您的工作。 HTTP and ASP.NET provide us a few means of sending data to the server, there are 3 main ones before HTML5: HTTP和ASP.NET为我们提供了一些将数据发送到服务器的方法,HTML5之前有3种主要方法:

  1. Query string 请求参数
  2. Form values 表单值
  3. AJAX calls AJAX通话

If you're redirecting the user to a new page after they answer the prompt, you can just send them to yournewurl.aspx?promptAnswer=*whatever* 如果您在用户回答提示后将用户重定向到新页面,则可以将其发送到yournewurl.aspx?promptAnswer=*whatever*

If you're doing a postback, then you can use a form value (this looks like what you're doing in your example). 如果要进行回发,则可以使用表单值(看起来像您在示例中所做的一样)。 You can put an <asp:HiddenField> on the page and populate it from JavaScript before submitting the form. 您可以在页面上放置<asp:HiddenField>并通过JavaScript填充它,然后再提交表单。

If you need just the prompt response, but are not attempting to reload the page, you can make an AJAX call that sends the variable to the server (this still uses #1 or #2 to send the data, it just does it without reloading the page). 如果您只需要即时响应,而又不尝试重新加载页面,则可以进行AJAX调用,将变量发送到服务器(这仍然使用#1或#2来发送数据,它只是在不重新加载的情况下进行发送)这一页)。

Which of those three options works best depends on your implementation. 这三个选项中哪一个最有效取决于您的实现。 However, your solution should work just fine. 但是,您的解决方案应该可以正常工作。 However, since the control you'd likely be using to stuff the value into is a HiddenField it would be in MyFieldID.Value not MyFieldID.Text . 但是,由于您可能将值填充到其中的HiddenFieldHiddenField因此它将位于MyFieldID.Value而不是MyFieldID.Text The only other thing you have to deal with is if your MyFieldID is nested in some other controls (like a ContentPlaceHolder ) such that the ClientID has naming containers pretended to it so it's really something like ContentPlanceHolder1_MyFieldID when accessed from JavaScript. 您需要处理的唯一另一件事是,如果MyFieldID嵌套在某些其他控件(例如ContentPlaceHolder )中,以使ClientID假装了命名容器,因此从JavaScript访问时,它的确类似于ContentPlanceHolder1_MyFieldID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM