在页面加载时将隐藏字段值传递给C#,然后在页面加载中使用它 [英] Pass Hidden Field value to C# at page load then use it in page load

查看:59
本文介绍了在页面加载时将隐藏字段值传递给C#,然后在页面加载中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何在页面加载时在asp.net中将变量从JavaScript传递到C#

我使用隐藏字段,但它会在页面加载时为null但是当我在点击按钮上使用时,我得到它的价值。

但我需要在加载时获得c#的值而不使用按钮或任何用户操作..



我用这个

Hi,
How to pass variable from JavaScript to C# in asp.net at page load
I use a hidden field but it will give me null on page load but the value i get it when i use on click button.
but i need to get the value at c# at on-load time without using buttons or any user action..

I use this

at JavaScript

    <script>SetHiddenVariable()</script>
    <script type="text/javascript">
        function SetHiddenVariable() {
            var jsVar = "dotnetcurry.com";

            var hiddenControl = '<%= inpHide.ClientID %>';
        document.getElementById(hiddenControl).value = jsVar;
    }
    </script>

---------------------------------------------
at html form

 <input id="inpHide" type="hidden" runat="server" />
        <asp:TextBox ID="txtJSValue" runat="server"></asp:TextBox>
        <asp:Button ID="btnJSValue" Text="Click to retreive Javascript Variable" runat="server" onclick="btnJSValue_Click"/>

-----------------------------------------

at c#

   protected void btnJSValue_Click(object sender, EventArgs e)
        {
            txtJSValue.Text = inpHide.Value;
        }



我需要在没有onclick功能的情况下传递数据....



谢谢


I need the data passing without onclick function....

thanks

推荐答案

问题在于Load事件多次发生:一次首次请求页面时,以及随后当用户与之交互时导致事件导致回到服务器的帖子。点击按钮例如。



但是......在第一种情况下,首次请求页面时,在页面加载之前不会执行Javascript代码 - 这是在Load事件发生之后。因此,当加载发生时,隐藏字段中没有值,因为页面尚未呈现,除了您在设计页面时放入的页面。



页面完全呈现后只有一个不同的值,这意味着在发生回发后。
The problem is that the Load event happens at several times: once when the page is first requested and subsequently when the user interacts with it in such a way as to cause a post back to the server. A button click for example.

But...in the first case, when the page is first requested, the Javascript code is not executed until the page is loaded - which is after the Load event has occurred. So when the Load happens, there is no value in the hidden field because the page has not been rendered yet, other than that you put in there when you designed the page.

There is only a different value after the page has been fully rendered, and that means after a postback has occurred.


这篇关于在页面加载时将隐藏字段值传递给C#,然后在页面加载中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