0

the thing is that I have a form with a textbox and a button and a hidden field, now what i want to do is to write something in the textbox ,pass it to the hidden field and then access that thing written in the hidden field in the controller . how can i do that?

3 Answers 3

3

You can use OnSubmit form event, and javascript to copy data from TextBox to the hidden field. Here is a simple html exmple:

<html>

<head>
<script type="text/javascript">
function AddDataToHidden()
{
  document.getElementById('test').value = document.getElementById('login').value;
}
</script>
</head>

<body>
  <form action="action.aspx" onsubmit="javascript:return AddDataToHidden();" method="get">
    <input type="text" id="login" name="login" />
    <input type="hidden" id="test" name="test" />
    <input type="submit" />
  </form>
</body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

For best usage you can change document.getElementById('test') to jQuery selectors, if you are using jQuery.
3

Why a hidden field?

Why can't you simply pass the Value of the TextField using jQuery's OnButtonClick handler?

Below is the JavaScript code:-

$(document).ready(function(){
$("#Create").click(function(){
var data1 = $('#TextField').val();

 $.ajax({
         type:"Post",
         url:'/Controller/SomeFunction',
         data:"Name="+ data1,
         success:function(result)
         {
            alert(result);
         },
         error:function(result)
         {
            alert("fail");
         }
     }); 
   });
});

Comments

1

Why do you need a hidden field for that? If you have a textbox, then on form submit, the value from the textbox will be directly available.

I might not understand you correctly, but from what it looks, your textbox's value will always have the same value as the hidden one..

What exactly are you trying to accomplish then?

Comments

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.