简体   繁体   English

如何使用隐藏文件将数据从(前端).aspx 传递到(后端).aspx.cs

[英]How to pass data from (front-end) .aspx to (back-end) .aspx.cs using hidden filed

I want to pass data from back-end to front-end and front-end to back-end so far I have tried like below到目前为止,我想将数据从后端传递到前端,从前端传递到后端,我已经尝试过如下

back-end to front-end:-后端到前端:-

back-end (.aspx.cs):-后端(.aspx.cs):-


public string amt;

protected void Page_Load(object sender, EventArgs e)
{
amt = "100";
}

front-end (.aspx):-前端(.aspx):-


<body>
 <form id="form1" runat="server">
<script type="text/javascript">
var amt = "<%=amt%>";
alert(amt); // data coming
</script>
 </form>
</body>

The above example is working fine but while passing the value from front-end to back-end I'm getting the null("") value (for this concept I have read this article)上面的示例工作正常,但是在将值从前端传递到后端时,我得到了 null("") 值(对于这个概念,我已经阅读了这篇文章)

front-end to back-end:-前端到后端:-

front-end (.aspx):-前端(.aspx):-


<body>
 <form id="form1" runat="server">
<script type="text/javascript">
                var amt = "<%=amt%>";

                alert("amt :- " + amt);

                function getval() {
                    var keyid = "1234";
                    document.getElementById('key_id').value = keyid;

                    alert(document.getElementById('key_id').value);
                    alert('hit');
                    window.location.href = "http://localhost:49855/ValuePassig.aspx";
                }
                   //alert(amt);
            </script>

            <input id="key_id" runat="server" type="hidden" name="key_id_1" />
            <input type="button" id="btn" value="click" runat="server" onclick="getval()" />

 </form>
</body>

back-end(.aspx.cs):-后端(.aspx.cs):-


 public string amt;
protected void Page_Load(object sender, EventArgs e)
{

amt = "100";

//I'm getting the null("") value 
//string kId = this.Request["key_id_1"];
//string kId = Request.Form["key_id_1"]; 
string kId = key_id.Value; //Initially the value come null(acceptable) and next I'm clicking on the "click" button at that time null value should not come(but coming)

Response.Write(kId);

}

I did my best so far to achieve this concept and I don't why I'm getting a null value because, I have followed the article also(above mentioned link) to achieve this concept到目前为止,我已尽最大努力实现这个概念,但我不知道为什么我会得到 null 值,因为我也按照文章(上面提到的链接)来实现这个概念

Suggest me where I did the mistake to pass the value from front-end to back-end and how to achieve this建议我在哪里将价值从前端传递到后端以及如何实现这一点

Please give me your best suggestions.请给我你最好的建议。

Note:- I have changed the code for better understanding that is button added and when I click on the button the hidden value should come back-end.注意:- 我已经更改了代码,以便更好地理解添加的按钮,当我单击按钮时,隐藏值应该出现在后端。

Ok, so we want to have some value - set in code behind cs, to set/pass/have some value for use in the client side js code.好的,所以我们想要一些值 - 在 cs 后面的代码中设置,以设置/传递/具有一些值以供在客户端 js 代码中使用。

And of course in the js code, we want use of that value, and ALSO to be able to change that value, and then upon actions, or code behind, we want that value passed back to the code behind.当然,在 js 代码中,我们希望使用该值,并且还能够更改该值,然后在执行操作或隐藏代码时,我们希望将该值传递回隐藏代码。

First up, don't use a server side expression to "set" that value for use in the js code.首先,不要使用服务器端表达式来“设置”该值以在 js 代码中使用。 The reason of course then you don't have a easy way to pass back and have use of that change value in the code behind.原因当然是您没有简单的方法来传回并在后面的代码中使用该更改值。

You can freely change the var in js code, but you really don't have a easy/nice way to get that value back to the code behind (so that <%= %> expression is a one way street to the client side.您可以在 js 代码中自由更改 var,但您确实没有一种简单/好的方法将该值返回给后面的代码(因此 <%= %> 表达式是通往客户端的单向街道。

There are a LOT of ways to do this, but probably best is to drop in a hidden field control (as per your question title)..有很多方法可以做到这一点,但最好的方法可能是放入一个隐藏的字段控件(根据您的问题标题)..

