简体   繁体   中英

How to get client side value from server side using VB.NET?

I have an ASPX page where I set data with JS but I am having a bad time trying to get this data from VB. This is a simplified version of what I have...

ASPX:

<body onload="DoStuff();">
<script>
    function DoStuff() {
        var myvalue = document.getElementById('lblValue');
        myvalue.textContent = "blah";
        console.log (myvalue.textContent); // just to be sure that the value IS there
    }
</script>

<div style="display:block">
    <asp:label id="lblText" visible="true" runat="server">Text: </asp:label><asp:label id="lblValue" runat="server" visible="true" ></asp:label><br /><br />
    <asp:label id="lblMsg" visible="true" runat="server" >Message: </asp:label><asp:label id="lblMsgValue" runat="server" visible="true" >Click button...</asp:label><br /><br />
    <asp:Button id="btnGo" Text="Go" OnClick="btnGo_Click" runat="server"/>
</div>
</body>

ASPX.VB:

Protected Sub btnGo_Click(sender As Object, e As EventArgs)
    lblMsgValue.Text = "The value is *" & lblValue.Text & "*"
End Sub

I am always getting nothing as output. Any idea?

How interesting. I had NEVER noticed that a label set client side does NOT persist.

Note that if you use code behind, set a label.Text, then the label DOES persist and survive a round trip. However, setting the label in JS? Well, you see the browser udpate instant, but when you post back, you lose that text.

This means you need to use say a text box - they DO persist, and even when changed client side.

So, for example this code:

        <asp:Button ID="Button2" runat="server" 
              Text="Js Code" Width="114px" 
              OnClientClick="setlabel();return false" />
        <br />

     <asp:Label ID="Label1" runat="server" Text="a" ClientIDMode="Static" ViewStateMode="Enabled"></asp:Label>
        <br />
        <asp:TextBox ID="TextBox1" runat="server" ClientIDMode="Static"></asp:TextBox>

        <script>
            function setlabel() {

                vlable = document.getElementById("Label1");
                vlable.innerHTML = 'Hello how are you';

                vtextBox = document.getElementById("TextBox1")
                vtextBox.value = "Js setup text box"

            }

        </script>

So, after we run the above button (js), then both the text box, and the label will update and you see the results in the browser.

在此处输入图像描述

However, now drop in another button like this:

        <asp:Button ID="Button1" runat="server" Height="26px" Text="Set Lable" Width="129px" />

And code behind:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Debug.Print(Label1.Text)
    Debug.Print("Text box = " & TextBox1.Text)

End Sub

Output (after running client side js button).

a
Text box = Js setup text box

So we see the Text="a" of the label, while it did change in the browser, when we post back, it does not survive WHEN changed client side.

As noted, if code behind changes a label - it does persist, and does survive around trips. But the label does not.

However, a text box, or hidden field and most controls that show/have the ability to hold data or a value will survive - labels are just not one of those controls.

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