简体   繁体   中英

Trying to get javascript variable in vb.net codebehind

I have a function where I am trying to get the username and password that the user entered. It IS being stored in "unixName" and "unixPass" on the client side. I have dUnixName and dUnixPass which one is a hidden input and the other is a label. They are different because I was playing around with different ways at getting this to work.

<script type="text/javascript">
//        Internet Explorer/Firefox needs this script to show radio selection after Modal Popup
function enableRDO() {
    document.getElementById("rdoUnix").checked = true;
   // document.getElementById("dUnixName").value = document.getElementById("unixName").value;
    //document.getElementById("dUnixPass").value = document.getElementById("unixPass").value;
    document.getElementById('<%=dUnixName.ClientID %>').value = document.getElementById("unixName").value;
    document.getElementById('<%=dUnixPass.ClientID %>').value = document.getElementById("unixPass").value;
    return true;
};

If I understand correctly: You have a value in javascript that you want to pass back to the server on a post back? Thats no problem, store the value in a an ASP:HiddenField and read it out in code behind. If I misunderstood your question let me know.

See: Access an asp:hiddenfield control in JavaScript

Example

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function test() {
            alert(document.getElementById('<%=txtBox.ClientID %>').value);

            return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtBox" runat="server" />
        <button onclick='test()'>Client Side Test</button>

    </div>
    <asp:Button ID="btnServer" runat="server" Text="Server Side Test" 
        onclick="btnServer_Click" style="height: 26px" />
    </form>
</body>
</html>  


protected void btnServer_Click(object sender, EventArgs e)
{
    //read value here
    string test = txtBox.Text;
}

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