You can also use a hidden text box, but might as well use the hidden field.您也可以使用隐藏文本框,但也可以使用隐藏字段。

So, lets on page load (and ONLY first page load - like all setup on the page should be inside of the.IsPostBack code block - all web pages quite much need this !IsPostBack code block).因此,让页面加载(并且只加载第一页——就像页面上的所有设置都应该在 .IsPostBack 代码块内——所有 web 页面都非常需要这个 !IsPostBack 代码块)。

And bonus?还有奖金? the Hidden field control has automatic view state. (that means the value will persist on page post-backs).隐藏字段控件具有自动视图 state。(这意味着该值将保留在页面回发中)。

So, lets drop in a server side button to "show" the value.因此,让我们放入一个服务器端按钮来“显示”该值。

And THEN lets drop in a button (client side) to show the value, and ALSO to modify the value.然后让我们放入一个按钮(客户端)来显示值,还可以修改值。

    <asp:HiddenField ID="MyHotelName" runat="server" ClientIDMode="Static" />
    <h3>Server side code</h3>
    <asp:Button ID="cmdShowServer" runat="server" OnClick="cmdShowServer_Click"
        Text="Show Hotel Name" CssClass="btn" />
    <br />
    <asp:Label ID="lblShow" runat="server" Text="Label"></asp:Label>


    <h3>Client side code</h3>
    <asp:Button ID="cmdShowClient" runat="server" Text="Show Hotel Name"
        OnClientClick="ShowHotel();return false" />
    <br />
    <asp:Button ID="cmdChangeClient" runat="server" Text="Change Hotel Name"
        OnClientClick="ChangeHotel();return false" />

    <script>

        function ShowHotel() {
            alert("Hotel name = " + $("#MyHotelName").val())
        }

        function ChangeHotel() {
            sHotelNew = prompt("Enter new hotel value")

            $("#MyHotelName").val(sHotelNew)

        }

    </script>

And our code behind:以及我们背后的代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MyHotelName.Value = "Zoo";
        }
    }

    protected void cmdShowServer_Click(object sender, EventArgs e)
    {
        lblShow.Text = "Value of hotel = " + MyHotelName.Value;
    }

So, we now have this:所以,我们现在有这个: 在此处输入图像描述

Edit: Above used jquery.编辑:以上使用 jquery。

Of course the js code above used jQuery.当然上面的js代码使用了jQuery。

however, we could assume pure js code, no jQuery.但是,我们可以假设纯 js 代码,没有 jQuery。

so, the js code would then become this:所以,js代码会变成这样:

    <script>

        function ShowHotel() {

            sHotel = document.getElementById("MyHotelName").value
            alert("Hotel name = " + sHotel)
        }

        function ChangeHotel() {

            sHotelNew = prompt("Enter new hotel value")
            document.getElementById("MyHotelName").value = sHotelNew

        }

    </script>

I should also point out the "very" imprortant adding of clientidmode="static" for the hidden field.我还应该指出为隐藏字段添加“非常”重要的 clientidmode="static"。 This will "prevent" asp.net system from changing the "id" used for the control, and as a result, the js code tends to be more "clean" and "easy" to reference controls.这将“防止”asp.net 系统更改用于控件的“id”,因此,js 代码往往更“干净”和“易于”引用控件。

If you don't want to use clientidmode=static for the hidden field, then the above code then becomes this:如果你不想为隐藏字段使用 clientidmode=static,那么上面的代码就变成了这样:

hidden field thus is this: (no client id mode).因此,隐藏字段是这样的:(无客户端 ID 模式)。

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

And now our code becomes this:现在我们的代码变成了这样:

    <script>

        function ShowHotel() {

            sHotel = document.getElementById('<%= MyHotelName.ClientID %>').value
            alert("Hotel name = " + sHotel)
        }

        function ChangeHotel() {

            sHotelNew = prompt("Enter new hotel value")
            document.getElementById('<%= MyHotelName.ClientID %>').value = sHotelNew

        }
    </script>

So, I often will toss in a ClientIDMode="static" for the hidden field, as that makes the js code to get the hidden control less messy.所以,我经常会在隐藏字段中加入 ClientIDMode="static",因为这使得获取隐藏控件的 js 代码不那么混乱。

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

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