0

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;
};

4
  • 1
    Can you explain exactly what the problem is? Are you saying that the value you're trying to put into dUnixName is not available on the server at post-back? Commented Jul 23, 2012 at 14:19
  • Correct. I have a modal window asking the user for their username and password. It is putting the value into dUnixName/dUnixPass. I want to be able to use dUnixName in the codebehind file. As of right now using IE it is not available for me. I have MsgBox popups testing to see if the server is able to call the variables. It is not working atm. Commented Jul 23, 2012 at 14:30
  • 1
    Please can you edit your question to show all the controls you are trying to copy the values from, all the controls you are trying to copy the value to, the current JavaScript function - and also state where you are checking for the return value (i.e. which event such as Init, Load, etc)? Commented Jul 23, 2012 at 14:33
  • Hmmm... not sure. Have you tried running debug alert("..."); calls in your javascript to prove that the objects are being found correctly? I can thoroughly recommend FireBug for FireFox or use the developer tools under IE or Chrome (F12 on both). Commented Jul 23, 2012 at 14:44

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

8 Comments

Yes, that is exactly what I want to do. I followed your link but I don't know if that matches it though. I changed the javascript to document.getElementById("<%= dUnixName.ClientID %>").value = document.getElementById("unixName").value; document.getElementById("<%= dUnixPass.ClientID %>").value = document.getElementById("unixPass").value;" and it still isn't passing the value. I am calling it in my codebehind as Dim UNIXUSERNAME As String = dUnixName.Value Dim UNIXPASSWORD As String = dUnixPass.Value
Is there a reason you aren't just using ASP.Net textboxes...?
I was, in my original code you will see "<asp:TextBox ID="dUnixPass" ClientIDMode="Static" runat="server" />". I was trying to play around with HiddenView and textboxes but I can't get either to work. It is very frustrating :(
It also seems that this code has been working just fine in Google Chrome but not in Internet Explorer 9...
Odd, I only tested it in IE... I wonder if you have some issue with your setup?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.