简体   繁体   English

如何使用 VB.NET 从服务器端获取客户端值?

[英]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.我有一个 ASPX 页面,我在其中使用 JS 设置数据,但我在尝试从 VB 获取这些数据时遇到了麻烦。 This is a simplified version of what I have...这是我所拥有的简化版本...

ASPX: 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: 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.我总是一无所获,因为 output。 Any idea?任何想法?

How interesting.很有意思。 I had NEVER noticed that a label set client side does NOT persist.我从未注意到 label 设置客户端不会持续存在。

Note that if you use code behind, set a label.Text, then the label DOES persist and survive a round trip.请注意,如果您使用后面的代码,请设置 label.Text,然后 label 会持续存在并在往返过程中存活。 However, setting the label in JS?但是,在JS中设置label? 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.因此,在我们运行上述按钮 (js) 后,文本框和 label 都会更新,您会在浏览器中看到结果。

在此处输入图像描述

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). Output(运行客户端js按钮后)。

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.所以我们看到 label 的 Text="a",虽然它在浏览器中确实发生了变化,但当我们回发时,它在更改客户端时无法生存。

As noted, if code behind changes a label - it does persist, and does survive around trips.如前所述,如果后面的代码更改了 label - 它确实会持续存在,并且会在旅途中存活下来。 But the label does not.但 label 没有。

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.但是,文本框或隐藏字段以及大多数显示/能够保存数据或值的控件将继续存在 - 标签不是这些控件之一。

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

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