简体   繁体   中英

How to get the value of hidden field and pass it to another aspx page? (C#)

how can I get the value of hidden field in my code behind?

I have tried getting it using javascript, but I want to do it in code behind so I can use it when I pass the value on the other page.

function load() {
    var lblAddress = document.getElementById("hdnAddress").value;
    var lblName = document.getElementById("hdnName").value;
    var lblEmail = document.getElementById("hdnEmail").value;
    var lblPhone = document.getElementById("hdnPhone").value;
    var lblMsg = document.getElementById("hdnMsg").value;

    window.parent.document.getElementById("lblAddress").innerText = lblAddress;
    window.parent.document.getElementById("lblName").innerText = lblName;
    window.parent.document.getElementById("lblEmail").innerText = lblEmail;
    window.parent.document.getElementById("lblPhone").innerText = lblPhone;
    window.parent.document.getElementById("lblMsg").innerText = lblMsg;
    //    alert(lblAddress);
}

NOTE:

  1. The flow of this is:

    • first the hidden fields is in separate page(Page2).
    • then I will get the value of that using the javascript code above and set the value of label on the other page(Page1) with the value of hidden field.
  2. The Page2 is placed on the iframe inside Page1.

You could use query parameters when loading the page that goes into the iframe. Something like this:

function load() {
    var lblAddress = document.getElementById("hdnAddress").value;
    var lblName = document.getElementById("hdnName").value;
    var lblEmail = document.getElementById("hdnEmail").value;
    var lblPhone = document.getElementById("hdnPhone").value;
    var lblMsg = document.getElementById("hdnMsg").value;

    var iframe = document.getElementById("myIframe");
    iframe.src = 'Page2.aspx?address=' + encodeURIComponent(lblAddress)
        + '&name=' + encodeURIComponent(lblName)
        + '&email=' + encodeURIComponent(lblEmail)
        + '&phone=' + encodeURIComponent(lblPhone)
        + '&msg=' + encodeURIComponent(lblMsg);
}

Be sure to use encodeURIComponent when building the URL to avoid injection attacks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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