简体   繁体   English

获取在document.ready中设置的隐藏字段的值,该值位于asp.net代码后面

[英]Grab hidden field's value that is set in document.ready in asp.net code behind

I need to access a hidden fields value in code behind, normally this is easy but this variable is being set during jQuery's document.ready event and I need to access it when the page loads... so far I have tried Page_Load , Page_LoadComplete , Init and Prerender with no luck. 我需要在后面的代码中访问一个隐藏字段值,通常这很容易,但是此变量是在jQuery的document.ready事件期间设置的,因此我需要在页面加载时访问它……到目前为止,我已经尝试了Page_Load,Page_LoadComplete,没有运气的Init和Prerender。 Is this just an impossible task? 这是不可能完成的任务吗?

Thanks! 谢谢!

Yes that is impossible. 是的,那是不可能的。

(At least with known technology at this point. You would need a time machine to get that data.) (目前至少使用已知技术。您将需要一台时间机器来获取该数据。)

All the code on the server runs to create the page to the browser. 服务器上的所有代码都会运行以创建浏览器页面。 when the server code is complete, the page is sent to the browser and the browser parses the page. 服务器代码完成后,会将页面发送到浏览器,然后浏览器将解析该页面。 When the entire page is parsed, the ready event happens. 解析整个页面后,将发生ready事件。

So, all server code runs before the page is sent to the browser, and all Javascript code runs after the page is sent to the browser. 因此,所有服务器代码在页面发送到浏览器之前运行,而所有Javascript代码在页面发送到浏览器之后运行。

(It's technically possible to have Javascript start in the browser before the server code completes, but that means that you need to turn buffering off for the response on the server and handle the response completely by yourself (ie not Webforms or MVC), and the Javascript needs to run when the page starts loading, not in the ready event. Also, you need another page on the server that the Javascript can request to send data back to the server, and that page has to run sessionless so that the web server can handle it in parallel with the current page. Also, you need to set up some static class on the server where the data can be stored, so that the current page can pick it up when it arrives, and you have to create some kind of identifier for the request that the Javascript can send back to identify which request should get the data, as the page used to send the data is sessionless. So, even if it possible to do something similar to what you want, it's way too complicated to be practic (从技术上讲,在服务器代码完成之前,可以在浏览器中启动Javascript,但这意味着您需要为服务器上的响应关闭缓冲,并完全由您自己处理响应(即,不是Webforms或MVC),并且需要在页面开始加载时运行Javascript,而不是在ready事件中运行,此外,您还需要服务器上的另一个页面,Javascript可以请求该页面将数据发送回服务器,并且该页面必须无会话运行,以便Web服务器可以与当前页面并行处理它。此外,您需要在可以存储数据的服务器上设置一些静态类,以便当前页面可以在到达时对其进行提取,并且您必须创建某种可以发送给请求的标识符的标识符,以标识哪个请求应该获取数据,因为用于发送数据的页面是无会话的,因此,即使可以执行与所需操作类似的操作,也太复杂了务实 al.) 等)

So the poster above is correct, but there are lots of ways to do what you want. 因此,上面的海报是正确的,但是有很多方法可以执行您想要的操作。 For example, submit a post back right after changing the value. 例如,更改值后立即提交回发信息。 Like below (there are many other alternatives to this as well). 像下面一样(还有很多其他选择)。

public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var test = HiddenField1.Value;

            if (test != "")
                HiddenField1.Value = "test finished";
        }
    }

<form id="form1" runat="server">

        <asp:HiddenField ID="HiddenField1" runat="server" />

        <script type="text/javascript">
            $(document).ready(function () {


                if ($('#HiddenField1').val() !== 'test finished') {
                    $('#HiddenField1').val('test');
                    $("#form1").submit();
                }

            });
        </script>

    </form>

暂无
暂无

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

相关问题 如何在javascript中设置asp.net隐藏字段并访问c#代码后面的值 - how to set asp.net hidden field in javascript and access the value in c# code behind 在document.ready中设置隐藏字段的值,并在pageload事件中获取其值 - set hidden field value in document.ready and get its value in pageload event 从后面的代码更新时,asp.net隐藏字段不保留值 - asp.net hidden field not retaining value when updated from code behind document.getElementById()。value在表单的隐藏字段ASP.NET网页中不起作用 - document.getElementById().value does not work in form's hidden field asp.net web pages jQuery document.ready + Asp.Net ContentPlaceholder导致Visual Studio智能问题 - jQuery document.ready + Asp.Net ContentPlaceholder cause Visual Studio intellisence problems 从C#Asp.net后面的代码设置输入文本字段的值 - Set the value of input text field from code behind C# Asp.net 尝试使用隐藏字段将值从ASP.NET JavaScript脚本传递给我的C#代码隐藏函数 - Trying to pass value from ASP.NET JavaScript script to my C# Code Behind function using Hidden Field 在ASP.NET代码后面访问文档? - Access document in ASP.NET code behind? ASP.NET隐藏字段的OnValueChange - ASP.NET Hidden field's OnValueChange 在ScriptManager.RegisterStartupScript之后的代码中调用脚本后,jQuery document.ready不会触发 - jQuery document.ready doesn't fire after script called in code behind ScriptManager.RegisterStartupScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM