Home > Blockchain >  How to pass data from (front-end) .aspx to (back-end) .aspx.cs using hidden filed
How to pass data from (front-end) .aspx to (back-end) .aspx.cs using hidden filed

Time:01-10

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):-


public string amt;

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

front-end (.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 enter image description here

Edit: Above used jquery.

Of course the js code above used jQuery.

however, we could assume pure js code, no jQuery.

so, the js code would then become this:

    <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. 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.

If you don't want to use clientidmode=static for the hidden field, then the above code then becomes this:

hidden field thus is this: (no client id mode).

    <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.

  • Related